poj_1979(dfs)
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
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
#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)的更多相关文章
- BZOJ 3083: 遥远的国度 [树链剖分 DFS序 LCA]
3083: 遥远的国度 Time Limit: 10 Sec Memory Limit: 1280 MBSubmit: 3127 Solved: 795[Submit][Status][Discu ...
- BZOJ 1103: [POI2007]大都市meg [DFS序 树状数组]
1103: [POI2007]大都市meg Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 2221 Solved: 1179[Submit][Sta ...
- BZOJ 4196: [Noi2015]软件包管理器 [树链剖分 DFS序]
4196: [Noi2015]软件包管理器 Time Limit: 10 Sec Memory Limit: 512 MBSubmit: 1352 Solved: 780[Submit][Stat ...
- 图的遍历(搜索)算法(深度优先算法DFS和广度优先算法BFS)
图的遍历的定义: 从图的某个顶点出发访问遍图中所有顶点,且每个顶点仅被访问一次.(连通图与非连通图) 深度优先遍历(DFS): 1.访问指定的起始顶点: 2.若当前访问的顶点的邻接顶点有未被访问的,则 ...
- BZOJ 2434: [Noi2011]阿狸的打字机 [AC自动机 Fail树 树状数组 DFS序]
2434: [Noi2011]阿狸的打字机 Time Limit: 10 Sec Memory Limit: 256 MBSubmit: 2545 Solved: 1419[Submit][Sta ...
- POJ_2386 Lake Counting (dfs 错了一个负号找了一上午)
来之不易的2017第一发ac http://poj.org/problem?id=2386 Lake Counting Time Limit: 1000MS Memory Limit: 65536 ...
- 深度优先搜索(DFS)
[算法入门] 郭志伟@SYSU:raphealguo(at)qq.com 2012/05/12 1.前言 深度优先搜索(缩写DFS)有点类似广度优先搜索,也是对一个连通图进行遍历的算法.它的思想是从一 ...
- 【BZOJ-3779】重组病毒 LinkCutTree + 线段树 + DFS序
3779: 重组病毒 Time Limit: 20 Sec Memory Limit: 512 MBSubmit: 224 Solved: 95[Submit][Status][Discuss] ...
- 【BZOJ-1146】网络管理Network DFS序 + 带修主席树
1146: [CTSC2008]网络管理Network Time Limit: 50 Sec Memory Limit: 162 MBSubmit: 3495 Solved: 1032[Submi ...
随机推荐
- tensorflow 高级api使用分布式之配置
"""Constructor. Sets the properties `cluster_spec`, `is_chief`, `master` (if `None` i ...
- ScrollReveal.js 用于创建和管理元素进入可视区域时的动画效果,帮助你的网站增加吸引力。
ScrollReveal.js 用于创建和管理元素进入可视区域时的动画效果,帮助你的网站增加吸引力. 1.http://www.yangqq.com/jstt/css3/2017-08-08/787. ...
- Centos7 安装 erlang rabbitmq
1.安装Erlang依赖采用官网的rpm包的形式进行安装,不采用yum(由系统进行自动安装 可能因为版本低的问题而出现一系列问题) erlang依赖 rpm包下载地址https://github.co ...
- c#中连接数据库
配置文件web.config: <connectionStrings> <add name="eport" connectionString="Data ...
- Java 中的 static 使用
Java语言基础--static 0.目录 8.static 8.1 Java 中的 static 使用之静态变量 8.2 Java 中的 static 使用之静态方法 8.3 Java 中的 sta ...
- NumPy Ndarray 对象
NumPy Ndarray 对象 NumPy 最重要的一个特点是其 N 维数组对象 ndarray,它是一系列同类型数据的集合,以 0 下标为开始进行集合中元素的索引. ndarray 对象是用于存放 ...
- Example of Formalising a Grammar for use with Lex & Yacc
Here is a sample of a data-file that we want to try and recognise. It is a list of students and info ...
- 204. Count Primes (Integer)
Count the number of prime numbers less than a non-negative number, n. 思路:寻找质数的方法 class Solution { pu ...
- Shell教程 之函数
1.函数定义 shell中函数的定义格式如下: [ function ] funname [()] { action; [return int;] } 说明: 可以带function fun() 定义 ...
- FortiGate密码恢复
1.需求 1.若设备的密码忘记,需要用配置线进行密码恢复: 2.密码恢复需要重启设备,并在设备的底层菜单界面上操作,会造成网络中断,请在方便断网时操作: 3.密码恢复后配置不会改变. 2.操作步骤 1 ...