Nyoj 27 水池数目
2014-04-03 21:43
204 查看
跟poj1562相当类似!
题目来源:http://acm.nyist.net/JudgeOnline/problem.php?pid=27
题目来源:http://acm.nyist.net/JudgeOnline/problem.php?pid=27
#include <iostream> #include <cstring> #include <cstdio> using namespace std; const int MAXN = 110; int Graph[MAXN][MAXN]; int m, n; int dir[4][2] = {{-1, 0}, {0, -1}, {1, 0}, {0, 1}};//上下左右四个方向 bool Is_Cango(int x, int y)//判断x, y这个位置是否在边界之外 { if(x < 0 || x >= m || y < 0 || y >= n) return false; return true; } void DFS(int x, int y) { int xx, yy; Graph[x][y] = 0;//把当前位置标志为0,说明这个水坑已经数过 for(int i = 0; i < 4; ++i) { xx = x + dir[i][0]; yy = y + dir[i][1]; if(Is_Cango(xx, yy) == false) continue ; if(Graph[xx][yy] == 1)//看他的左右上下是否还存在水坑 DFS(xx, yy); } } int main() { int T; int kcount; int i, j; scanf("%d", &T); while(T--) { scanf("%d %d", &m, &n); memset(Graph, 0, sizeof(Graph)); for(i = 0; i < m; ++i) for(j = 0; j < n; ++j) scanf("%d", &Graph[i][j]); kcount = 0; for(i = 0; i < m; ++i) { for(j = 0; j < n; ++j) { if(Graph[i][j] == 1) { DFS(i, j); kcount++; } } } printf("%d\n", kcount); } return 0; }