D - Red and Black

Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d
& %I64u

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

  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
 

这题已经不想在吐槽什么了,n,m 和正常的出相反 害我一些细节错 一直WA 还说到底还是自己太弱了

  1. #include<iostream>
  2. using namespace std;
  3.  
  4. #include<string.h>
  5. #define max 25
  6. char map[max][max];int mmin;
  7. long n,m,visited[max][max];
  8. long directions[4][2]={{0,-1},{0,1},{1,0},{-1,0}};
  9.  
  10. void DFS(int x,int y)
  11. {
  12. int i,mx,my;
  13. for(i=0;i<4;i++)
  14. {
  15. mx=x+directions[i][0];
  16. my=y+directions[i][1];
  17. if(mx>=0&&mx<m&&my>=0&&my<n)
  18. {
  19. if(!visited[mx][my]&&map[mx][my]=='.')
  20. {
  21. visited[mx][my]=1;
  22. mmin++;
  23. DFS(mx,my);
  24. }
  25. }
  26. }
  27. }
  28. int main()
  29. {
  30.  
  31. int i,j,a,b;
  32. while(cin>>n>>m)
  33. {
  34. if(n==0&&m==0)break;
  35. memset(visited,0,sizeof(visited));
  36.  
  37. for(i=0;i<m;i++)
  38. {
  39. for(j=0;j<n;j++)
  40. {
  41. cin>>map[i][j];
  42. if(map[i][j]=='@')
  43. {
  44. a=i,b=j;
  45. }
  46. }
  47. }
  48. mmin=1;
  49. visited[a][b]=1;
  50. DFS(a,b);
  51. cout<<mmin<<endl;
  52. }
  53. return 0;
  54. }

版权声明:本文为博主原创文章,未经博主允许不得转载。

DFS-HDU 1312 -Red and Black的更多相关文章

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

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

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

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

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

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

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

  5. HDU 1312 Red and Black(bfs,dfs均可,个人倾向bfs)

    题目代号:HDU 1312 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1312 Red and Black Time Limit: 2000/100 ...

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

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

  7. HDU 1312 Red and Black(DFS,板子题,详解,零基础教你代码实现DFS)

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

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

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

  9. HDU 1312 Red and Black (DFS & BFS)

    原题链接:http://acm.hdu.edu.cn/showproblem.php?pid=1312 题目大意:有一间矩形房屋,地上铺了红.黑两种颜色的方形瓷砖.你站在其中一块黑色的瓷砖上,只能向相 ...

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

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

随机推荐

  1. js生成有缩进的表格

    项目中用到用了两天时间想到的,记录下来,如有更好的方法,留言给我,谢谢! js做如下表格: json [{"id":302,"serviceId":15,&qu ...

  2. apache和IIS共享80端口问题

    使用apache代理功能和IIS共享80端口的解决办法. 第一步:把iis所发布的网站默认端口由80改为8080: 第二步:修改apache的httpd.conf配置文件.  首先,要让apache支 ...

  3. Dalvik opcodes

    原文地址: http://pallergabor.uw.hu/androidblog/dalvik_opcodes.html Dalvik opcodes Author: Gabor Paller V ...

  4. 关键字替换排除HTML标签属性字符

    解决办法: 1.打开文件e/class/functions.php 2.找到函数 ReplaceKey($newstext,$classid=0) 3.找到替换代码 if(STR_IREPLACE) ...

  5. Linux shell用法和技巧(转)

    原文出处: techbar   译文出处: 外刊IT评论 使用Linux shell是我每天的基本工作,但我经常会忘记一些有用的shell命令和技巧.当然,命令我能记住,但我不敢说能记得如何用它执行某 ...

  6. 小课堂week13 Clean Code Part2

    Clean Code Part2 对象与数据结构 首先让我们进行一个严肃的思考,对象与数据结构的区别在哪里? 如下两段代码分别用数据结构和对象的方法来描述了一个Point. public class ...

  7. Oracle 11g 执行计划管理2

    1.创建测试数据 SQL> conn NC50/NC50 Connected. SQL)); SQL> insert into tab1 select rownum,object_name ...

  8. Learning Scrapy笔记(零) - 前言

    我已经使用了scrapy有半年之多,但是却一直都感觉没有入门,网上关于scrapy的文章简直少得可怜,而官网上的文档(http://doc.scrapy.org/en/1.0/index.html)对 ...

  9. SRF之日志和异常

    日志: 日志功能采用log4net实现 log4配置文件在站点目录下的log4net.config. 调用log4写日志的代码如下: log4net.ILog logger = log4net.Log ...

  10. WPF实现渐变淡入淡出的动画效果

    1.实现原理 1.1 利用UIElement.OpacityMask属性,用于改变对象区域的不透明度的画笔.可以使元素的特定区域透明或部分透明,从而实现比较新颖的效果. 1.2 OpacityMask ...