标签:
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1312
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 15186 Accepted Submission(s): 9401
1 #include<cstdio> 2 #include<cstring> 3 #include<algorithm> 4 #include<string> 5 using namespace std; 6 #define N 25 7 char mp[N][N]; 8 int go[4][2] = {{1,0},{-1,0},{0,-1},{0,1}}; 9 10 int n,m; 11 bool ck(int x, int y){ 12 if(x<n&&x>=0&&y<m&&y>=0) return true; 13 else return false; 14 } 15 bool vis[N][N]; 16 void dfs(int x, int y, int &sum) 17 { 18 vis[x][y] = 1; 19 bool fl = 0; 20 for(int i = 0; i < 4; i++){ 21 int xx = x + go[i][0]; 22 int yy = y + go[i][1]; 23 if(ck(xx,yy)&&!vis[xx][yy]&&mp[xx][yy]==‘.‘){ 24 fl = 1; 25 sum = sum+1; 26 dfs(xx,yy,sum); 27 } 28 } 29 if(fl==0) return; 30 } 31 int main() 32 { 33 while(~scanf("%d%d",&m,&n)) 34 { 35 int x, y; 36 if(n==0&&m==0) break; 37 getchar(); 38 for(int i = 0; i < n; i++){ 39 for(int j = 0; j < m; j++){ 40 scanf("%c",&mp[i][j]); 41 if(mp[i][j]==‘@‘){ x = i; y = j; } 42 } 43 getchar(); 44 } 45 memset(vis,0,sizeof(vis)); 46 int sum = 0; 47 dfs(x,y,sum); 48 printf("%d\n",sum+1); 49 } 50 return 0; 51 }
标签:
原文地址:http://www.cnblogs.com/shanyr/p/5207020.html