题目:

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.

Input

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

题意描述:
输入矩阵的大小W和H(均小于20)
计算并输出从'@'位置最多能走多少块'.'
解题思路:
输入的时候找到'@'的位置,随后对其进行DFS搜索,下面的代码实现的搜索有点模拟广搜的意思。
代码实现:
 #include<stdio.h>
char map[][];
int dfs(int x,int y);
int w,h;
int main()
{
int i,j,sx,sy;
while(scanf("%d%d",&w,&h),w+h != )
{
for(i=;i<=h;i++)
{
for(j=;j<=w;j++){
scanf(" %c",&map[i][j]);
if(map[i][j]=='@')
{ sx=i;sy=j; }
}
getchar();
}
printf("%d\n",dfs(sx,sy));
}
return ;
}
int dfs(int x,int y)
{
if(x< || x>h || y< || y>w)
return ;
//如果进入不了dfs函数就是边界问题,注意行数和列数就是x和y的范围
if(map[x][y]=='#')
return ;
else
{
map[x][y]='#';
return +dfs(x-,y)+dfs(x+,y)+dfs(x,y-)+dfs(x,y+);
}
}

易错分析:

1、如果搜索进入不了注意边界的设置问题

HDU 1979 Red and Black的更多相关文章

  1. POJ 1979 Red and Black (红与黑)

    POJ 1979 Red and Black (红与黑) Time Limit: 1000MS    Memory Limit: 30000K Description 题目描述 There is a ...

  2. HDU 1312 Red and Black --- 入门搜索 BFS解法

    HDU 1312 题目大意: 一个地图里面有三种元素,分别为"@",".","#",其中@为人的起始位置,"#"可以想象 ...

  3. HDU 1312 Red and Black --- 入门搜索 DFS解法

    HDU 1312 题目大意: 一个地图里面有三种元素,分别为"@",".","#",其中@为人的起始位置,"#"可以想象 ...

  4. HDU 1312:Red and Black(DFS搜索)

      HDU 1312:Red and Black Time Limit:1000MS     Memory Limit:30000KB     64bit IO Format:%I64d & ...

  5. HDU 1312 Red and Black (dfs)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1312 Red and Black Time Limit: 2000/1000 MS (Java/Oth ...

  6. OpenJudge/Poj 1979 Red and Black / OpenJudge 2816 红与黑

    1.链接地址: http://bailian.openjudge.cn/practice/1979 http://poj.org/problem?id=1979 2.题目: 总时间限制: 1000ms ...

  7. HDU 1312 Red and Black(DFS,板子题,详解,零基础教你代码实现DFS)

    Red and Black Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) To ...

  8. HDU 1312 Red and Black(最简单也是最经典的搜索)

    传送门: http://acm.hdu.edu.cn/showproblem.php?pid=1312 Red and Black Time Limit: 2000/1000 MS (Java/Oth ...

  9. hdu 1979 DFS + 字典树剪枝

    http://acm.hdu.edu.cn/showproblem.php?pid=1979 Fill the blanks Time Limit: 3000/1000 MS (Java/Others ...

随机推荐

  1. [编织消息框架][JAVA核心技术]动态代理应用7-IRpcSend实现

    根据设计生成两个接口,IRpcSend send方法返回数据要求包装成QResult对象 public interface IRpcSend { public <T> QResult< ...

  2. thinkphp3.2.3使用ajax 的一些坑——使用AjaxReturn()后,直接返回null,模板文件不起作用

    从接触thinkphp到今天,填完此坑,必有其他的坑有会冒出来.哎!这个填坑之路我想是没有尽头的了. 最近,需要使用ajax完成一些操作,一开始想Ajax简单啊,不过是一种提交数据的方式,不过是害苦了 ...

  3. elastaticresearch 学习过程

    1.在Windows上安装了es 2.在chrome上装了sense 3.尝试创建 es 的模板

  4. ubuntu 安装 pythonenv

    This will get you going with the latest version of pyenv and make it easy to fork and contribute any ...

  5. Linux小记

    一.在vim中如何查看正在编辑的文件名 在正常模式下: :f 或 CTRL+G 查看文件的路径 用:!pwd 可以看当前的详细路径. 二.crontab 在crontab中, 命令crontab -e ...

  6. zabbix 图形插件 Grafana的安装

    看http://www.myexception.cn/software-testing/2008870.html 就好了.

  7. 深入剖析MSAA

    本文打算对MSAA(Multisample anti aliasing)做一个深入的讲解,包括基本的原理.以及不同平台上的实现对比(主要是PC与Mobile).为了对MSAA有个更好的理解,所以写下了 ...

  8. qt中建立图片资源文件

    qt中如果你要添加图片资源文件我们需要执行以下步骤: (1)先找好一张图片,这里就不多说了,网上资源很多. (2)把我们找好的文件统一放到一个文件夹,然后拉到工程文件所在的文件夹下 (3)在qt中新建 ...

  9. [Spark性能调优] 第二章:彻底解密Spark的HashShuffle

    本課主題 Shuffle 是分布式系统的天敌 Spark HashShuffle介绍 Spark Consolidated HashShuffle介绍 Shuffle 是如何成为 Spark 性能杀手 ...

  10. Updates were rejected because the remote contains work that you do(git报错解决方案)

    Updates were rejected because the remote contains work that you do(git报错解决方案) 今天向GitHub远程仓库提交本地项目文件时 ...