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 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
| #include <iostream> #include <queue> #include <cstring> using namespace std; int N, max_height, ans = 1; int arr[100][100]; bool visited[100][100]; int dx[4] = { -1, 1, 0, 0 }; int dy[4] = { 0, 0, -1, 1 }; void BFS(int x, int y, int h) { queue<pair<int, int>> q; q.push({ x, y }); visited[x][y] = true; while (!q.empty()) { int x = q.front().first; int y = q.front().second; q.pop(); for (int dir = 0; dir < 4; ++dir) { int nx = x + dx[dir]; int ny = y + dy[dir]; if (nx < 0 || ny < 0 || nx >= N || ny >= N) continue; if (visited[nx][ny] || arr[nx][ny] <= h) continue; visited[nx][ny] = true; q.push({ nx, ny }); } } } int main() { cin >> N; for (int i = 0; i < N; i++){ for (int j = 0; j < N; j++){ cin >> arr[i][j]; if (max_height < arr[i][j]) max_height = arr[i][j]; } } for (int h = 1; h <= max_height; ++h) { int area = 0; for (int i = 0; i < N; i++) { for (int j = 0; j < N; j++) { if (visited[i][j] || arr[i][j] <= h) continue; BFS(i, j, h); area++; } } if (ans < area) ans = area; memset(visited, false, sizeof(visited)); } cout << ans << "\n"; return 0; }
|