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.
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.
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).
1 #include
2 #include<string.h>
3 struct node
4 {
5 int x,y;
6 }q[410];
7
8 struct node P, N;
9 int flag[25][25];
10 int dir[4][2]={{1,0},{-1,0},{0,1},{0,-1}};
11 char str[25][25];
12
13 int main()
14 {
15 int c, r, i, j, front, rear;
16 while(scanf("%d%d",&c,&r)!=EOF, c + r){
17 memset(flag, 0, sizeof(flag));
18 for(i = 0; i )
19 scanf("%s", str[i]);
20
21 for(i = 0; i ){
22 for(j=0;j)
23 if(str[i][j] == ‘@‘) break;
24 if(str[i][j] == ‘@‘) break;
25 }
26 N.x = i;
27 N.y = j;
28 flag[i][j] = 1;
29 q[0] = N;
30 frOnt= 0;
31 rear = 1;
32
33 while(front < rear){
34 N = q[front++];
35 for(i = 0; i <4; i++){
36 int tx = N.x + dir[i][0];
37 int ty = N.y + dir[i][1];
38 if(tx >= 0 && tx = 0&& ty 0 && str[tx][ty] == ‘.‘){
39 P.x = tx;
40 P.y = ty;
41 q[rear++] = P;
42 flag[tx][ty] = 1;
43 }
44 }
45 }
46 printf("%d\n",rear);
47 }
48 return 0;
49 }