1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
| #include <iostream> #include <cstring> using namespace std; int n, m, h, arr[102][102], v[102][102], xx, yy, ans, tmp, dx[4] = {0, 0, -1, 1}, dy[4] = {-1, 1, 0, 0}; void dfs(int x, int y) { v[y][x] = 1; for(int i = 0; i < 4; i++) { xx = x + dx[i]; yy = y + dy[i]; if(xx > n || yy > n || xx < 1 || yy < 1 || arr[yy][xx] <= h || v[yy][xx]) continue; dfs(xx, yy); } } int main() { cin >> n; for(int i = 1; i <= n; i++) for(int j = 1; j <= n; j++) { cin >> arr[i][j]; m = max(m, arr[i][j]); } for(h = 0; h <= m; h++) { memset(v, 0, sizeof(v)); tmp = 0; for(int i = 1; i <= n; i++) for(int j = 1; j <= n; j++) if(arr[i][j] > h && !v[i][j]) { dfs(j, i); tmp++; } ans = max(ans, tmp); } cout << ans; }
|