标签:
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 22931 Accepted Submission(s): 13213
1 #include <iostream> 2 #include <cstring> 3 using namespace std; 4 int m,n,many; 5 char s[105][105]; 6 int visit[105][105]; 7 const int dx[] = {0,0,1,1,1,-1,-1,-1}; 8 const int dy[] = {1,-1,0,1,-1,0,1,-1}; 9 10 void dfs(int i, int j){ 11 visit[i][j] = 1; 12 for(int k = 0; k < 8; k++) 13 { 14 int nx = i + dx[k]; 15 int ny = j + dy[k]; 16 if(nx >= 0 && nx < m && ny >= 0 && ny < n && visit[nx][ny] == 0 && s[nx][ny] == ‘@‘) 17 dfs(nx,ny); 18 } 19 //return 1; 20 } 21 22 int main(){ 23 while(cin >> m >> n) 24 { 25 many = 0; 26 memset(s, 0, sizeof(s)); 27 memset(visit,0,sizeof(visit)); 28 if(m == 0) 29 break; 30 for(int i = 0; i < m; i++){ 31 cin >> s[i]; 32 } 33 for(int i = 0; i < m; i++) 34 { 35 for(int j = 0; j < n; j++) 36 { 37 if(s[i][j] == ‘@‘ && visit[i][j] == 0) 38 { 39 many++; 40 dfs(i, j); 41 } 42 43 } 44 } 45 cout << many << endl; 46 } 47 return 0; 48 }
标签:
原文地址:http://www.cnblogs.com/qinduanyinghua/p/5513527.html