标签:
速刷一道DFS
Description
Input
Output
Sample Input
6 9 ....#. .....# ...... ...... ...... ...... ...... #@...# .#..#. 11 9 .#......... .#.#######. .#.#.....#. .#.#.###.#. .#.#..@#.#. .#.#####.#. .#.......#. .#########. ........... 11 6 ..#..#..#.. ..#..#..#.. ..#..#..### ..#..#..#@. ..#..#..#.. ..#..#..#.. 7 7 ..#.#.. ..#.#.. ###.### ...@... ###.### ..#.#.. ..#.#.. 0 0
Sample Output
45 59 6 13
Source
刷道普通的DFS,复习模板
1 /*by SilverN*/ 2 #include<iostream> 3 #include<algorithm> 4 #include<cstring> 5 #include<cstdio> 6 #include<cmath> 7 using namespace std; 8 const int mxn=50; 9 char mp[mxn][mxn]; 10 int ans; 11 int W,H; 12 void dfs(int x,int y){ 13 if(x<1 || x>H || y<1 || y>W)return; 14 if(mp[x][y]==‘#‘)return; 15 ans++; 16 mp[x][y]=‘#‘; 17 dfs(x+1,y); 18 dfs(x,y+1); 19 dfs(x-1,y); 20 dfs(x,y-1); 21 return; 22 } 23 int main(){ 24 while(scanf("%d%d",&W,&H) && W && H){ 25 memset(mp,‘ ‘,sizeof(mp)); 26 ans=0; 27 int i,j; 28 for(i=1;i<=H;i++){ 29 scanf("%s",mp[i]+1); 30 } 31 int sx,sy; 32 bool flag=0; 33 for(i=1;i<=H;i++){ 34 for(j=1;j<=W;j++) 35 if(mp[i][j]==‘@‘){ 36 sx=i;sy=j; 37 flag=1; 38 break; 39 } 40 if(flag)break; 41 } 42 dfs(sx,sy); 43 printf("%d\n",ans); 44 } 45 return 0; 46 }
标签:
原文地址:http://www.cnblogs.com/SilverNebula/p/5585837.html