速刷一道DFS

Description

There is a rectangular room, covered with square tiles. Each tile is colored either red or black. A man is standing on a black tile. From a tile, he can move to one of four adjacent tiles. But he can't move on red tiles, he can move only on black tiles.

Write a program to count the number of black tiles which he can reach by repeating the moves described above.

@是起点,#不能走,'.'是正常道路,问从起点能到达多少格子

Input

The input consists of multiple data sets. A data set starts with a line containing two positive integers W and H; W and H are the numbers of tiles in the x- and y- directions, respectively. W and H are not more than 20.

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

For each data set, your program should output a line which contains the number of tiles he can reach from the initial tile (including itself).

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的更多相关文章

  1. POJ1979 Red and Black (简单DFS)

    POJ1979 Description There is a rectangular room, covered with square tiles. Each tile is colored eit ...

  2. Poj1979 Red and Black (DFS)

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

  3. poj1979 Red And Black(DFS)

    题目链接 http://poj.org/problem?id=1979 思路 floodfill问题,使用dfs解决 代码 #include <iostream> #include < ...

  4. poj-1979 red and black(搜索)

    Time limit1000 ms Memory limit30000 kB There is a rectangular room, covered with square tiles. Each ...

  5. POJ1979(Red and Black)--FloodFill

    题目在这里 题目意思是这样的,一个人起始位置在    '@'  处,他在途中能到达的地方为 ' .  '     而  '#' 是障碍物,他不能到达. 问途中他所有能到达的   '.'的数量是多少 ? ...

  6. POJ-1979 Red and Black(DFS)

    题目链接:http://poj.org/problem?id=1979 深度优先搜索非递归写法 #include <cstdio> #include <stack> using ...

  7. 《挑战程序设计竞赛》2.1 深度优先搜索 POJ2386 POJ1979 AOJ0118 AOJ0033 POJ3009

    POJ2386 Lake Counting Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 25366   Accepted: ...

  8. 图的遍历之深度优先搜索(DFS)

    深度优先搜索(depth-first search)是对先序遍历(preorder traversal)的推广.”深度优先搜索“,顾名思义就是尽可能深的搜索一个图.想象你是身处一个迷宫的入口,迷宫中的 ...

  9. 算法总结—深度优先搜索DFS

    深度优先搜索(DFS) 往往利用递归函数实现(隐式地使用栈). 深度优先从最开始的状态出发,遍历所有可以到达的状态.由此可以对所有的状态进行操作,或列举出所有的状态. 1.poj2386 Lake C ...

随机推荐

  1. android gravity属性 和 weight属性

    来看这个布局文件 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:and ...

  2. C语言 百炼成钢4

    //题目10:打印楼梯,同时在楼梯上方打印两个笑脸. #define _CRT_SECURE_NO_WARNINGS #include<stdio.h> #include<stdli ...

  3. use AP_VENDOR_PUB_PKG.Update_Vendor_Site_Public to u ORA-01722: invalid number in Package AP_VENDOR_PUB_PKG Procedure Update_Vendor_Site_Public

    ORA-01722: invalid number in Package AP_VENDOR_PUB_PKG Procedure Update_Vendor_Site_Public 发现此问题的经过: ...

  4. SpringMVC实现上传和下载

    摘要 有些下载的错误解决来 java.lang.IllegalStateException: getOutputStream() has already been called for this re ...

  5. Linux第七次实验笔记

    #期中总结 习题总结与分析 填空:Linux Bash中,Ctrl+a快捷键的作用是(将光标移至输入行头,相当于Home键). [ctrl]+u 从游标处向前删除指令串 [ctrl]+k 从游标处向后 ...

  6. Andriod Studio Clear Project或Rebuild Project出错

    以前在Eclipse中出现过类似的错误:在编译工程时,提示无法删除bin目录下的某个jar. 想不到Android Studio中也会有. Clear Project或Rebuild Project, ...

  7. Bootstrap3.0学习第十四轮(分页、徽章)

    详情请查看http://aehyok.com/Blog/Detail/21.html 个人网站地址:aehyok.com QQ 技术群号:206058845,验证码为:aehyok 本文文章链接:ht ...

  8. Nginx下Redmine配置

    安装redmine依赖的所有ruby包 cd .. gem install bundler #注意是在网站根目录下执行 bundle install --without development tes ...

  9. nginx root && alias 文件路径配置

    文章摘自:http://www.ttlsa.com/nginx/nginx-root_alias-file-path-configuration/ nginx指定文件路径有两种方式root和alias ...

  10. python 切片

    本人的博客中的python内容基本上全是看着廖雪峰大神博客做的一个笔记 关于列表或者元祖的切片 下面说一下列表的切片的语法: L[起始位置:结束位置:步长] number = range(100) n ...