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 ...
随机推荐
- 线段树或树状数组---Flowers
题目网址:http://acm.hdu.edu.cn/showproblem.php?pid=4325 Description As is known to all, the blooming tim ...
- isEmpty与null、""的区别
前一段时间我阅读别人的代码,发现有的时候用isEmpty,有的时候用null,有的时候用"".我很困惑三者之间的区别,于是我就自己写了一个程序来验证一下 public class ...
- mysql grant all on *.* to xxx@'%' 报Access denied for user 'root'@'localhost'
今日,开发反馈某台mysql服务器无法登陆,解决之后,远程登录后发现用户只能看到information_schema,其他均看不到. 故登录服务器执行: mysql> grant all on ...
- mybatis3批量更新 批量插入
在公司ERP项目开发中,遇到批量数据插入或者更新,因为每次连接数据库比较耗时,所以决定改为批量操作,提升效率.库存盘点导入时,需要大量数据批量操作. 1:数据库连接代码中必须开启批量操作.加上这句,& ...
- JS中检测数据类型的四种方法
1.typeof 用来检测数据类型的运算符->typeof value->返回值首先是一个字符串,其次里面包含了对应的数据类型,例如:"number"."st ...
- 如何解决div层被flash遮盖的问题
页面构建中的Flash层会遮挡Div的问题,一般通过设置wmode="transparent" 或wmode="window"就可以解决.不过对于Flash视频 ...
- ASP.NET数据绑定技术
1.DataBinder.Eval()方法 DataBinder.Eval()方法是ASP.NET框架支持的一个静态方法,用来计算Late_Bound(后期绑定)数据绑定表达式,并随时将结果转换为字符 ...
- SharePoint 2013 术语和术语集介绍
托管元数据是一个集中管理的术语的分层集合,我们可以定义术语和术语集,然后将其用作 SharePoint Server 2013 中项目的属性.简单的说,术语是一个可与 SharePoint Serve ...
- Oracle数据库中创建表空间语句
1:创建临时表空间 create temporary tablespace user_temp tempfile 'Q:\oracle\product\10.2.0\oradata\Test\xyrj ...
- 挣值管理(PV、EV、AC、SV、CV、SPI、CPI)记忆之我见
挣值管理(PV.EV.AC.SV.CV.SPI.CPI)记忆之我见 挣值管理法中的PV.EV.AC.SV.CV.SPI.CPI这些英文简写相信把大家都搞得晕头转向的.在挣值管理法中,需要记忆理解的有三 ...