题源 |
description |
有一间长方形的房子,地上铺了红色、黑色两种颜色的正方形瓷砖。你站在其中一块黑色的瓷砖上,只能向相邻的黑色瓷砖移动。请写一个程序,计算你总共能够到达多少块黑色的瓷砖。
|
input |
输入有多组。每组的第一行是两个整数W 和H,分别表示x 方向和y 方向瓷砖的数量。W 和H 都不超过20。在接下来的H 行中,每行包括W 个字符。
每个字符表示一块瓷砖的颜色,规则如下:
1)‘.’:黑色的瓷砖;
2)‘#’:白色的瓷砖;
3)‘@’:黑色的瓷砖,并且你站在这块瓷砖上。该字符在每个数据集合中唯一出现一次。
|
output |
输出你总共能够达到的方块数。
|
sample_input |
6 9
....#.
.....#
......
......
......
......
......
#@...#
.#..#.
11 9
.#.........
.#.#######.
.#.#.....#.
.#.#.###.#.
.#.#..@#.#.
.#.#####.#.
.#.......#.
.#########.
...........
11 6
..#..#..#..
..#..#..#..
..#..#..###
..#..#..#@.
..#..#..#..
..#..#..#..
7 7
..#.#..
..#.#..
###.###
...@...
###.###
..#.#..
..#.#..
0 0
|
sample_output |
45
59
6
13
|
#include <string.h> #include <stdio.h> #include <algorithm> #include <iostream> #include <string.h> using namespace std; const int dx[]={1,-1,0,0}; const int dy[]={0,0,1,-1}; int n,m; char a[100][100]; void dfs(int x,int y) { int tx,ty,i; a[x][y]='$'; for(int i=0;i<4;i++) { tx=dx[i]+x; ty=dy[i]+y; if(tx>=0&&tx<n&&ty>=0&&ty<m&&a[tx][ty]=='.') { dfs(tx,ty); } } } int main() { int i,j,count,x,y; while(~scanf("%d%d%*c",&m,&n)&&m+n!=0) { memset(a,0,sizeof(a)); for(i=0;i<n;i++) { for(j=0;j<m;j++) { scanf("%c",&a[i][j]); if(a[i][j]=='@') { x=i; y=j; } } getchar(); } dfs(x,y); count=0; for(i=0;i<n;i++) for(j=0;j<m;j++) if(a[i][j]=='$') count++; printf("%d\n",count); } return 0; }