HDU 1312
View Code
1 #include<stdio.h> 2 #include<stdlib.h> 3 #include<string.h> 4 以前用BFS写过,这次改用DFS写发现DFS掌握的不好,挑了好久才过,今下午才AC了两道题,还要加紧练习DFS! 5 int row,col,count,vis[21][21]; 6 char map[21][21]; 7 int dx[] = {0,0,-1,1}; 8 int dy[] = {1,-1,0,0}; 9 void DFS(int x,int y) 10 { 11 int sx,sy,i; 12 vis[x][y] = 1; 13 for(i=0; i<4; i++) 14 { 15 sx = x + dx[i]; 16 sy = y + dy[i]; 17 if(!vis[sx][sy]&&sx>=0&&sx<row&&sy>=0&&sy<col&&map[sx][sy]!='#') 18 { 19 vis[sx][sy] = 1; 20 if(map[sx][sy]=='.') 21 count++; 22 DFS(sx,sy); 23 } 24 } 25 return ; 26 } 27 28 int main() 29 { 30 int i,j,bx,by; 31 32 while(scanf("%d%d",&col,&row)&&col&&row) 33 { 34 getchar(); 35 for(i=0; i<row; i++) 36 { 37 for(j=0; j<col; j++) 38 { 39 scanf("%c",&map[i][j]); 40 } 41 getchar(); 42 } 43 for(i=0; i<row; i++) 44 for(j=0; j<col; j++) 45 { 46 if(map[i][j]=='@') 47 { 48 bx = i; 49 by = j; 50 } 51 } 52 count = 1; 53 memset(vis,0,sizeof(vis)); 54 vis[bx][by] = 1; 55 DFS(bx,by); 56 printf("%d\n",count); 57 58 } 59 return 0; 60 }