POJ 3620 Avoid The Lakes
dfs,统计最多多少个相邻(有公共边)方块。
# include <cstdio> # include <cstring> # define N 100 + 15 int n, m, k; char lake[N][N]; int ans, cur; const int dx[] = {0,1,0,-1}; const int dy[] = {1,0,-1,0}; void dfs(int x, int y) { lake[x][y] = 0, ++cur; for (int i = 0; i < 4; ++i) { int nx = x + dx[i]; int ny = y + dy[i]; if (1<=nx&&nx<=n && 1<=ny&&ny<=m && lake[nx][ny]) { dfs(nx, ny); } } } void init(void) { for (int i = 1; i <= n; ++i) { memset(lake[i]+1, 0, sizeof(lake[0][0])*m); } int r, c; for (int i = 1; i <= k; ++i) { scanf("%d%d", &r, &c); lake[r][c] = 1; } } void solve(void) { ans = 0; for (int i = 1; i <= n; ++i) for (int j = 1; j <= m; ++j) { cur = 0; if (lake[i][j]) dfs(i, j); if (cur > ans) ans = cur; } printf("%d\n", ans); } int main() { while (~scanf("%d%d%d", &n, &m, &k)) { init(); solve(); } return 0; }