Red and Black
Time Limit: 1000MS   Memory Limit: 30000K
Total Submissions: 26944   Accepted: 14637

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
/*题目大意:在一个矩形房间里,里面填满着红砖与黑砖,每次走动只能走黑砖位置,而不能走红砖,
* 可以上下左右四个方向走动,现在初始位置在黑砖位置上,试问从此黑砖位置开始能够到达的砖的数量是多少
*算法分析:从起始位置开始四个方向进行dfs,若遇到红砖位置则将此砖块换为黑砖。换砖的次数即为到达的黑砖数量 */ #include <iostream>
#include <cstdio>
using namespace std; char a[25][25];
int n, m;
int dir[4][2] = {0, 1, 1, 0, 0, -1, -1, 0}; //四个方向数组 void dfs(int x, int y, int &res) {
a[x][y] = '#';
res ++ ;
for (int i = 0; i<4; i++) {
int nx = x + dir[i][0];
int ny = y + dir[i][1];
if (nx>=0 && nx<n && ny>=0 && ny<m && a[nx][ny] == '.')
dfs(nx, ny, res);
}
} int main() { while (cin >> m >> n && (n+m)) { memset(a, 0, sizeof(a));
for (int i = 0; i<n; i++) {
for (int j = 0; j<m; j++) {
cin >> a[i][j];
}
}
int res = 0;
for (int i = 0; i<n; i++) {
for (int j = 0; j<m; j++) {
if (a[i][j] == '@')
dfs(i, j, res);
}
}
cout << res << endl;
}
return 0;
}

DFS入门__poj1979的更多相关文章

  1. 算法学习之BFS、DFS入门

    算法学习之BFS.DFS入门 0x1 问题描述 迷宫的最短路径 给定一个大小为N*M的迷宫.迷宫由通道和墙壁组成,每一步可以向相邻的上下左右四格的通道移动.请求出从起点到终点所需的最小步数.如果不能到 ...

  2. Oil Deposits(poj 1526 DFS入门题)

    http://poj.org/problem?id=1562                                                                       ...

  3. DFS入门之二---DFS求连通块

    用DFS求连通块也是比较典型的问题, 求多维数组连通块的过程也称为--“种子填充”. 我们给每次遍历过的连通块加上编号, 这样就可以避免一个格子访问多次.比较典型的问题是”八连块问题“.即任意两格子所 ...

  4. DFS入门之一

    深度优先搜索实现较为简单,需要控制两个因素: 1.已经访问过的元素不能再访问,在实际题目中还要加上不能访问的元素(障碍) 2.越界这种情况是不允许的 以杭电的1312 Red and Black 为例 ...

  5. poj1562 DFS入门

    K - 搜索 Crawling in process... Crawling failed Time Limit:1000MS     Memory Limit:10000KB     64bit I ...

  6. [HDU]1016 DFS入门题

    题目的意思就是在1到n的所有序列之间,找出所有相邻的数相加是素数的序列.Ps:题目是环,所以头和尾也要算哦~ 典型的dfs,然后剪枝. 这题目有意思的就是用java跑回在tle的边缘,第一次提交就tl ...

  7. POJ 3984(DFS入门题 +stack储存路径)

    POJ 3984 Description 定义一个二维数组: int maze[5][5] = { 0, 1, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, ...

  8. HDU 1241 连通块问题(DFS入门题)

    Input The input file contains one or more grids. Each grid begins with a line containing m and n, th ...

  9. POJ 1416 Shredding Company【dfs入门】

    题目传送门:http://poj.org/problem?id=1416 Shredding Company Time Limit: 1000MS   Memory Limit: 10000K Tot ...

随机推荐

  1. 遍历map的几种方式

    1,平时开发中对map的使用很多,然后发现了很多map可能存在的各种问题:如HashMap 需要放置 1024 个元素,由于没有设置容量初始大小,随着元素不断增加,容量 7 次被迫扩大,resize ...

  2. [array] leetcode - 42. Trapping Rain Water - Hard

    leetcode - 42. Trapping Rain Water - Hard descrition Given n non-negative integers representing an e ...

  3. Udacity并行计算课程笔记-The GPU Programming Model

    一.传统的提高计算速度的方法 faster clocks (设置更快的时钟) more work over per clock cycle(每个时钟周期做更多的工作) more processors( ...

  4. 【知了堂学习笔记】_String、StringBuffer与StringBuilder的区别

    String Stringbuffer  StringBuilder的区别: 1.string是字符串常量,且长度是不可改变的,Stringbuffer.stringBuilder是字符串变量 2.S ...

  5. Netty-Websocket 根据URL路由,分发机制的实现

    最近在做netty整合websocket,发现网上很多项目都是最简单的demo,单例的一个项目. 然而公司的项目需要接受几个不同功能的ws协议消息,因此最好是用URL来区分,让页面上采用不同的链接方式 ...

  6. 虚拟机下linux系统安装nginx

    近在windows虚机下安装nginx,也遇到部分问题,写篇随笔总结一下 一.安装虚机 windows下安装虚拟机我就不说了,一搜一大把,一直下一步就ok了 二. 打开虚拟安装nginx 1.选择版本 ...

  7. .bashrc:16: command not found: shopt配置环境变量时出错

    source .bashrc ------------------------------------------------------- .bashrc:: command not found: ...

  8. 【原创】java NIO FileChannel 学习笔记 FileChannel 简介

    java NIO 中FileChannel 的实现类是  FileChannelImpl,FileChannel本身是一个抽象类. 先介绍FileChannel File Channels 是线程安全 ...

  9. Android 根据字符串动态获取资源ID

    1.常用方法public int getResId(String name,Context context){ Resources r = context.getResources(); int id ...

  10. 【NOIP2012】疫情传递

    题解 这题是真的烦... 越来越心疼2012年的dalao们了[不过好像dalao们都不需要本蒟蒻的心疼2333] 其实这题还有点半懂不懂... 所以把洛谷上一个比较好的题解粘过来记忆一下233 1. ...