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. IntelliJ IDEA安装ideaIU-2019.1

  2. UGUI中粒子特效与UI的遮挡问题

    问题背景: 在做主线任务时发现完成任务后的特效显示穿透上面的UI层,不美观,策划不乐意了,抓紧解决下 解决思路: 首先讲下影响渲染顺序的因素: 能够影响渲染顺序的因素有:1.Camera Depth  ...

  3. 将Go的main包拆分为多个文件

    将Go的main包拆分为多个文件的写法和普通包是完全一致的,其使用规则也相同.如编写main包结构如下: main |----main.go |----show.go 在main.go中编写了main ...

  4. 使用Jenkins时,如果GIT_COMMIT无变化,跳过构建

    使用Jenkins时,如果GIT_COMMIT无变化,跳过构建    使用插件: conditional-buildstep A buildstep wrapping any number of ot ...

  5. Liunx系统下的进程与线程

    1.    进程.线程的概念 a.    进程是操作系统进行资源分配的单位. b.    线程(Thread)是程序中独立的指令流,是CPU调度和分派的基本单位. c.     多进程是指同时运行多种 ...

  6. C# 实现 Snowflake算法生成唯一性Id

    参考地址:https://blog.csdn.net/w200221626/article/details/52064976 /// <summary> /// 动态生产有规律的ID // ...

  7. Codeforces 677E Vanya and Balloons

    Vanya and Balloons 枚举中心去更新答案, 数字过大用log去比较, 斜着的旋转一下坐标, 然后我旋出来好多bug.... #include<bits/stdc++.h> ...

  8. angular之表达式

    1.作用:使用表达式把数据绑定到HTML. 2.语法:表达式写在双打括号内:{{expression}} 3.比较:表达式作用类似于ng-bind指令:建议更多的使用指令. 4.AngularJS表达 ...

  9. Array库

    /** * 查找元素在数组中出现的所有位置 * @param {要查找的数组} array * @param {要查找的元素} ele * @param {回调函数} callback */ func ...

  10. 基于Emgucv,C#的图片旋转方式

    /// <summary> /// 图片旋转 --百度 旋转仿射 /// </summary> /// <param name="modelImage" ...