题目代号:HDU 1312

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1312

Red and Black

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 20820    Accepted Submission(s): 12673

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
6 9
....#.
.....#
......
......
......
......
......
#@...#
.#..#.
11 9
.#.........
.#.#######.
.#.#.....#.
.#.#.###.#.
.#.#..@#.#.
.#.#####.#.
.#.......#.
.#########.
...........
11 6
..#..#..#..
..#..#..#..
..#..#..###
..#..#..#@.
..#..#..#..
..#..#..#..
7 7
..#.#..
..#.#..
###.###
...@...
###.###
..#.#..
..#.#..
0 0
 
Sample Output
45
59
6
13

题目大意:一个男子站在‘.’上,他不能走到‘#’上问他能走到的‘.’的数量是多少。

解题思路:初始点bfs四个方向都遍历一次即可。

差点被自己气哭,第一次提交的时候忘记初始化数组,因为没有判断边界,直接导致WA。

AC代码:

  1. # include <stdio.h>
  2. # include <string.h>
  3. # include <stdlib.h>
  4. # include <iostream>
  5. # include <fstream>
  6. # include <vector>
  7. # include <queue>
  8. # include <stack>
  9. # include <map>
  10. # include <math.h>
  11. # include <algorithm>
  12. using namespace std;
  13. # define pi acos(-1.0)
  14. # define mem(a,b) memset(a,b,sizeof(a))
  15. # define FOR(i,a,n) for(int i=a; i<=n; ++i)
  16. # define For(i,n,a) for(int i=n; i>=a; --i)
  17. # define FO(i,a,n) for(int i=a; i<n; ++i)
  18. # define Fo(i,n,a) for(int i=n; i>a ;--i)
  19. typedef long long LL;
  20. typedef unsigned long long ULL;
  21.  
  22. const int MAXM=;
  23. char a[MAXM][MAXM];
  24. int n,m,ans;
  25. int cx[]={-,,,};
  26. int cy[]={,,-,};
  27.  
  28. struct node
  29. {
  30. int x,y;
  31. };
  32.  
  33. queue<node>Q;
  34.  
  35. void bfs()
  36. {
  37. while(!Q.empty())
  38. {
  39. int x=Q.front().x;
  40. int y=Q.front().y;
  41. Q.pop();
  42. for(int i=;i<;i++)
  43. {
  44. int tx=x+cx[i];
  45. int ty=y+cy[i];
  46. if(a[tx][ty]=='.')
  47. {
  48. ans++;
  49. a[tx][ty]='#';
  50. Q.push(node{tx,ty});
  51. }
  52. }
  53. }
  54. }
  55.  
  56. int main()
  57. {
  58. //freopen("in.txt", "r", stdin);
  59. while(cin>>m>>n,n&&m)
  60. {
  61. mem(a,);
  62. for(int i=;i<=n;i++)
  63. {
  64. cin>>a[i]+;
  65. for(int j=;j<=m;j++)
  66. {
  67. if(a[i][j]=='@')
  68. {
  69. Q.push(node{i,j});
  70. a[i][j]='#';
  71. }
  72. }
  73. }
  74. ans=;
  75. bfs();
  76. cout<<ans<<endl;
  77. }
  78. return ;
  79. }

HDU 1312 Red and Black(bfs,dfs均可,个人倾向bfs)的更多相关文章

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

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

  2. 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 ...

  3. hdu 1312:Red and Black(DFS搜索,入门题)

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

  4. HDU 1312 Red and Black【DFS】

    搜索虐我千万遍@_@-----一道搜索的水题,WA了好多好多次@_@发现是n,m搞反了-_- 题意-- 给出m行 n列的矩形,其中从@出发,不能跳到#,只能跳到'.'问最多能够跳到多少块'.' 直接搜 ...

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

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

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

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

  7. HDU 1312 Red and Black DFS(深度优先搜索) 和 BFS(广度优先搜索)

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

  8. HDU 1312 Red and Black(经典DFS)

    嗯... 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1312 一道很经典的dfs,设置上下左右四个方向,读入时记下起点,然后跑dfs即可...最后答 ...

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

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

随机推荐

  1. linux 内核数据结构之 avl树.

    转载: http://blog.csdn.net/programmingring/article/details/37969745 https://zh.wikipedia.org/wiki/AVL% ...

  2. Executor框架(转)

    摘要:        Executor作为灵活且强大的异步执行框架,其支持多种不同类型的任务执行策略,提供了一种标准的方法将任务的提交过程和执行过程解耦开发,基于生产者-消费者模式,其提交任务的线程相 ...

  3. MySql 5.7关键字和保留字-附表

    现在使用navicat图形界面或者Hibernate做映射生成表的时候,渐渐的会忽视掉关键字这个问题,而后续也会不断的产生错误提示,一遍遍的查询代码无果,甚至开始怀疑人生,但是其实很多情况下只是使用了 ...

  4. javascript 与node的 event-loop

    参考:https://juejin.im/post/5c337ae06fb9a049bc4cd218 https://github.com/forthealllight/blog/issues/5 h ...

  5. 2019-11-29-C#-很少人知道的科技

    title author date CreateTime categories C# 很少人知道的科技 lindexi 2019-11-29 10:12:43 +0800 2018-03-16 08: ...

  6. 学习-Pytest(五)yield操作

    1.fixture的teardown操作并不是独立的函数,用yield关键字呼唤teardown操作 2.scope="module" 1.fixture参数scope=”modu ...

  7. windows pip使用国内源

    在这里我使用清华源来做示范 win+R 打开用户目录%HOMEPATH%,在此目录下创建 pip 文件夹,在 pip 目录下创建 pip.ini 文件, 内容如下, 在pip文件夹里新建的pip.in ...

  8. Hive的安装搭建(三)

    03 Hive的安装搭建 Hive可以从源码中编译安装,也可以直接使用官网下载的安装包,在此处我们选择安装包解压安装的方式. Hive中最最重要的角色就是metastore 因此按照metastore ...

  9. open函数的打开标志所在文件

    /usr/include/x86_64-linux-gnu/bits/fcntl-linux.h

  10. nginx代理证书使用方法

    一.证书购买 一般情况,申请证书时需要添加DNS解析,具体的步骤一般运营商都会给予详细说明.当然,也需要填写证书保护的处理的域名,一般非免费的证书可以支持多个域名处理,免费的一般只能支持一个域名的设置 ...