Time limit1000 ms

Memory limit30000 kB

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.  
  6. 题意:红色和黑色的地砖,只能走黑色的地砖,问最多可以走几块地砖
    题解:dfs
  1. #include <iostream>
  2. #include <cstdio>
  3. #include <cstring>
  4. #include <cmath>
  5. #include <algorithm>
  6. #include <queue>
  7. #include <map>
  8. #include <set>
  9. #include <stack>
  10. #include <vector>
  11. #include <list>
  12. using namespace std;
  13. #define PI 3.14159265358979323846264338327950
  14. #define INF 0x3f3f3f3f3f3f3f3f;
  15.  
  16. char a[][];
  17. int vis[][];
  18. int m,n,st,en,sum;
  19.  
  20. void dfs(int x,int y)
  21. {
  22. a[x][y]='#';
  23. sum++;
  24. if(x->= && a[x-][y]=='.')
  25. dfs(x-,y);
  26. if(x+<n && a[x+][y]=='.')
  27. dfs(x+,y);
  28. if(y->= && a[x][y-]=='.')
  29. dfs(x,y-);
  30. if(y+<m && a[x][y+]=='.')
  31. dfs(x,y+);
  32. }
  33.  
  34. int main()
  35. {
  36. while(scanf("%d %d",&m,&n) && (m||n))
  37. {
  38. sum=;
  39. int i,j;
  40. memset(vis,,sizeof(vis));
  41. for( i=;i<n;i++)
  42. for(j=;j<m;j++)
  43. {
  44. cin>>a[i][j];
  45. if(a[i][j]=='@')
  46. {
  47. st=i;
  48. en=j;
  49. }
  50. }
  51. int x=st,y=en;
  52. a[x][y]='#';
  53. if(x->= && a[x-][y]=='.')
  54. dfs(x-,y);
  55. if(x+<n && a[x+][y]=='.')
  56. dfs(x+,y);
  57. if(y->= && a[x][y-]=='.')
  58. dfs(x,y-);
  59. if(y+<m && a[x][y+]=='.')
  60. dfs(x,y+);
  61. printf("%d\n",sum);
  62. }
  63. }

poj-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. POJ 1979 Red and Black 四方向棋盘搜索

    Red and Black Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 50913   Accepted: 27001 D ...

  3. OpenJudge/Poj 1979 Red and Black / OpenJudge 2816 红与黑

    1.链接地址: http://bailian.openjudge.cn/practice/1979 http://poj.org/problem?id=1979 2.题目: 总时间限制: 1000ms ...

  4. poj 1979 Red and Black(dfs)

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

  5. poj 1979 Red and Black 题解《挑战程序设计竞赛》

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

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

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

  7. POJ 1979 Red and Black (zoj 2165) DFS

    传送门: poj:http://poj.org/problem?id=1979 zoj:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problem ...

  8. HDOJ 1312 (POJ 1979) Red and Black

    Problem Description There is a rectangular room, covered with square tiles. Each tile is colored eit ...

  9. poj 1979 Red and Black(dfs水题)

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

  10. POJ 1979 Red and Black (DFS)

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

随机推荐

  1. NET Core中使用Redis和Memcached

    .NET Core中使用Redis和Memcached的序列化问题   前言 在使用分布式缓存的时候,都不可避免的要做这样一步操作,将数据序列化后再存储到缓存中去. 序列化这一操作,或许是显式的,或许 ...

  2. 使用Zeppelin时出现sh interpreter not found错误的解决办法(图文详解)

    不多说,直接上干货! 问题详解 http://192.168.80.145:8099/#/notebook/2CSV2VT5S 相关博客是 Zeppelin的入门使用系列之使用Zeppelin运行sh ...

  3. 树莓派连接启动SSH

    树莓派的官方更新消息发布:http://downloads.raspberrypi.org/raspbian/release_notes.txt SSH禁用的启用方法: 2016年11月25日: * ...

  4. asp.net mvc整合Nhibernate的配置方法

    http://blog.csdn.net/xz2001/article/details/8452794 http://www.cnblogs.com/GoodHelper/archive/2011/0 ...

  5. centos 离线安装 mysql 5.7

    1 . 安装新版mysql前,需将系统自带的mariadb-lib卸载. rpm -qa|grep mariadb mariadb-libs--.el7.centos.x86_64 rpm -e -- ...

  6. 一键部署Moodle开源课程管理系统

    产品详情 产品介绍Moodle https://moodle.org/ 是一个开源及自由的电子学习软件平台,亦称为课程管理系统.学习管理系统或虚拟学习环境.Moodle 特色异于其他商业线上教学平台, ...

  7. 卓越管理的秘密(Behind Closed Doors)

    或许提到本书甚至本书的作者Johanna Rothman我们会感到些许陌生,那么提起她的另一本获得素有软件界奥斯卡之称的Jolt生产效率大奖的名著<项目管理修炼之道>,会不会惊讶的发现,原 ...

  8. Java 集合框架_中

    Set接口 特点: [1]Set接口表示一个唯一.无序的容器(和添加顺序无关) Set接口常用实现类有 HashSet [1]HashSet是Set接口的实现类,底层数据结构是哈希表. [2]Hash ...

  9. Render渲染函数和JSX

    1.Render函数:render是用来替换temlate的,需要更灵活的模板的写法的时候,用render. 官网API地址:https://cn.vuejs.org/v2/guide/render- ...

  10. Maven:项目结构

    目录结构图: project        |- src            |- main   //工程源代码目录                |- java        //工程java源代 ...