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 . 

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

....#.
.....#
......
......
......
......
......
#@...#
.#..#. .#.........
.#.#######.
.#.#.....#.
.#.#.###.#.
.#.#..@#.#.
.#.#####.#.
.#.......#.
.#########.
........... ..#..#..#..
..#..#..#..
..#..#..###
..#..#..#@.
..#..#..#..
..#..#..#.. ..#.#..
..#.#..
###.###
...@...
###.###
..#.#..
..#.#..

Sample Output


Source

 
 
 
 #include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
#define N 26
int n,m;
char mp[N][N];
int x,y;
int vis[N][N];
int ans;
int dirx[]={,,-,};
int diry[]={-,,,};
void dfs(int sx,int sy){
for(int i=;i<;i++){
int xx=sx+dirx[i];
int yy=sy+diry[i];
if(vis[xx][yy]) continue;
if(mp[xx][yy]=='#') continue;
if(xx< || xx>=n || yy< || yy>=m) continue;
vis[xx][yy]=;
ans++;
dfs(xx,yy);
}
}
int main()
{
while(scanf("%d%d",&m,&n)== && n+m){
for(int i=;i<n;i++){
scanf("%s",mp[i]);
for(int j=;j<m;j++){
if(mp[i][j]=='@'){
x=i;y=j;
}
}
}
memset(vis,,sizeof(vis));
ans=;
vis[x][y]=;
dfs(x,y);
printf("%d\n",ans); }
return ;
}

poj 1979 Red and Black(dfs水题)的更多相关文章

  1. POJ 1979 Red and Black dfs 难度:0

    http://poj.org/problem?id=1979 #include <cstdio> #include <cstring> using namespace std; ...

  2. poj 1979 Red and Black(dfs)

    题目链接:http://poj.org/problem?id=1979 思路分析:使用DFS解决,与迷宫问题相似:迷宫由于搜索方向只往左或右一个方向,往上或下一个方向,不会出现重复搜索: 在该问题中往 ...

  3. POJ 1979 Red and Black (红与黑)

    POJ 1979 Red and Black (红与黑) Time Limit: 1000MS    Memory Limit: 30000K Description 题目描述 There is a ...

  4. poj1564 Sum It Up dfs水题

    题目描述: Description Given a specified total t and a list of n integers, find all distinct sums using n ...

  5. 【wikioi】1229 数字游戏(dfs+水题)

    http://wikioi.com/problem/1229/ 赤裸裸的水题啊. 一开始我认为不用用完全部的牌,以为爆搜会tle.. 可是我想多了. 将所有状态全部求出,排序后暴力判断即可. (水题有 ...

  6. [POJ 1000] A+B Problem 经典水题 C++解题报告 JAVA解题报告

        A+B Problem Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 311263   Accepted: 1713 ...

  7. DFS水题 URAL 1152 False Mirrors

    题目传送门 /* 题意:一个圈,每个点有怪兽,每一次射击能消灭它左右和自己,剩余的每只怪兽攻击 搜索水题:sum记录剩余的攻击总和,tot记录承受的伤害,当伤害超过ans时,结束,算是剪枝吧 回溯写挫 ...

  8. POJ 1061 青蛙的约会 数论水题

    http://poj.org/problem?id=1061 傻逼题不多说 (x+km) - (y+kn) = dL 求k 令b = n-m ; a = x - y ; 化成模线性方程一般式 : Lx ...

  9. 【UVA - 1644 / POJ - 3518】Prime Gap(水题)

    Prime Gap 这里直接写中文了 Descriptions: 对于一个数n,若n为素数则输出0,否则找到距离n最小的两个素数,一个大于n,一个小于n,输出他们的差(正数) Input 多组输入 每 ...

  10. 咸鱼的ACM之路:DFS水题集

    DFS的核心就是从一种状态出发,转向任意的一个可行状态,直到达到结束条件为止.(个人理解) 下面全是洛谷题,毕竟能找到测试点数据的OJ我就找到这一个....在其他OJ上直接各种玄学问题... P159 ...

随机推荐

  1. 美丽的for循环语句

    美丽的for循环语句 题目:用for循环语句实现四个三角形不同的形状.   图案:    ---------------第一个三角形图形形状----------------**********第二个三 ...

  2. Fatal signal 11 (SIGSEGV) at 0xdeadbaad (code=1) 错误 解决方案(android-ndk)

    在android里做ndk编程的时候,碰到个随机性错误 错误信息如下: 05-06 15:59:44.411: A/libc(3347): Fatal signal 11 (SIGSEGV) at 0 ...

  3. window+Apache 配置虚拟主机(2)

    1. 打开虚拟主机功能 2. 设置虚拟主机相应的文件夹 3. 将虚拟的域名绑定到127.0.0.1 4. 结果图: 记忆一下,突然感觉都忘记了!

  4. Linux菜鸟学习笔记--Linux系统结构

      什么是Linux? Linux是一种自由和开放源码的类Unix操作系统,存在着许多不同的Linux版本,但它们都使用了Linux内核.严格来讲,Linux这个词本身只表示Linux内核,但实际上人 ...

  5. C++中数组初始化

    #include<iostream>using std::cout;using std::endl;int arr1[5];int arr2[5] = {1,3,5};int main() ...

  6. 关于在xp(sp3 专业版)下安装sql2005开发版图解

    今天我在xp上安装sql2005,搞了一上午也没有搞好,最终自己还是搞好,也装了,也卸载了!这里就总结一下,让以后用sql2005的朋友能有个参考!我也是自己在GOOGLE上搜索的! 转自:http: ...

  7. springmvc入门详解

    首先,我们先写一个入门小案例,先熟悉一下springmvc是什么,了解一下springmvc的运行流程,对加强springmvc的深层理解有很大帮助 .第一步,创建一个maven项目: <?xm ...

  8. javascript 识别移动端设备

    看到一种比较简单的方法,于是就把它记录下来备用吧.最近离职了,房子换了,还有...真是一把心酸,我知道谁活着都不容易,自己也资格把自己的苦水吐给别人,因为别人也过得不容易,所以大多不快都只能闷着,大家 ...

  9. SQL从入门到基础–08 Union、Union all及案例

    一.联合结果集 1. 简单的结果集联合: Select FNumber,FName,FAge from T_Employee union select FidCardNumber,FName,FAge ...

  10. SVN 密码破解,svn密码本地找回 忘记密码

    svn 密码被保存在本地文件中 C:\Users\[your computer name]\AppData\Roaming\Subversion\auth\svn.simple 文件下. 加密保存 到 ...