POJ-1979 Red and Black(DFS)
题目链接: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)的更多相关文章
- POJ 1979 Red and Black (DFS)
Description There is a rectangular room, covered with square tiles. Each tile is colored either red ...
- POJ 1979 Red and Black (红与黑)
POJ 1979 Red and Black (红与黑) Time Limit: 1000MS Memory Limit: 30000K Description 题目描述 There is a ...
- poj 1979 Red and Black(dfs水题)
Description There is a rectangular room, covered with square tiles. Each tile is colored either red ...
- 【POJ - 3984】迷宫问题(dfs)
-->迷宫问题 Descriptions: 定义一个二维数组: int maze[5][5] = { 0, 1, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0 ...
- poj 3009 Curling 2.0 (dfs )
Curling 2.0 Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 11879 Accepted: 5028 Desc ...
- 【POJ - 1321】棋盘问题 (dfs)
棋盘问题 Descriptions: 在一个给定形状的棋盘(形状可能是不规则的)上面摆放棋子,棋子没有区别.要求摆放时任意的两个棋子不能放在棋盘中的同一行或者同一列,请编程求解对于给定形状和大小的棋盘 ...
- 【POJ - 1970】The Game(dfs)
-->The Game 直接中文 Descriptions: 判断五子棋棋局是否有胜者,有的话输出胜者的棋子类型,并且输出五个棋子中最左上的棋子坐标:没有胜者输出0.棋盘是这样的,如图 Samp ...
- 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 ...
- Poj1979 Red and Black (DFS)
Red and Black Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 47466 Accepted: 25523 D ...
随机推荐
- javascript 之拼接html字符串
// var one = {"id":1, "leasetime":2, "ney":0,"myhuifangshi": ...
- 性能分析之-- JAVA Thread Dump 分析综述
性能分析之-- JAVA Thread Dump 分析综述 一.Thread Dump介绍 1.1什么是Thread Dump? Thread Dump是非常有用的诊断Java应用问题的工 ...
- git 删除错误提交的commit
方法: 根据–soft –mixed –hard,会对working tree和index和HEAD进行重置: git reset --mixed:此为默认方式,不带任何参数的git reset ...
- How to Install Hadoop on Ubuntu
安装教程,https://www.digitalocean.com/community/tutorials/how-to-install-hadoop-on-ubuntu-13-10
- access中根据一个表创建另一个
access中根据一个表创建另一个 SELECT * INTO newTableFROM zD_qlr; SELECT * into mdFROM zd IN 'E:\fz\高阳\大姚\fz\bz\b ...
- 总结隐藏Ribbon菜单的方法
1. 重载 using (SPSite site = new SPSite("http://SP2010-01")) { using (SPWeb web = site.OpenW ...
- DownloadManager 的使用
一.基本概念 1.DownloadManager是Android 2.3A (API level 9) 引入的,基于http协议,用于处理长时间下载. 2.DownloadManager对于断点 ...
- App开发流程之通用宏定义及头文件
工欲善其事,必先利其器. 在正式实现各种炫酷的功能和UI前,做好准备工作是提高后续开发效率的必经之路. 所以,这个系列,我不是在各种堆技术,更关注的是“兵马动”之前的“粮草行”,有些繁琐,但当清晰理出 ...
- MyBatis入门(二)---一对一,一对多
一.创建数据库表 1.1.创建数据表同时插入数据 /* SQLyog Enterprise v12.09 (64 bit) MySQL - 5.6.27-log : Database - mybati ...
- SSH整合简述一
1.web.xml中配置 struts2过滤器 <filter> <filter-name>struts2</filter-name> <filter-cla ...