当前位置:网站首页>-红与黑-
-红与黑-
2022-08-09 23:46:00 【-JMY-】
题目描述
有一间长方形的房子,地上铺了红色、黑色两种颜色的正方形瓷砖。你站在其中一块黑色的瓷砖上,只能向上下左右四个方向的相邻的黑色瓷砖移动。请写一个程序,计算你总共能够到达多少块黑色的瓷砖。
输入
第一行是两个整数W和H,分别表示x方向和y方向瓷砖的数量。W和H都不超过20。在接下来的H行中,每行包括W个字符。每个字符表示一块瓷砖的颜色,规则如下
1)‘.’:黑色的瓷砖;
2)‘#’:白色的瓷砖;
3)‘@’:黑色的瓷砖,并且你站在这块瓷砖上。该字符在每个数据集合中唯一出现一次。
输出
输出一行,显示你从初始位置出发能到达的瓷砖数(记数时包括初始位置的瓷砖)。
样例输入
6 9 ....#. .....# ...... ...... ...... ...... ...... #@...# .#..#.
样例输出
45
参考代码:
#include<bits/stdc++.h>
using namespace std;
int w,h,l;
char s[105][105];
void around(int i,int j){
if(s[i][j]=='#'||i<1||i>w||j<1||j>h)
return;
s[i][j]='#';
l++;
around(i,j-1);
around(i-1,j);
around(i,j+1);
around(i+1,j);
return;
}
int main(){
cin>>h>>w;
for(int i=1;i<=w;i++)
for(int j=1;j<=h;j++)
cin>>s[i][j];
for(int i=1;i<=w;i++)
for(int j=1;j<=h;j++)
if(s[i][j]=='@'){
around(i,j);
cout<<l;
return 0;
}
return 0;
}
边栏推荐
猜你喜欢
随机推荐
第十二,十三章 mysql数据类型,视图的课后练习
数字孪生智慧制造生产线项目实施方案,平台认知与概念
[C language] In-depth understanding of pointers and arrays (issue 4)
Redisson 分布式锁
New window Display Agreement
The technical aspects of the byte have been passed, and the salary has been negotiated for 20K*13, but the result is still being brushed. I asked the HR why...
Leetcode79. 单词搜索
[C language] Address book "Static Memory Version"
[SUCTF 2019]CheckIn (.htaccess和.user.ini)
《痞子衡嵌入式半月刊》 第 60 期
数据的存储——C语言
Redis redisTemplate.execute 执行锁
Redis 非关系型数据库学习(一) ---- Redis 的安装
温度响应性纳米水凝胶光子品体/纤维素修饰荧光水凝胶/载脂质体水凝胶的制备方法
win10重装系统后没声音怎么办?
Leetcode82. 删除排序链表中的重复元素 II
hql语言
mysql无法远程连接 Can‘t connect to MySQL server on ‘xxx.xxx.xxx.xxx‘ (10060 “Unknown error“)
Leetcode81. 搜索旋转排序数组 II
Leetcode83. 删除排序链表中的重复元素









