Red and Black

Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other)
Total Submission(s) : 45   Accepted Submission(s) : 34

Font: Times New Roman | Verdana | Georgia

Font Size: ← →

Problem Description

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)

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

————————————————————————————————————————————————————

找出@所在位置的连通块,直接DFS搜下


  1. #include<iostream>
  2. #include<cmath>
  3. using namespace std;
  4. char map[105][105];
  5. int m, n, t;
  6. int dir[8][2] = {{ -1, 0 },{ 0, -1 }, { 0, 1 },{1, 0 }};
  7.  
  8. void dfs(int si, int sj)
  9. {
  10. if (si <= 0 || sj <= 0 || si > m || sj > n)
  11. return;
  12. t++;
  13. for (int i = 0; i < 4; i++)
  14. {
  15. if (map[si + dir[i][0]][sj + dir[i][1]] != '#')
  16. {
  17. map[si + dir[i][0]][sj + dir[i][1]] = '#';
  18. dfs(si + dir[i][0], sj + dir[i][1]);
  19. }
  20. }
  21. return;
  22. }
  23. int main()
  24. {
  25. int si, sj;
  26. while (cin >> n >> m&&(m||n))
  27. {
  28. for (int i = 1; i <= m;i++)
  29. for (int j = 1; j <= n; j++)
  30. cin >> map[i][j];
  31. for (int i = 1; i <= m; i++)
  32. for (int j = 1; j <= n; j++)
  33. {
  34. if (map[i][j] == '@')
  35. {
  36. si = i;
  37. sj = j;
  38. break;
  39. }
  40. }
  41.  
  42. map[si][sj] = '#';
  43. t = 0;
  44. dfs(si, sj);
  45. printf("%d\n", t);
  46. }
  47. return 0;
  48.  
  49. }

HDU1312 Red and Black(DFS) 2016-07-24 13:49 64人阅读 评论(0) 收藏的更多相关文章

  1. HDU1426 Sudoku Killer(DFS暴力) 2016-07-24 14:56 65人阅读 评论(0) 收藏

    Sudoku Killer Problem Description 自从2006年3月10日至11日的首届数独世界锦标赛以后,数独这项游戏越来越受到人们的喜爱和重视. 据说,在2008北京奥运会上,会 ...

  2. Hdu1010 Tempter of the Bone(DFS+剪枝) 2016-05-06 09:12 432人阅读 评论(0) 收藏

    Tempter of the Bone Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Othe ...

  3. HDU1045 Fire Net(DFS枚举||二分图匹配) 2016-07-24 13:23 99人阅读 评论(0) 收藏

    Fire Net Problem Description Suppose that we have a square city with straight streets. A map of a ci ...

  4. HDU1078 FatMouse and Cheese(DFS+DP) 2016-07-24 14:05 70人阅读 评论(0) 收藏

    FatMouse and Cheese Problem Description FatMouse has stored some cheese in a city. The city can be c ...

  5. HDU2212 DFS 2016-07-24 13:52 56人阅读 评论(0) 收藏

    DFS Problem Description A DFS(digital factorial sum) number is found by summing the factorial of eve ...

  6. HDU1181 变形课(DFS) 2016-07-24 13:31 73人阅读 评论(0) 收藏

    变形课 Problem Description 呃......变形课上Harry碰到了一点小麻烦,因为他并不像Hermione那样能够记住所有的咒语而随意的将一个棒球变成刺猬什么的,但是他发现了变形咒 ...

  7. leetcode N-Queens/N-Queens II, backtracking, hdu 2553 count N-Queens, dfs 分类: leetcode hdoj 2015-07-09 02:07 102人阅读 评论(0) 收藏

    for the backtracking part, thanks to the video of stanford cs106b lecture 10 by Julie Zelenski for t ...

  8. Red and Black(BFS or DFS) 分类: dfs bfs 2015-07-05 22:52 2人阅读 评论(0) 收藏

    Description There is a rectangular room, covered with square tiles. Each tile is colored either red ...

  9. Hdu1427 速算24点 2017-01-18 17:26 46人阅读 评论(0) 收藏

    速算24点 Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other) Total Submiss ...

随机推荐

  1. 【Java杂记】Equals 和 hashCode

    equals 和 hashCode含义 equal:判断两个对象是否相等,如果相同,返回true 否则返回false hashcode: 返回一个int数 Object 默认(内部地址转化为一个数字) ...

  2. tomcat 设置内存

    SET JAVA_OPTS=-Xms256m -Xmx512m -XX:PermSize=256M -XX:MaxPermSize=512M -Xms :初始化堆内存值 -Xmx :堆内存最大值 -X ...

  3. Fiddler命令行和HTTP断点调试

    fiddler设置断点: fiddler->rules->automatic Breakpoints->选择断点方式,这种方式下设定的断点会对之后的所有HTTP请求有效. 有两个断点 ...

  4. javascritp伪协议

    [javascritp伪协议] 将javascript代码添加到客户端的方法是把它放置在伪协议说明符javascript:后的URL中.这个特殊的协议类型声明了URL的主体是任意的javascript ...

  5. mongo远程登录

    1. 进入数据库: use admin db.addUser("foo","foo"); ps:高版本用db.createUser创建. 2. 改配置 如/et ...

  6. Web站点性能拨测脚本

    功能:检测自己本地访问目标网站的返回状态.访问质量信息 [root@localhost src]# cat get_site_status.sh #! /usr/bin/env bash if [[ ...

  7. 仿微信客户端 帧布局中加入fragment

    学习内容来自“慕课网” 这里用Fragment来实现APP主界面 思路: 底部横向排列4个LinearLayout,每个LinearLayout包含一个图片按钮和一个文字 1.默认显示第一个功能(微信 ...

  8. WebApi是轻量级的,WCF是重量级的,可以Api调用WCF,更灵活

    WCF.WebAPI.WCFREST.WebService之间的区别 注明:转载 在.net平台下,有大量的技术让你创建一个HTTP服务,像Web Service,WCF,现在又出了Web API.在 ...

  9. WCF的例子

    Demo的 “Service端”以本机IIS为宿主,“Client端”以WebForm项目为例. 1.新建项目:WCF>WCF Service Application: 2.删除默认文件ISer ...

  10. vue2.0 MintUI安装和基本使用

    http://mint-ui.github.io/docs/#/en2 Mintui 详细地址 基于2.0的安装 npm install mint-ui -S 主要就三行指令 import Mint ...