标签:
Description
Input
Output
Sample Input
Sample Output
1 #include <iostream> 2 #include <string.h> 3 using namespace std; 4 5 char map[105][105]; 6 int way[105][105]; 7 int all; 8 int m,n; 9 10 void read_map() 11 { 12 memset(way,0,sizeof(way)); 13 for (int i=1;i<=m;i++) 14 { 15 for (int j=1;j<=n;j++) 16 { 17 cin>>map[i][j]; 18 } 19 } 20 21 } 22 23 void dfs(int x,int y) 24 { 25 way[x][y]=1; 26 if (x+1<=m&&map[x+1][y]==‘@‘&&way[x+1][y]==0) dfs(x+1,y); 27 if (x+1<=m&&y-1>=1&&map[x+1][y-1]==‘@‘&&way[x+1][y-1]==0) dfs(x+1,y-1); 28 if (y-1>=1&&map[x][y-1]==‘@‘&&way[x][y-1]==0) dfs(x,y-1); 29 if (y-1>=1&&x-1>=1&&map[x-1][y-1]==‘@‘&&way[x-1][y-1]==0) dfs(x-1,y-1); 30 if (x-1>=1&&map[x-1][y]==‘@‘&&way[x-1][y]==0) dfs(x-1,y); 31 if (x-1>=1&&y+1<=n&&map[x-1][y+1]==‘@‘&&way[x-1][y+1]==0) dfs(x-1,y+1); 32 if (y+1<=n&&map[x][y+1]==‘@‘&&way[x][y+1]==0) dfs(x,y+1); 33 if (y+1<=n&&x+1<=m&&map[x+1][y+1]==‘@‘&&way[x+1][y+1]==0) dfs(x+1,y+1); 34 } 35 36 37 int main() 38 { 39 40 while (cin>>m>>n) 41 { 42 if (m==0&&n==0) break; 43 44 all=0; 45 read_map(); 46 for (int i=1;i<=m;i++) 47 { 48 for (int j=1;j<=n;j++) 49 { 50 if (map[i][j]==‘@‘&&way[i][j]==0) 51 { 52 dfs(i,j); 53 all++; 54 } 55 } 56 } 57 cout<<all<<endl; 58 } 59 60 return 0; 61 }
标签:
原文地址:http://www.cnblogs.com/haoabcd2010/p/5676724.html