# Problem M

### Problem 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)

### 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 <stdio.h>
#include <string.h>
struct queuenode{								//建结构
int a, b;
}queue[100000];

int count, sx, sy, tx, ty, head = 0,tail = 0;			//定义
int a, b, n, temp;
int dir[4][2] = { {1,0} , {0,1} , {-1,0} , {0,-1} };
bool map[110][110];
int m, k, i, j;
int inarea(int x,int y)					//判断是否在区域内
{
return x >= 0 && y >= 0 && x < a && y < b;
}

void bfs(  )									//bfs
{
map[sx][sy] = 1;
for(k = 0;k < 4; k++){				//四个方向
if(inarea(tx, ty) && map[tx][ty]== 0){
tail++;
count++;
queue[tail].a = tx;
queue[tail].b = ty;
map[tx][ty] = 1;
}
}
}
return;
}

int main(void)									//主函数
{
while(scanf("%d%d",&b,&a)==2&&b!=0){
memset(map, 1, sizeof(map));
while(scanf(" ")||scanf("\n")) ;
for(i = 0;i < a; i++){					//扫描地图
for(j = 0;j < b; j++){
scanf("%c",&temp);
if (temp == '#') map[i][j] = 1;
else if (temp == '.') map[i][j] = 0;
else if (temp == '@') {
map[i][j] = 0;
sx = i;
sy = j;
}
else {
j--;
continue;
}
}
}
count = 1;
bfs();									//进入bfs
printf("%d\n",count);
}
return 0;
}



#### HNCU1103：红与黑(BFS&DFS)

2013-07-14 21:08:38

#### POJ 1979 Red and Black（红与黑）

2015-12-09 20:13:23

#### 【NOI OJ】1818 红与黑

2016-08-26 16:06:14

#### poj2816-红与黑-C语言-递归算法入门

2017-06-11 19:15:15

#### NOI 1818:红与黑（C++）

2017-03-28 16:44:29

#### [OpenJudge] 2.5基本算法之搜索 红与黑

2016-08-24 15:19:55

#### 红与黑-dfs

2017-04-24 19:55:48

#### C++搜索与回溯算法之红与黑

2017-06-11 14:41:10

#### 【原创】浅谈搜索-中（dfs）（红与黑，Dungeon Master）

2016-10-12 13:32:16

#### HDU 1312 Red and Black(经典搜索,DFS&BFS三种方式)

2016-03-18 23:19:49