标签:
简单深搜
1 #include <cstdio> 2 int dir[4][2] = {{0,1},{1,0},{0,-1},{-1,0}}; 3 char map[21][21]; 4 int n,m; 5 int result; 6 7 void DFS(int sx, int sy) 8 { 9 int tx,ty; 10 for(int k = 0; k < 4; ++k) 11 { 12 tx = sx + dir[k][0]; 13 ty = sy + dir[k][1]; 14 15 if(tx < 0 || ty < 0 || tx >= m || ty >= n) 16 continue; 17 18 if(map[tx][ty] == ‘.‘) 19 { 20 ++result; 21 map[tx][ty] = ‘#‘; 22 DFS(tx,ty); 23 } 24 } 25 } 26 27 int main() 28 { 29 int sx,sy; 30 while(scanf("%d %d",&n,&m) && n+m) 31 { 32 result = 1; 33 for(int i = 0; i < m; ++i) 34 for(int j = 0; j < n; ++j) 35 { 36 scanf(" %c",&map[i][j]); 37 if(map[i][j] == ‘@‘) 38 { 39 sx = i; 40 sy = j; 41 } 42 } 43 44 DFS(sx,sy); 45 printf("%d\n",result); 46 } 47 }
标签:
原文地址:http://www.cnblogs.com/guoyongheng/p/5762470.html