Red and Black

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
 #include <iostream>
#include <cstring>
using namespace std;
int n, m;
int num;
char map[][];
bool vis[][];
int dir[][] = {,,,,-,,,-}; void dfs(int x, int y){
for(int i = ; i < ; i++){
int xx = x + dir[i][];
int yy = y + dir[i][];
if(xx >= && xx <= m && yy >= && yy <= n && vis[xx][yy] == && map[xx][yy] == '.'){
vis[xx][yy] = ;
num++;
dfs(xx,yy);
}
}
}
int main(){
while(cin >> n >> m){
if(n == && m == )
break;
int x, y;
memset(vis,,sizeof(vis));
for(int i = ; i <= m; i++){
for(int j = ; j <= n; j++){
cin >> map[i][j];
if(map[i][j] == '@')
x = i, y = j;
}
}
num = ;
vis[x][y] = ;
dfs(x,y);
cout << num << endl;
}
return ;
}
												

poj_1979(dfs)的更多相关文章

  1. BZOJ 3083: 遥远的国度 [树链剖分 DFS序 LCA]

    3083: 遥远的国度 Time Limit: 10 Sec  Memory Limit: 1280 MBSubmit: 3127  Solved: 795[Submit][Status][Discu ...

  2. BZOJ 1103: [POI2007]大都市meg [DFS序 树状数组]

    1103: [POI2007]大都市meg Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 2221  Solved: 1179[Submit][Sta ...

  3. BZOJ 4196: [Noi2015]软件包管理器 [树链剖分 DFS序]

    4196: [Noi2015]软件包管理器 Time Limit: 10 Sec  Memory Limit: 512 MBSubmit: 1352  Solved: 780[Submit][Stat ...

  4. 图的遍历(搜索)算法(深度优先算法DFS和广度优先算法BFS)

    图的遍历的定义: 从图的某个顶点出发访问遍图中所有顶点,且每个顶点仅被访问一次.(连通图与非连通图) 深度优先遍历(DFS): 1.访问指定的起始顶点: 2.若当前访问的顶点的邻接顶点有未被访问的,则 ...

  5. BZOJ 2434: [Noi2011]阿狸的打字机 [AC自动机 Fail树 树状数组 DFS序]

    2434: [Noi2011]阿狸的打字机 Time Limit: 10 Sec  Memory Limit: 256 MBSubmit: 2545  Solved: 1419[Submit][Sta ...

  6. POJ_2386 Lake Counting (dfs 错了一个负号找了一上午)

    来之不易的2017第一发ac http://poj.org/problem?id=2386 Lake Counting Time Limit: 1000MS   Memory Limit: 65536 ...

  7. 深度优先搜索(DFS)

    [算法入门] 郭志伟@SYSU:raphealguo(at)qq.com 2012/05/12 1.前言 深度优先搜索(缩写DFS)有点类似广度优先搜索,也是对一个连通图进行遍历的算法.它的思想是从一 ...

  8. 【BZOJ-3779】重组病毒 LinkCutTree + 线段树 + DFS序

    3779: 重组病毒 Time Limit: 20 Sec  Memory Limit: 512 MBSubmit: 224  Solved: 95[Submit][Status][Discuss] ...

  9. 【BZOJ-1146】网络管理Network DFS序 + 带修主席树

    1146: [CTSC2008]网络管理Network Time Limit: 50 Sec  Memory Limit: 162 MBSubmit: 3495  Solved: 1032[Submi ...

随机推荐

  1. Asp.Net WebApi 学习记录(一)

    刚创建的 Asp.Net Web Api 项目,在进行简单的测试时发现返回的 JSON 数据很丑陋.与平时我们使用的 JSON.NET 序列化出来的字符串不一样.通过下面的设置就可以了: // 清除所 ...

  2. 3D 模型

    http://www.imooc.com/article/12670

  3. 多线程 ThreadLocal

    要了解ThreadLocal,首先搞清楚ThreadLocal 是什么?是用来解决什么问题的? ThreadLocal 是线程的局部变量, 是每一个线程所单独持有的,其他线程不能对其进行访问, 通常是 ...

  4. selector的例子

    例子1 文件名:sl_rounded_rectangle_button_picture.xml 文件内容: <?xml version="1.0" encoding=&quo ...

  5. springboot logback

    /resources/logback-spring.xml <configuration> <appender name="stdout" class=" ...

  6. Centos 7 下 LAMP 部署

    一.介绍 LAMP is a combination of operating system and open-source software stack. The acronym of LAMP i ...

  7. 查看linux发行版版本

    可以通过查看/etc/issue文件查看发行版版本号,不论系统是redhat\suse\debian

  8. wiper

    wiper - 必应词典 美['waɪpər]英['waɪpə(r)] n.手绢儿:搌布:揩布:(汽车风挡上的)雨刷器 网络刮水器:雨刮器:拭雨器 变形复数:wipers:

  9. synchronized细节问题(一)

    synchronized锁重入: 关键字synchronized拥有锁重入的功能,也就是在使用synchronized时,当一个线程得到一个对象的锁后,再次请求此对象时是可以再次得到该对象的锁. 下面 ...

  10. TZOJ 3295 括号序列(区间DP)

    描述 给定一串字符串,只由 “[”.“]” .“(”.“)”四个字符构成.现在让你尽量少的添加括号,得到一个规则的序列. 例如:“()”.“[]”.“(())”.“([])”.“()[]”.“()[( ...