728x90
BFS로 풀었다.
#include <iostream>
#include <vector>
#include <queue>
#include <algorithm>
using namespace std;
int n;
vector<int> countAll;
int main() {
countAll.push_back(1);
cin >> n;
vector<vector<int>> height(n);
vector<vector<int>> rain(n);
for (int i = 0; i < n; i++) {
height[i] = vector<int>(n);
rain[i] = vector<int>(n);
}
int maxHeight = 0;
int minHeight = 101;
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
cin >> height[i][j];
if (height[i][j] > maxHeight) maxHeight = height[i][j];
if (height[i][j] < minHeight) minHeight = height[i][j];
}
}
for (int rainAmount = minHeight; rainAmount <= maxHeight; rainAmount++) {
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
if (height[i][j] <= rainAmount) {
rain[i][j] = 1;
}
}
}
int count = 0;
vector<vector<bool>> visited(n);
for (int i = 0; i < n; i++) {
visited[i] = vector<bool>(n);
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
if (!visited[i][j] && rain[i][j] == 0) {
count++;
queue<pair<int, int>> q;
q.push(make_pair(i, j));
while (!q.empty()) {
pair<int, int> cur = q.front();
q.pop();
if (!visited[cur.first][cur.second]) {
visited[cur.first][cur.second] = true;
if (cur.first + 1 < n && !visited[cur.first + 1][cur.second] && rain[cur.first + 1][cur.second] == 0) {
q.push(make_pair(cur.first + 1, cur.second));
}
if (cur.first - 1 >= 0 && !visited[cur.first - 1][cur.second] && rain[cur.first - 1][cur.second] == 0) {
q.push(make_pair(cur.first - 1, cur.second));
}
if (cur.second + 1 < n && !visited[cur.first][cur.second + 1] && rain[cur.first][cur.second + 1] == 0) {
q.push(make_pair(cur.first, cur.second + 1));
}
if (cur.second - 1 >= 0 && !visited[cur.first][cur.second - 1] && rain[cur.first][cur.second - 1] == 0) {
q.push(make_pair(cur.first, cur.second - 1));
}
}
}
}
}
}
countAll.push_back(count);
}
int maxCount = 0;
for (int i : countAll)
if (i > maxCount) maxCount = i;
cout << maxCount;
return 0;
}
728x90
'알고리즘 문제' 카테고리의 다른 글
[백준] 11651번 좌표 정렬하기 2 (0) | 2020.02.06 |
---|---|
[백준] 2583번 영역 구하기 (0) | 2020.02.06 |
[백준] 10026번 적록색약 (0) | 2020.02.06 |
[백준] 4153번 직각삼각형 (0) | 2020.02.05 |
[백준] 3009번 네 번째 점 (0) | 2020.02.05 |