POJ1979 Red and Black
速刷一道DFS
Description
Write a program to count the number of black tiles which he can reach by repeating the moves described above.
Input
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)
The end of the input is indicated by a line consisting of two zeros.
Output
Sample Input
6 9
....#.
.....#
......
......
......
......
......
#@...#
.#..#.
11 9
.#.........
.#.#######.
.#.#.....#.
.#.#.###.#.
.#.#..@#.#.
.#.#####.#.
.#.......#.
.#########.
...........
11 6
..#..#..#..
..#..#..#..
..#..#..###
..#..#..#@.
..#..#..#..
..#..#..#..
7 7
..#.#..
..#.#..
###.###
...@...
###.###
..#.#..
..#.#..
0 0
Sample Output
45
59
6
13
Source
刷道普通的DFS,复习模板
/*by SilverN*/
#include<iostream>
#include<algorithm>
#include<cstring>
#include<cstdio>
#include<cmath>
using namespace std;
const int mxn=;
char mp[mxn][mxn];
int ans;
int W,H;
void dfs(int x,int y){
if(x< || x>H || y< || y>W)return;
if(mp[x][y]=='#')return;
ans++;
mp[x][y]='#';
dfs(x+,y);
dfs(x,y+);
dfs(x-,y);
dfs(x,y-);
return;
}
int main(){
while(scanf("%d%d",&W,&H) && W && H){
memset(mp,' ',sizeof(mp));
ans=;
int i,j;
for(i=;i<=H;i++){
scanf("%s",mp[i]+);
}
int sx,sy;
bool flag=;
for(i=;i<=H;i++){
for(j=;j<=W;j++)
if(mp[i][j]=='@'){
sx=i;sy=j;
flag=;
break;
}
if(flag)break;
}
dfs(sx,sy);
printf("%d\n",ans);
}
return ;
}
POJ1979 Red and Black的更多相关文章
- POJ1979 Red and Black (简单DFS)
POJ1979 Description There is a rectangular room, covered with square tiles. Each tile is colored eit ...
- Poj1979 Red and Black (DFS)
Red and Black Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 47466 Accepted: 25523 D ...
- poj1979 Red And Black(DFS)
题目链接 http://poj.org/problem?id=1979 思路 floodfill问题,使用dfs解决 代码 #include <iostream> #include < ...
- poj-1979 red and black(搜索)
Time limit1000 ms Memory limit30000 kB There is a rectangular room, covered with square tiles. Each ...
- POJ1979(Red and Black)--FloodFill
题目在这里 题目意思是这样的,一个人起始位置在 '@' 处,他在途中能到达的地方为 ' . ' 而 '#' 是障碍物,他不能到达. 问途中他所有能到达的 '.'的数量是多少 ? ...
- POJ-1979 Red and Black(DFS)
题目链接:http://poj.org/problem?id=1979 深度优先搜索非递归写法 #include <cstdio> #include <stack> using ...
- 《挑战程序设计竞赛》2.1 深度优先搜索 POJ2386 POJ1979 AOJ0118 AOJ0033 POJ3009
POJ2386 Lake Counting Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 25366 Accepted: ...
- 图的遍历之深度优先搜索(DFS)
深度优先搜索(depth-first search)是对先序遍历(preorder traversal)的推广.”深度优先搜索“,顾名思义就是尽可能深的搜索一个图.想象你是身处一个迷宫的入口,迷宫中的 ...
- 算法总结—深度优先搜索DFS
深度优先搜索(DFS) 往往利用递归函数实现(隐式地使用栈). 深度优先从最开始的状态出发,遍历所有可以到达的状态.由此可以对所有的状态进行操作,或列举出所有的状态. 1.poj2386 Lake C ...
随机推荐
- pl/sql developer中的SQL语句
1.无论是在查询还是在插入的时候,值都必须是单引号‘’,否则会报错,ORA-00904:标识符无效.
- velocity .vm
关于.vm 后缀的文件,是velocity的文件.velocity是基于java的一种页面模板引擎,支持#if #else #foreach等写法的前台文件.$link.contextPath是该引擎 ...
- C# 小型资源管理器
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...
- 【C#】实现按Windows排序方式排序
很多人或许都遇到过类似的问题,C#读取的文件列表默认是按ASCII排序的,这样会出现一个问题就是10会排在2的前面. 那么是否可以解决呢,答案是肯定的.虽然这个是很早之前遇到的问题,这次突然想起来,就 ...
- Eclipse中10个最有用的快捷键组合(转)
Eclipse中10个最有用的快捷键组合 1. ctrl+shift+r:打开资源 这可能是所有快捷键组合中最省时间的了.这组快捷键可以让你打开你的工作区中任何一个文件,而你只需要按下文件名或mask ...
- Jdev Run Page 没有反应
从旧电脑把原有的Jdeveloper完整的拷贝至新电脑,且已完整配置JDEV_USER_HOME,JAVA_HOME等环境变量, Run Page报以下错误. [Starting OC4J using ...
- python数字图像处理(8):对比度与亮度调整
图像亮度与对比度的调整,是放在skimage包的exposure模块里面 1.gamma调整 原理:I=Ig 对原图像的像素,进行幂运算,得到新的像素值.公式中的g就是gamma值. 如果gamma& ...
- Linux(12.1-12.6)学习笔记
第十二章 并发编程 如果逻辑控制流在时间上重叠,那么他们就是并发的.应用级并发在以下情况中发挥作用: 访问慢速I/O设备. 与人交互. 通过推迟工作以降低延迟. 服务多个网络客户端. 在多核机器上进行 ...
- 高校手机签到系统——Ksoap2的一些使用心得(补充)
高校手机签到系统系列: 高校手机签到系统——第一部分Authority权限系统(上) 高校手机签到系统——第一部分Authority权限系统(下) 高校手机签到系统——手机客户端 高校手机签到系统—— ...
- Object C学习笔记19-枚举
一. 枚举类型 枚举类型是一个基本类型,不能再分为为任何其他的类型.在一般的编程语言中都有枚举(enum)这种数据结构类型.枚举类型主要用于将一个变量限定在特定的范围内.比如一周有七天,那么一周的值就 ...