Red and Black
Time Limit: 1000MS   Memory Limit: 30000K
Total Submissions: 47466   Accepted: 25523

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

 
神坑的是:xy方向需要换一下
 
  1. #include <iostream>
  2. #include <algorithm>
  3. #include <cmath>
  4. using namespace std;
  5. char a[][];
  6. int n,m;
  7. int res=;
  8. int dx[]={,-,,};
  9. int dy[]={,,,-};
  10. void dfs(int x,int y)
  11. {
  12. res++;
  13. a[x][y]='#';
  14. for(int i=;i<;i++){
  15. int nx=x+dx[i],ny=y+dy[i];
  16. if(nx>=&&nx<n&&ny>=&&ny<m&&a[nx][ny]=='.'){
  17. dfs(nx,ny);
  18. }
  19. }
  20. return ;
  21. }
  22. void solve()
  23. {
  24. for(int i=;i<n;i++){
  25. for(int j=;j<m;j++){
  26. if(a[i][j]=='@'){
  27. dfs(i,j);
  28. }
  29. }
  30. }
  31. }
  32. int main()
  33. {
  34. while(cin>>m>>n&&n!=&&m!=){
  35.  
  36. res=;
  37. for(int i=;i<n;i++){
  38. for(int j=;j<m;j++){
  39. cin>>a[i][j];
  40. }
  41. }
  42. solve();
  43. cout<<res<<endl;
  44. }
  45. return ;
  46. }

脱离参考书自己再根据自己的理解过一遍:

  1. #include <iostream>
  2. #include <algorithm>
  3. #include <cmath>
  4. #include <string>
  5. #include <cstring>
  6. using namespace std;
  7. int n,m;
  8. char a[][];
  9. int sx,sy,nx,ny;
  10. int dx[]={,,,-};
  11. int dy[]={,-,,};
  12. int res;
  13. void dfs(int x,int y)
  14. {
  15. res++;
  16. a[x][y]='#';
  17. for(int i=;i<;i++){
  18. nx=x+dx[i],ny=y+dy[i];
  19. if(nx>=&&nx<m&&ny>=&&ny<n&&a[nx][ny]=='.'){
  20. dfs(nx,ny);
  21. }
  22. }
  23. }
  24. int main()
  25. {
  26. while(cin>>n>>m&&(n&&m)){
  27. for(int i=;i<m;i++){
  28. for(int j=;j<n;j++){
  29. cin>>a[i][j];
  30. if(a[i][j]=='@'){
  31. sx=i,sy=j;
  32. }
  33. }
  34. }
  35. res=;
  36. dfs(sx,sy);
  37. cout<<res<<endl;
  38. }
  39. return ;
  40. }

Poj1979 Red and Black (DFS)的更多相关文章

  1. POJ-1979 Red and Black(DFS)

    题目链接:http://poj.org/problem?id=1979 深度优先搜索非递归写法 #include <cstdio> #include <stack> using ...

  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. HDU1312 Red and Black(DFS) 2016-07-24 13:49 64人阅读 评论(0) 收藏

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

  4. poj-1979 red and black(搜索)

    Time limit1000 ms Memory limit30000 kB There is a rectangular room, covered with square tiles. Each ...

  5. Red and Black---hdu1312(dfs)

    2015-04-07http://acm.hdu.edu.cn/showproblem.php?pid=1312 Sample Input 6 9....#......#............... ...

  6. POJ 1979 Red and Black (DFS)

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

  7. HDU1312 / POJ1979 / ZOJ2165 Red and Black(红与黑) 解题报告

    题目链接:pid=1312" target="_blank">HDU1312 / POJ1979 / ZOJ2165 Red and Black(红与黑) Red ...

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

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

  9. LeetCode Subsets II (DFS)

    题意: 给一个集合,有n个可能相同的元素,求出所有的子集(包括空集,但是不能重复). 思路: 看这个就差不多了.LEETCODE SUBSETS (DFS) class Solution { publ ...

随机推荐

  1. JS 使用const声明常量的本质(很多人都有误解)

    在我们使用const声明常量时,总认为值一旦声明就不可改变,其实是有误解的: 刚在看ES6标准文档时,仔细阅读了const的解析,恍然大悟的感觉,分享给大家. 本质 const实际上保证的,并不是变量 ...

  2. CSS设置浏览器滚动条样式

    /*定义滚动条高宽及背景 高宽分别对应横竖滚动条的尺寸*/ ::-webkit-scrollbar { width: 5px; height: 110px; background-color: #F5 ...

  3. vs2013在使用ef6时,创建模型向导过程中,四种模型方式缺少2种

    下载eftool,并安装 https://download.microsoft.com/download/2/C/F/2CF7AFAB-4068-4DAB-88C6-CEFD770FAECD/EFTo ...

  4. 守护线程daemon

    如下代码: from threading import Thread import time def func1(n): time.sleep(10) print(n) t = Thread(targ ...

  5. 小程序里let和var以及const区别

    在JavaScript中有三种声明变量的方式:var.let.const. var:声明全局变量,换句话理解就是,声明在for循环中的变量,跳出for循环同样可以使用. [JavaScript] 纯文 ...

  6. js的简单介绍

    1.js的介绍 js全称叫javascript,但不是java,他是一门前台语言,而java是后台语言. js的作者是布兰登艾奇. 前台语言:运行在客户端的 后台语言:跟数据库有关的. 2.能干什么? ...

  7. 2018今日头条杯 E-Jump a Jump

    Problem E. Jump A JumpInput file: standard inputOutput file: standard outputTime limit: 1 secondsMemor ...

  8. H5的缓存 manifest

    H5里面的App Cache是由开发Web页面的开发者控制的,而不是由Native去控制的,但是Native里面的WebView也需要我们做一下设置才能支持H5的这个特性. 1.工作原理 写Web页面 ...

  9. Python字符串拼接的6种方法(转)

    add by zhj: 对于多行字符串连接,第6种连接方法很方便,连接时不会添加额外的空格. 原文:http://www.cnblogs.com/bigtreei/p/7892113.html 1. ...

  10. [特征工程]-------使用sklearn做单机特征工程[转载]

    https://www.cnblogs.com/jasonfreak/p/5448385.html 使用sklearn做单机特征工程 目录 1 特征工程是什么?2 数据预处理 2.1 无量纲化 2.1 ...