标签:
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 #include <cstdio> 2 #include <cstring> 3 #include <iostream> 4 using namespace std; 5 int n, m, map[110][110], ac[4][2] = {0, 1, 0, -1, -1, 0, 1, 0}; 6 int Dfs(int x, int y) 7 { 8 map[x][y] = 0; 9 for(int i = 0; i < 4; i++) 10 { 11 int nx = x + ac[i][0]; 12 int ny = y + ac[i][1]; 13 if(nx >=0 && nx < n && ny >= 0 && ny < m && map[nx][ny] == 1) 14 Dfs(nx, ny); 15 } 16 } 17 int main() 18 { 19 int t; 20 scanf("%d", &t); 21 while(t--) 22 { 23 scanf("%d %d", &n, &m); 24 for(int i = 0; i < n; i++) 25 for(int j = 0; j < m; j++) 26 scanf("%d", &map[i][j]); 27 int need = 0; 28 for(int i = 0; i < n; i++) 29 { 30 for(int j = 0; j < m; j++) 31 { 32 if(map[i][j] == 1) 33 { 34 Dfs(i, j); 35 need++; 36 } 37 } 38 } 39 printf("%d\n", need); 40 } 41 return 0; 42 }
标签:
原文地址:http://www.cnblogs.com/fengshun/p/4705762.html