题目代号: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. windows编程 使用C++实现多线程类

    有时候我们想在一个类中实现多线程,主线程在某些时刻获得数据,可以“通知”子线程去处理,然后把结果返回.下面的实例是主线程每隔2s产生10个随机数,将这10随机数传给多线程类,让它接收到数据后马上打印出 ...

  2. 零基础学习前端1-1配置node及npm环境变量

    零基础学习前端1-1配置node及npm环境变量 ## 1-1配置node及npm环境变量 首先:下载node 可以直接去官方网站下载 1.首先从官网下载安装包 https://nodejs.org/ ...

  3. 教你用 Netty 实现一个简单的 RPC!

    众所周知,dubbo 底层使用了 Netty 作为网络通讯框架,而 Netty 的高性能我们之前也分析过源码,对他也算还是比较了解了. 今天我们就自己用 Netty 实现一个简单的 RPC 框架. 1 ...

  4. SpringBoot_01

    一.初识springboot 个人总结:springboot是一个开发更加便捷的spring的技术框架,通过引入启动器便可以快捷的让spring框架和其他框架进行整合, springboot很容易上手 ...

  5. OpenSSL使用小结

    引言 互联网的发展史上,安全性一直是开发者们相当重视的一个主题,为了实现数据传输安全,我们需要保证:数据来源(非伪造请求).数据完整性(没有被人修改过).数据私密性(密文,无法直接读取)等.虽然现在已 ...

  6. eclipse中web项目tomcat的设置

    1.  出现的问题: web开发中(eclipse环境),为本地项目添加tomcat,我们一般都会选择直接添加.在本次开发中突然遇到一个问题:因为项目涉及到文件上传,我利用MultipartFile进 ...

  7. vue学习【三】vue-router路由显示多页面

    大家好,我是一叶,今天是七夕,单身狗的我还在这里写踩坑文.在这里还是要祝大家早日脱单(能不能脱单自己心里没个数吗).本篇继续踩坑,在单页面上展示多页的内容,大家的想法是什么,估计大家第一印象会是ifr ...

  8. openlayers之框选放缩DragZoom(vue项目)

    环境vue3.0项目 最初是以npm i ol -s方式安装的ol,import方式导入引用,但是实际使用的时候一直报ol is not defined,最后选择在HTML以script标签引入ol, ...

  9. 如何正确训练一个 SVM + HOG 行人检测器

    这几个月一直在忙着做大论文,一个基于 SVM 的新的目标检测算法.为了做性能对比,我必须训练一个经典的 Dalal05 提出的行人检测器,我原以为这个任务很简单,但是我错了. 为了训练出一个性能达标的 ...

  10. Wannafly挑战赛22 D 整数序列 (线段树维护三角函数值)

    链接:https://ac.nowcoder.com/acm/contest/160/D 来源:牛客网 整数序列 时间限制:C/C++ 2秒,其他语言4秒 空间限制:C/C++ 262144K,其他语 ...