题目链接:http://poj.org/problem?id=1979

深度优先搜索非递归写法

  1. #include <cstdio>
  2. #include <stack>
  3.  
  4. using namespace std;
  5. const int MAX_W = , MAX_H = ;
  6. char Map[MAX_W][MAX_H+];
  7. int W, H;
  8.  
  9. int DFS(int sx, int sy);
  10.  
  11. int main()
  12. {
  13. while (scanf("%d %d", &H, &W) ==
  14. && W != && H != ) {
  15. for (int i = ; i < W; i++)
  16. scanf("%s", Map[i]);
  17. for (int i = ; i < W; i++)
  18. for (int j = ; j < H; j++) {
  19. if (Map[i][j] == '@')
  20. printf("%d\n", DFS(i, j));
  21. }
  22. }
  23. return ;
  24. }
  25.  
  26. int DFS(int sx, int sy)
  27. {
  28. int dx[] = {, , -, }, dy[] = {, , , -};
  29. int Visited[MAX_W][MAX_H] = {};
  30. typedef pair<int, int> Position;
  31. stack<Position> sta;
  32.  
  33. Visited[sx][sy] = ;
  34. int num = ;
  35. Map[sx][sy] = '.';
  36. sta.push(Position(sx, sy));
  37.  
  38. while (!sta.empty()) {
  39. Position p = sta.top(); sta.pop();
  40. for (int i = ; i < ; i++) {
  41. int nx = p.first + dx[i], ny = p.second + dy[i];
  42. if ( <= nx && nx < W && <= ny && ny < H &&
  43. Map[nx][ny] == '.' && !Visited[nx][ny]) {
  44. sta.push(Position(nx, ny));
  45. Visited[nx][ny] = ;
  46. num++;
  47. }
  48. }
  49. }
  50. return num;
  51. }

POJ-1979 Red and Black(DFS)的更多相关文章

  1. POJ 1979 Red and Black (DFS)

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

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

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

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

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

  4. 【POJ - 3984】迷宫问题(dfs)

    -->迷宫问题 Descriptions: 定义一个二维数组: int maze[5][5] = { 0, 1, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0 ...

  5. poj 3009 Curling 2.0 (dfs )

    Curling 2.0 Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 11879   Accepted: 5028 Desc ...

  6. 【POJ - 1321】棋盘问题 (dfs)

    棋盘问题 Descriptions: 在一个给定形状的棋盘(形状可能是不规则的)上面摆放棋子,棋子没有区别.要求摆放时任意的两个棋子不能放在棋盘中的同一行或者同一列,请编程求解对于给定形状和大小的棋盘 ...

  7. 【POJ - 1970】The Game(dfs)

    -->The Game 直接中文 Descriptions: 判断五子棋棋局是否有胜者,有的话输出胜者的棋子类型,并且输出五个棋子中最左上的棋子坐标:没有胜者输出0.棋盘是这样的,如图 Samp ...

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

  9. Poj1979 Red and Black (DFS)

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

随机推荐

  1. 线段树或树状数组---Flowers

    题目网址:http://acm.hdu.edu.cn/showproblem.php?pid=4325 Description As is known to all, the blooming tim ...

  2. isEmpty与null、""的区别

    前一段时间我阅读别人的代码,发现有的时候用isEmpty,有的时候用null,有的时候用"".我很困惑三者之间的区别,于是我就自己写了一个程序来验证一下 public class ...

  3. mysql grant all on *.* to xxx@'%' 报Access denied for user 'root'@'localhost'

    今日,开发反馈某台mysql服务器无法登陆,解决之后,远程登录后发现用户只能看到information_schema,其他均看不到. 故登录服务器执行: mysql> grant all on ...

  4. mybatis3批量更新 批量插入

    在公司ERP项目开发中,遇到批量数据插入或者更新,因为每次连接数据库比较耗时,所以决定改为批量操作,提升效率.库存盘点导入时,需要大量数据批量操作. 1:数据库连接代码中必须开启批量操作.加上这句,& ...

  5. JS中检测数据类型的四种方法

    1.typeof 用来检测数据类型的运算符->typeof value->返回值首先是一个字符串,其次里面包含了对应的数据类型,例如:"number"."st ...

  6. 如何解决div层被flash遮盖的问题

    页面构建中的Flash层会遮挡Div的问题,一般通过设置wmode="transparent" 或wmode="window"就可以解决.不过对于Flash视频 ...

  7. ASP.NET数据绑定技术

    1.DataBinder.Eval()方法 DataBinder.Eval()方法是ASP.NET框架支持的一个静态方法,用来计算Late_Bound(后期绑定)数据绑定表达式,并随时将结果转换为字符 ...

  8. SharePoint 2013 术语和术语集介绍

    托管元数据是一个集中管理的术语的分层集合,我们可以定义术语和术语集,然后将其用作 SharePoint Server 2013 中项目的属性.简单的说,术语是一个可与 SharePoint Serve ...

  9. Oracle数据库中创建表空间语句

    1:创建临时表空间 create temporary tablespace user_temp tempfile 'Q:\oracle\product\10.2.0\oradata\Test\xyrj ...

  10. 挣值管理(PV、EV、AC、SV、CV、SPI、CPI)记忆之我见

    挣值管理(PV.EV.AC.SV.CV.SPI.CPI)记忆之我见 挣值管理法中的PV.EV.AC.SV.CV.SPI.CPI这些英文简写相信把大家都搞得晕头转向的.在挣值管理法中,需要记忆理解的有三 ...