|
| 1 | +//http://acmicpc.net/problem/2468 |
| 2 | +#include <iostream> |
| 3 | +#include <vector> |
| 4 | +#include <algorithm> |
| 5 | +#include <queue> |
| 6 | +#include <cstring> |
| 7 | +using namespace std; |
| 8 | + |
| 9 | +/* |
| 10 | + 높이 이하의 모든 지점 잠김 |
| 11 | + 물에 잠기지 않는 안전한 영역 최대 개수 |
| 12 | + 가장 높은 h, 낮은 h |
| 13 | + 비가 아예 안 내릴 경우도 있음 |
| 14 | + 최소 1군데 안전 영역s |
| 15 | +*/ |
| 16 | + |
| 17 | +struct Node { |
| 18 | + int x, y; |
| 19 | +}; |
| 20 | + |
| 21 | +int N, res = 1; |
| 22 | +int arr[101][101]; |
| 23 | +bool visited[101][101]; |
| 24 | +const int dx[4] = {-1, 0, 1, 0}; |
| 25 | +const int dy[4] = {0, 1, 0, -1}; |
| 26 | +int minH = 1e9, maxH = 0; |
| 27 | + |
| 28 | +bool OOB(int x, int y) { |
| 29 | + return x < 0 || x >= N || y < 0 || y >= N; |
| 30 | +} |
| 31 | + |
| 32 | +void bfs(int x, int y, int h) { |
| 33 | + queue<Node> q; |
| 34 | + q.push({x, y}); |
| 35 | + visited[x][y] = true; |
| 36 | + |
| 37 | + while(!q.empty()) { |
| 38 | + Node now = q.front(); |
| 39 | + q.pop(); |
| 40 | + |
| 41 | + for(int dir = 0; dir < 4; dir++){ |
| 42 | + int nx = now.x + dx[dir]; |
| 43 | + int ny = now.y + dy[dir]; |
| 44 | + |
| 45 | + if (OOB(nx, ny) || visited[nx][ny] || h >= arr[nx][ny]) continue; |
| 46 | + visited[nx][ny] = true; |
| 47 | + q.push({nx, ny}); |
| 48 | + } |
| 49 | + } |
| 50 | +} |
| 51 | + |
| 52 | +int main(){ |
| 53 | + ios_base::sync_with_stdio(false); |
| 54 | + cin.tie(NULL); cout.tie(NULL); |
| 55 | + |
| 56 | + cin >> N; |
| 57 | + for(int i = 0; i < N; i++){ |
| 58 | + for(int j = 0; j < N; j++){ |
| 59 | + cin >> arr[i][j]; |
| 60 | + minH = min(minH, arr[i][j]); |
| 61 | + maxH = max(maxH, arr[i][j]); |
| 62 | + } |
| 63 | + } |
| 64 | + |
| 65 | + // 각 높이마다 안전 영역 개수 |
| 66 | + for(int h = minH; h <= maxH; h++){ |
| 67 | + // 초기화 |
| 68 | + int cnt = 0; |
| 69 | + memset(visited, 0, sizeof(visited)); |
| 70 | + for(int i = 0; i < N; i++){ |
| 71 | + for(int j = 0; j < N; j++){ |
| 72 | + // 조건 제대로? |
| 73 | + if (h < arr[i][j] && !visited[i][j]) { |
| 74 | + bfs(i, j, h); // 안전영역 개수 세기 |
| 75 | + cnt++; |
| 76 | + } |
| 77 | + } |
| 78 | + } |
| 79 | + res = max(res, cnt); |
| 80 | + } |
| 81 | + cout << res; |
| 82 | + |
| 83 | + return 0; |
| 84 | +} |
0 commit comments