标签:
Sample Input1 6 7 ....... ..***.. ..*..*. ..*..*. ...**.. ....... Sample Input2 5 7 ....... ..***.. ..*.*.. ..***.. .......
Sample Output1 4 Sample Output2 1
1 #include <iostream> 2 #include <algorithm> 3 #include <cstdio> 4 #include <cstring> 5 using namespace std; 6 int map[101][101]; 7 int dirx[4]={1,-1,0,0}; 8 int diry[4]={0,0,-1,1}; 9 int m,n; 10 int dfs(int x,int y) 11 { 12 int i,j,dx,dy; 13 if(x<0||x>m+1||y<0||y>n+1||map[x][y]==1) 14 return 0; 15 map[x][y]=1; 16 for(i=0;i<4;i++) 17 { 18 dx=x+dirx[i]; 19 dy=y+diry[i]; 20 dfs(dx,dy); 21 } 22 return 0; 23 } 24 int main() 25 { 26 int i,j; 27 //freopen("in.txt","r",stdin); 28 while(cin>>m>>n) 29 { 30 char s; 31 bool flag=0; 32 int sum=0; 33 memset(map,0,sizeof(map)); 34 for(i=1;i<=m;i++) 35 { 36 for(j=1;j<=n;j++) 37 { 38 cin>>s; 39 map[i][j]=(s==‘.‘?0:1); 40 } 41 } 42 dfs(0,0); 43 for(i=1;i<=m;i++) 44 { 45 for(j=1;j<=n;j++) 46 if(map[i][j]) 47 sum++; 48 } 49 cout<<m*n-sum<<endl; 50 } 51 }
标签:
原文地址:http://www.cnblogs.com/a1225234/p/4896350.html