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

深度优先搜索非递归写法

#include <cstdio>
#include <stack> using namespace std;
const int MAX_W = , MAX_H = ;
char Map[MAX_W][MAX_H+];
int W, H; int DFS(int sx, int sy); int main()
{
while (scanf("%d %d", &H, &W) ==
&& W != && H != ) {
for (int i = ; i < W; i++)
scanf("%s", Map[i]);
for (int i = ; i < W; i++)
for (int j = ; j < H; j++) {
if (Map[i][j] == '@')
printf("%d\n", DFS(i, j));
}
}
return ;
} int DFS(int sx, int sy)
{
int dx[] = {, , -, }, dy[] = {, , , -};
int Visited[MAX_W][MAX_H] = {};
typedef pair<int, int> Position;
stack<Position> sta; Visited[sx][sy] = ;
int num = ;
Map[sx][sy] = '.';
sta.push(Position(sx, sy)); while (!sta.empty()) {
Position p = sta.top(); sta.pop();
for (int i = ; i < ; i++) {
int nx = p.first + dx[i], ny = p.second + dy[i];
if ( <= nx && nx < W && <= ny && ny < H &&
Map[nx][ny] == '.' && !Visited[nx][ny]) {
sta.push(Position(nx, ny));
Visited[nx][ny] = ;
num++;
}
}
}
return num;
}

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. 不是语言之争--Go vs Erlang

    因为 云巴 系统对高并发.低延迟的需求,我们对各个语言.平台做了很多的调研比较工作.这自然就包括致力于开发高并发应用的 Go 和 Erlang. 并发 Go 对高并发的支持通过 goroutine 实 ...

  2. PhpStorm的open in browser怎么修改端口和相对路径

    昨天下班后,在电脑安装phpstorm.xampp安装正常,但是在phpstorm上直接打开网站文件一直报错,一直报错502.我感觉好奇快,怎么会报错呢.后面我用hbuild打开文件,在浏览器显示正常 ...

  3. Ahjesus获取自定义属性Attribute或属性的名称

    1:设置自己的自定义属性 public class NameAttribute:Attribute { private string _description; public NameAttribut ...

  4. nginx服务器是怎么执行php脚本的?

    简单的说: fastCGI是nginx和php之间的一个通信接口,该接口实际处理过程通过启动php-fpm进程来解 析php脚本,即php-fpm相 当于一个动态应用服务器,从而实现nginx动态解析 ...

  5. [vim] vim入门

    1. 概述 工欲善其事 必先利其器.vim是非常好用的文本编辑器,可以将它看作是vi的进阶.绝大多数Unix系统都会内置vi编辑器,vi是文本编辑器,vim是程序编辑器.相比vi,它可以根据文件的类型 ...

  6. ArcEngine尝试读取或写入受保护的内存

    先说一下我的开发环境: Win10 + ArcGIS10.0 + ArcEngine10.0 + Framework4.0 今天调用新的GP工具则出现"尝试读取或写入受保护的内存.这通常指示 ...

  7. 高精度练习(hdoj1042)

    Problem Description Given an integer N(0 ≤ N ≤ 10000), your task is to calculate N!   Input One N in ...

  8. iOS--九宫格奥秘(UIView)(arc4random)

    下面程序主要实现,九宫格的模型,每个小UIView的颜色随机产生的,用到了 arc4random函数: ; ; ; ; ; i<; i++) { ; j<; j++) { UIView * ...

  9. CocoaPods的使用(图文并茂)OS X 10.11 系统

    系统:OS X EI Capitan 版本:10.11.2 开发工具:XCode:7.2 要使用CocoaPods,那么就需要先安装哦,你安装了么?如果没安装那就请阅读我的前篇<OS X 10. ...

  10. System占用端口80

    可尝试如下结束System进程: 开始——设置——控制面板——管理工具——服务结束系统服务项:World Wide Web Publishing Service