标签:
2 3 4 1 0 0 0 0 0 1 1 1 1 1 0 5 5 1 1 1 1 0 0 0 1 0 1 0 0 0 0 0 1 1 1 0 0 0 0 1 1 1
2 3
1 2 #include <iostream> 3 #include <cstdio> 4 #include <cstring> 5 using namespace std; 6 7 int visit[105][105]; 8 int s[105][105], m, n; 9 const int dx[4] = {-1,0,1,0}; 10 const int dy[4] = {0,1,0,-1}; 11 12 void dfs(int i,int j) 13 { 14 visit[i][j] = 1; 15 for(int k = 0; k < 4; k++) 16 { 17 int nx = i + dx[k]; 18 int ny = j + dy[k]; 19 if((nx >= 0) && (nx < m) && (ny >= 0) && (ny < n) && (visit[nx][ny] == 0) && (s[nx][ny] == 1)) 20 { 21 dfs(nx,ny); 22 } 23 } 24 } 25 26 27 int main(){ 28 int cou, test; 29 cin >> test; 30 31 while(test--) 32 { 33 cou = 0; 34 memset(visit, 0, sizeof(visit)); 35 memset(s, 0, sizeof(s)); 36 37 cin >> m >> n; 38 for(int i = 0; i < m; i++) 39 for(int j = 0; j < n; j++) 40 cin >> s[i][j]; 41 42 for(int i = 0; i < m; i++) 43 for(int j = 0; j < n; j++) 44 if(visit[i][j] == 0 && s[i][j] == 1) 45 { 46 cou++; 47 dfs(i, j); 48 } 49 50 printf("%d\n", cou); 51 } 52 return 0; 53 }
标签:
原文地址:http://www.cnblogs.com/qinduanyinghua/p/5571607.html