题目:

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. Centos6.8防火墙配置

    1.基本操作 # 查看防火墙状态 service iptables status # 停止防火墙 service iptables stop # 启动防火墙 service iptables star ...

  2. Struts简介、原理及简单实现

    struts简介 Struts是Apache软件基金会(ASF)赞助的一个开源项目.它最初是jakarta项目中的一个子项目,并在2004年3月成为ASF的顶级项目.它通过采用JavaServlet/ ...

  3. openldap 搭建

    环境构建 1)软件安装: yum -y install openldap-servers openldap-clients openldap openldap-devel migrationtools ...

  4. celery出现警告或异常的解决方式

    做个笔记,记录下使用celery踩过的坑,不定期更新.  warnings.warn(CDeprecationWarning(W_PICKLE_DEPRECATED)) 我用的是Flask,所以在Fl ...

  5. python词云的制作方法

    第一次接触到词云主要是觉得很好看,就研究了一下,官方给出了代码的,但是新手看的话还是有点不容易,我们来尝试下吧. 环境:python2.7 python库:PIL(pillow),numpy,matp ...

  6. Java的注释和Javadoc在eclipse生成的方法 – Break易站

    本文内容来自:Java的注释和Javadoc在eclipse生成的方法 – Break易站 1.  Java的注释 Java里有两种注释风格.下面这个写法是非常常见的 1 2 3 4 /*This i ...

  7. PXE+kickstart网络安装CentOS7.4系统及过程中各种报错

    环境:关闭防火墙.selinux 注意:虚拟机进行网络安装的话,7.3以后的系统是需要2G以上的内存 [root@kickstart ~]# cat /etc/redhat-release CentO ...

  8. 前端自动化(三) 合并压缩css、压缩js、添加时间戳、打包上线操作

    前端自动化(三)   合并压缩css.压缩js.添加时间戳.打包上线操作 此文在前两篇基础上对比参考,会很方便理解 gulp.task("delete",function(){ r ...

  9. mycat全局自增

    指定自增类型 [root@node002 conf]# vi   /usr/local/mycat/conf/server.xml 每个参数代表的含义: 0:本地文件自增方式. 1:使用mysql自增 ...

  10. Mysql查询不为null值

    Mysql本以为查询不为null就是!=null可是结果查询出来什么都没有,后来才发现不为null应该是is not null ,为null应该是is null.