题目:

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

  1. 6 9
  2. ....#.
  3. .....#
  4. ......
  5. ......
  6. ......
  7. ......
  8. ......
  9. #@...#
  10. .#..#.
  11. 11 9
  12. .#.........
  13. .#.#######.
  14. .#.#.....#.
  15. .#.#.###.#.
  16. .#.#..@#.#.
  17. .#.#####.#.
  18. .#.......#.
  19. .#########.
  20. ...........
  21. 11 6
  22. ..#..#..#..
  23. ..#..#..#..
  24. ..#..#..###
  25. ..#..#..#@.
  26. ..#..#..#..
  27. ..#..#..#..
  28. 7 7
  29. ..#.#..
  30. ..#.#..
  31. ###.###
  32. ...@...
  33. ###.###
  34. ..#.#..
  35. ..#.#..
  36. 0 0

Sample Output

  1. 45
  2. 59
  3. 6
  4. 13

  5. 题意描述:
    输入矩阵的大小WH(均小于20
    计算并输出从'@'位置最多能走多少块'.'
    解题思路:
    输入的时候找到'@'的位置,随后对其进行DFS搜索,下面的代码实现的搜索有点模拟广搜的意思。
    代码实现:
  1. #include<stdio.h>
  2. char map[][];
  3. int dfs(int x,int y);
  4. int w,h;
  5. int main()
  6. {
  7. int i,j,sx,sy;
  8. while(scanf("%d%d",&w,&h),w+h != )
  9. {
  10. for(i=;i<=h;i++)
  11. {
  12. for(j=;j<=w;j++){
  13. scanf(" %c",&map[i][j]);
  14. if(map[i][j]=='@')
  15. { sx=i;sy=j; }
  16. }
  17. getchar();
  18. }
  19. printf("%d\n",dfs(sx,sy));
  20. }
  21. return ;
  22. }
  23. int dfs(int x,int y)
  24. {
  25. if(x< || x>h || y< || y>w)
  26. return ;
  27. //如果进入不了dfs函数就是边界问题,注意行数和列数就是x和y的范围
  28. if(map[x][y]=='#')
  29. return ;
  30. else
  31. {
  32. map[x][y]='#';
  33. return +dfs(x-,y)+dfs(x+,y)+dfs(x,y-)+dfs(x,y+);
  34. }
  35. }

易错分析:

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. userdel 命令详解

    userdel  作用: 删除指定用户,以及用户相关的文件. 如不加选项,则仅删除用户账号,而不删除相关文件 选项: -f:强制删除用户,即时用户当前已登录 -r:删除用户的同时删除与用户相关的所有文 ...

  2. git pull与git fetch的区别

    git pull: 取回远程主机某个分支的更新,再与本地的指定分支合并. 用法: git pull <远程仓库> <远程分支名>:<本地分支名> // 如 git ...

  3. python:发送消息给微信企业号

    # -*- coding:utf-8 -*- import requests import json ''' 基础环境:微信企业号 version:python 2.7 ''' class Send_ ...

  4. COMPUTE子句和Group By

                        首先声明一下,这个COMPUTE语法在SQLServer 2012之后,就废弃使用了,详情请看https://msdn.microsoft.com/librar ...

  5. Zabbix Agent端配置文件说明

    Zabbix Agent端配置文件说明 由于工作中经常接触到zabbix,所以将agent配置整理一下,方便日常查看. # This is a config file for the Zabbix a ...

  6. Android 快速点击的处理

    为了对付拥有麒麟臂的测试人员或者用户对我们的按钮等控件展开惨无人道的快速啄击.厮以为可以用如下方法: 1 setEnabled 大法:在用户点击发生后调用setEnable(false);阻止持续受到 ...

  7. js 客户端打印html 并且去掉页眉、页脚

    print() 方法用于打印当前窗口的内容,支持部分或者整个网页打印. 调用 print() 方法所引发的行为就像用户单击浏览器的打印按钮.通常,这会产生一个对话框,让用户可以取消或定制打印请求. w ...

  8. asp.net mvc 客户端验证

    插件 jQuery unobtrusive Validation @Html.TextBoxFor(x=>x.UserName) [StringLength(7,MinimumLength=2, ...

  9. 解决打开png图片黑屏问题(批量还原Xcode优化后的png)

    window 打开Xcode 里面的png图片会黑屏,但是在mac 打开就显示正常, 这是因为Xocde里面的png图片被 pngcrush 优化过了,需要还原它的优化,window 平台才可以打开. ...

  10. 【三十】php之PDO抽象层

    1.PDO介绍(php data object) PHP 数据对象 (PDO) 扩展为PHP访问数据库定义了一个轻量级的一致接口. PDO 提供了一个数据访问抽象层,这意味着,不管使用哪种数据库,都可 ...