Red and Black
Time Limit: 1000MS | Memory Limit: 30000K | |
Total Submissions: 19254 | Accepted: 10252 |
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.
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.
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
一道基础的深搜题,一个人从给定的位置开始走,可以朝上下左右四个方向,只能走黑色的格子,问这个人一共可以走多少个格子
只要从起始位置开始深搜就可以了
1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 5 using namespace std; 6 7 int w,h,start_x,start_y,ans; 8 bool dfsed[30][30]; 9 char g[30][30]; 10 11 void dfs(int x,int y) 12 { 13 if(g[x-1][y]=='.'&&(!dfsed[x-1][y])) 14 { 15 dfsed[x-1][y]=true; 16 dfs(x-1,y); 17 ans++; 18 } 19 if(g[x+1][y]=='.'&&(!dfsed[x+1][y])) 20 { 21 dfsed[x+1][y]=true; 22 dfs(x+1,y); 23 ans++; 24 } 25 if(g[x][y-1]=='.'&&(!dfsed[x][y-1])) 26 { 27 dfsed[x][y-1]=true; 28 dfs(x,y-1); 29 ans++; 30 } 31 if(g[x][y+1]=='.'&&(!dfsed[x][y+1])) 32 { 33 dfsed[x][y+1]=true; 34 dfs(x,y+1); 35 ans++; 36 } 37 } 38 39 int main() 40 { 41 while(scanf("%d %d",&w,&h)==2) 42 { 43 getchar(); 44 if(w==0&&h==0) 45 break; 46 for(int i=1;i<=h;i++) 47 gets(&g[i][1]); 48 for(int i=0;i<=h+1;i++) 49 g[i][0]=g[i][w+1]='#'; 50 for(int i=1;i<=w+1;i++) 51 g[0][i]=g[h+1][i]='#'; 52 for(int i=1;i<=h;i++) 53 for(int j=1;j<=w;j++) 54 if(g[i][j]=='@') 55 { 56 start_x=i; 57 start_y=j; 58 } 59 ans=1; 60 memset(dfsed,false,sizeof(dfsed)); 61 dfs(start_x,start_y); 62 cout<<ans<<endl; 63 } 64 65 return 0; 66 }