题目描述

There is a rectangular room, covered with square tiles. Each tile is colored either red or black. A man is standing on a black tile. From a tile, he can move to one of four adjacent tiles. But he can't move on red tiles, he can move only on black tiles.

Write a program to count the number of black tiles which he can reach by repeating the moves described above.

输入

The input
consists of multiple data sets. A data set starts with a line containing
two positive integers W and H; W and H are the numbers of tiles in the
x- and y- directions, respectively. W and H are not more than 20.

There are H more lines in the data set, each of which
includes W characters. Each character represents the color of a tile as
follows.

'.' - a black tile
'#' - a red tile
'@' - a man on a black tile(appears exactly once in a data set)

The end of the input is indicated by a line consisting of two zeros.

Output

For each data set, your program should output a line which contains the
number of tiles he can reach from the initial tile (including itself).

Sample Input

6 9
....#.
.....#
......
......
......
......
......
#@...#
.#..#.
11 9
.#.........
.#.#######.
.#.#.....#.
.#.#.###.#.
.#.#..@#.#.
.#.#####.#.
.#.......#.
.#########.
...........
11 6
..#..#..#..
..#..#..#..
..#..#..###
..#..#..#@.
..#..#..#..
..#..#..#..
7 7
..#.#..
..#.#..
###.###
...@...
###.###
..#.#..
..#.#..
0 0

Sample Output

45
59
6
13 解题思路:
深搜的方法解决,题目意思就是从@开始找.并与@连通,碰到#等于碰到了墙,题目很简单,@可以向四个方向上、下、左、右走,所以 用四个坐标标记出来,然后,再一一遍历,递归调用寻找,用一个30*30的数组标识此点有没有走过,避免走重复 程序代码:
#include <cstdio>
#include <cstring>
using namespace std;
int n,m,cot;
char map[][];
int to[][] = {{,},{,},{-,},{,-}}; void dfs(int i,int j)
{
cot++;
map[i][j] = '#';
for(int k = ; k<; k++)
{
int x = i+to[k][];
int y = j+to[k][];
if(x<n && y<m && x>= && y>= && map[x][y] == '.')
dfs(x,y);
}
return;
} int main()
{
int i,j,fi,fj;
while(~scanf("%d%d%*c",&m,&n)&&m&&n)
{
for(i = ; i<n; i++)
{
for(j = ; j<m; j++)
{
scanf("%c",&map[i][j]);
if(map[i][j] == '@')
{
fi = i;
fj = j;
}
}
getchar();
}
cot= ;
dfs(fi,fj);
printf("%d\n",cot);
} return ;
}

最新文章

  1. Centos7 升级内核和应用TCP BBR 算法
  2. 关于UIScollView 中的contentOffset 的理解
  3. 利用 filter 机制 给 静态资源 url 加上时间戳,来防止js和css文件的缓存,利于开发调试
  4. ahjesus Axure RP 7.0注册码
  5. vncserver安装
  6. 找出如下数组中最大的元素和最小的元素, a[][]={{3,2,6},{6,8,2,10},{5},{12,3,23}}
  7. FACADE
  8. 查看nginx版本号
  9. 03_线程的创建和启动_实现Runnable接口方式
  10. 『安全科普』WEB安全之渗透测试流程
  11. 2014.12.13 ASP.NET文件上传
  12. 实现类似MVC ViewBag类型的对象
  13. TensorFlow框架(3)之MNIST机器学习入门
  14. Talking appsettings.json in Asp.Net Core
  15. 如何发起、防御和测试XSS攻击,我们用DVWA来学习(上)
  16. 清北学堂 清北-Day1-R2-监听monitor
  17. [nginx] - 使用nginx实现反向代理,动静分离,负载均衡,session共享
  18. mybatis源码-解析配置文件(二)之解析的流程
  19. iOS自动布局框架-Masonry详解
  20. Linux--忘记MySQL密码的解决方法和输入mysqld_safe --skip-grant-tables &amp;后无法进入MySQL的解决方法

热门文章

  1. (转)webstorm快捷键
  2. Objective-C消息机制的原理
  3. jQuery 效果- 动画
  4. JavaMail API 1.4.7邮件发送
  5. Android 新版NDK环境搭建(免Cygwin)
  6. vs2008 下编译jrtplib-3.9.0成功
  7. css3基础教程十六变形与动画animation
  8. nav
  9. python进度条代码
  10. lsmod