Red and Black
Time Limit: 1000MS   Memory Limit: 30000K
Total Submissions: 27891   Accepted: 15142

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) 
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

Source

  1. #include <cstdio>
  2. #include <algorithm>
  3. #include <iostream>
  4. #include <cstring>
  5. #include <queue>
  6. using namespace std;
  7. char map[][];
  8. bool vis[][];
  9. int dx[]={,,,-},dy[]={,-,,};
  10. int w,h;
  11. int sx,sy;
  12. struct node
  13. {
  14. int x,y;
  15. };
  16. int bfs()
  17. {
  18. queue<node> q;
  19. int ans=;
  20. memset(vis,,sizeof(vis));
  21. node s,temp;
  22. s.x=sx,s.y=sy;
  23. q.push(s);
  24. while(!q.empty())
  25. {
  26. s=q.front();
  27. q.pop();
  28. ans++;
  29. for(int i=;i<;i++)
  30. {
  31. temp.x=s.x+dx[i];
  32. temp.y=s.y+dy[i];
  33. if(temp.x>=&&temp.x<h&&temp.y>=&&temp.y<w&&vis[temp.x][temp.y]==&&map[temp.x][temp.y]=='.')
  34. {
  35.  
  36. vis[temp.x][temp.y]=;
  37. q.push(temp);
  38.  
  39. }
  40. }
  41. }
  42. return ans;
  43. }
  44. int main()
  45. {
  46. int i,j;
  47. freopen("in.txt","r",stdin);
  48. while(scanf("%d%d",&w,&h))
  49. {
  50. if(w==&&h==)
  51. break;
  52. for(i=;i<h;i++)
  53. scanf("%s",map[i]);
  54. for(i=;i<h;i++)
  55. for(j=;j<w;j++)
  56. if(map[i][j]=='@')
  57. {
  58. sx=i,sy=j;
  59. break;
  60. }
  61.  
  62. cout<<bfs()<<endl;
  63. }
  64. return ;
  65. }

Red and Black(poj 1979 bfs)的更多相关文章

  1. DFS:Red and Black(POJ 1979)

    红与黑 题目大意:一个人在一个矩形的房子里,可以走黑色区域,不可以走红色区域,从某一个点出发,他最多能走到多少个房间? 不多说,DFS深搜即可,水题 注意一下不要把行和列搞错就好了,我就是那样弄错过一 ...

  2. POJ 1979 Red and Black (红与黑)

    POJ 1979 Red and Black (红与黑) Time Limit: 1000MS    Memory Limit: 30000K Description 题目描述 There is a ...

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

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

  4. POJ 1979 Red and Black (BFS)

    链接 : Here! 思路 : 简单的搜索, 直接广搜就ok了. /****************************************************************** ...

  5. POJ 1979 dfs和bfs两种解法

      fengyun@fengyun-server:~/learn/acm/poj$ cat 1979.cpp #include<cstdio> #include<iostream&g ...

  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. poj 1979 Red and Black 题解《挑战程序设计竞赛》

    地址 http://poj.org/problem?id=1979 Description There is a rectangular room, covered with square tiles ...

  8. POJ 1979 Red and Black dfs 难度:0

    http://poj.org/problem?id=1979 #include <cstdio> #include <cstring> using namespace std; ...

  9. poj 1979 Red and Black(dfs)

    题目链接:http://poj.org/problem?id=1979 思路分析:使用DFS解决,与迷宫问题相似:迷宫由于搜索方向只往左或右一个方向,往上或下一个方向,不会出现重复搜索: 在该问题中往 ...

随机推荐

  1. Go and JSON

    Go and JSON 在使用Go开发web项目的过程中, 数据库读写操作与JSON格式的输入输出是两块最基础的模块, Go的标准库已经帮我们做了很多, 熟悉database/sql与encoding ...

  2. [spoj104][Highways] (生成树计数+矩阵树定理+高斯消元)

    In some countries building highways takes a lot of time... Maybe that's because there are many possi ...

  3. CoreData (三)备

    NSFetchedResultsController 什么是NSFetchedResultsController NSFetchedResultsController是一个让人爱恨交加的一个类.如果使 ...

  4. 关于NGINX变量的一些测试结果

    作为NGINX变量,不像正规语言那么正式. 但处理自定义和内部变量时,这些操作,也是少不了的. geo $dollar { default "$"; } server { list ...

  5. 下一代云计算模式:Docker正掀起个性化商业革命

    作者: 吴宁川  来源: ITValue  发布时间: 2015-09-20 10:41  阅读: 10008 次  推荐: 16   原文链接   [收藏] 文/ITValue 记者吴宁川 从 20 ...

  6. spring 4 泛型注入

    最近对系统进行改造,发现在泛型实例初始化的时候,得不到想要的泛型.或者需要强制转换. spring 4 开始支持泛型对象初始化,初始化方法如下: 注:使用配置文件的方法暂时还没有发现,下面是使用jav ...

  7. 比较有用的log4j.properties

    转自   http://walsh.iteye.com/blog/314941 log4j.properties log4j.rootLogger=DEBUG,CONSOLE,DATABASE,FIL ...

  8. SQL生成随机字符串

    1.SQLserve生成随机字符串 SELECT replace(newid(), '-', '')

  9. Android使用bindService启动服务

    1.Service package com.example.ebobo; import java.util.Timer; import java.util.TimerTask; import andr ...

  10. Linux usb子系统(三):通过usbfs操作设备的用户空间驱动

    内核中提供了USB设备文件系统(usbdevfs,Linux 2.6改为usbfs,即USB文件系统),它和/proc类似,都是动态产生的.通过在/etc/fstab文件中添加如下一行:none /p ...