Time Limit: 1000MS | Memory Limit: 30000K | |
Total Submissions: 23095 | Accepted: 12467 |
Description
Input
Output
Sample Input
6 9 ....#. .....# ...... ...... ...... ...... ...... #@...# .#..#. 11 9 .#......... .#.#######. .#.#.....#. .#.#.###.#. .#.#..@#.#. .#.#####.#. .#.......#. .#########. ........... 11 6 ..#..#..#.. ..#..#..#.. ..#..#..### ..#..#..#@. ..#..#..#.. ..#..#..#.. 7 7 ..#.#.. ..#.#.. ###.### ...@... ###.### ..#.#.. ..#.#.. 0 0
Sample Output
45 59 6 13
Source
#include<stdio.h> #include<string.h> char a[22][22]; int v[22][22]; int sum; void dfs(int i,int j) { if(a[i-1][j]=='.'&&v[i-1][j]==0) { sum++; v[i-1][j]=1; dfs(i-1,j); } if(a[i][j-1]=='.'&&v[i][j-1]==0) { sum++; v[i][j-1]=1; dfs(i,j-1); } if(a[i][j+1]=='.'&&v[i][j+1]==0) { sum++; v[i][j+1]=1; dfs(i,j+1); } if(a[i+1][j]=='.'&&v[i+1][j]==0) { sum++; v[i+1][j]=1; dfs(i+1,j); } } int main() { int n,m,i,j; while(~scanf("%d%d",&n,&m),n||m) { getchar(); sum=0; memset(v,0,sizeof(v)); memset(a,'#',sizeof(a)); for(i=0;i<m;i++) { for(j=0;j<n;j++) { scanf("%c",&a[i][j]); } getchar(); } for(i=0;i<m;i++) { for(j=0;j<n;j++) { if(a[i][j]=='@') { v[i][j]=1; sum+=1; dfs(i,j); break; } } } printf("%d\n",sum); } return 0; }
原文地址:http://blog.csdn.net/ice_alone/article/details/41159245