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>
#include<cstdio>
#include<algorithm>
using namespace std;
int n,m;
int ans=;
int vis[][];
char title[][];
int fx[]={-,,,},fy[]={,,,-}; void dfs(int x,int y){
ans++;
vis[x][y]=;
for(int i=; i<; i++ ){
int nx=x+fx[i];
int ny=y+fy[i];
if(nx>=&&nx<n&&ny>=&&ny<m&&title[nx][ny]=='.'&&!vis[nx][ny]){
dfs(nx,ny);
}
}
} int main(){
while(~scanf("%d%d",&m,&n)&&n&&m){
// getchar();
ans=;
memset(vis,,sizeof(vis));
for( int i=; i<n; i++ ){
cin>>title[i];
}
for( int i=; i<n; i++ ){
for(int j=; j<m; j++ ){
if(title[i][j]=='@'&&!vis[i][j]){
dfs(i,j);
break;
}
}
}
printf("%d\n",ans);
} return ;
}

Red and Black---POJ - 1979的更多相关文章

  1. Red and Black(poj 1979 bfs)

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

  2. DFS:Red and Black(POJ 1979)

    红与黑 题目大意:一个人在一个矩形的房子里,可以走黑色区域,不可以走红色区域,从某一个点出发,他最多能走到多少个房间? 不多说,DFS深搜即可,水题 注意一下不要把行和列搞错就好了,我就是那样弄错过一 ...

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

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

  4. OpenJudge/Poj 1979 Red and Black / OpenJudge 2816 红与黑

    1.链接地址: http://bailian.openjudge.cn/practice/1979 http://poj.org/problem?id=1979 2.题目: 总时间限制: 1000ms ...

  5. poj 1979 Red and Black 题解《挑战程序设计竞赛》

    地址 http://poj.org/problem?id=1979 Description There is a rectangular room, covered with square tiles ...

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

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

  7. poj 1979 Red and Black(dfs)

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

  8. POJ 1979 Red and Black (zoj 2165) DFS

    传送门: poj:http://poj.org/problem?id=1979 zoj:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problem ...

  9. HDOJ 1312 (POJ 1979) Red and Black

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

  10. poj 1979 Red and Black(dfs水题)

    Description There is a rectangular room, covered with square tiles. Each tile is colored either red ...

随机推荐

  1. 吴恩达《机器学习》课程笔记——第六章:Matlab/Octave教程

    上一篇  ※※※※※※※※  [回到目录]  ※※※※※※※※  下一篇 这一章的内容比较简单,主要是MATLAB的一些基础教程,如果之前没有学过matlab建议直接找一本相关书籍,边做边学,matl ...

  2. <A>标记onclick事件

    <script> function ss() { document.getElementById("btnPublicity").click(); }</scri ...

  3. 提示缺少tns_admin的解决办法

    问题:安装ZL数据安全辅助工具后,执行时出现 “未设置系统环境变量tns_admin!” 解决: 1.查找tnsnames.ora所在目录,比如这里的目录为:C:\app\H\product\11.2 ...

  4. 初学python之路-day08

    #学习了编码后,还要了解三种字符串.# 一.# 普通字符串:u''以字符作为输出单位,# print(u'abc') # 用于显示abc# # 二进制字符串:b'' 二进制字符串以字节作为输出单位# ...

  5. MQTT初步使用

    环境搭建 1.mosquitto所需要的rpm包 2.c-ares-1.12.0 3.安装最新的openssl版本 4.mosquitto-1.4.10 mosquitto需要的rpm包 c-ares ...

  6. http 四大特征

  7. 快捷键打开Generate

    在eclipse下有覆盖toString.hashcode.setter.getter等功能. 在intelj idea中,同样具有类似的功能. 按下键盘上的alt+insert键,就会弹出gener ...

  8. TP-Shop安装步骤教程(Windows版)

    TP-Shop安装步骤教程(Windows版) PS:首次发文,请多指教! 一.安装要求 1.PHP5.4以上,MYsql5.5以上. 2.需要Phpcurl,gd库.php_mysqli,php_o ...

  9. kettle基础概念的学习

    参考书籍:Pentaho Kettle Solutions中文版.由于最近不断的使用kettle,随着不断深入使用,遇到的问题越来越多,发现脑子那点货根本不够用,所以根据阅读把一些概念记录一下,方便自 ...

  10. django.db.utils.OperationalError: cannot ALTER TABLE "servers_ecs" because it has pending trigger events

    数据库:postgresql9.4 原因:这是因为数据库的字段原来可以为null,现在改变该字段为not null,而你的数据库有数据,而这些数据该字段已经为null