Baekjoon

백준 #2468 [c++]

PON_Z 2021. 8. 31. 20:48

#include <iostream>
#include <vector>
#include <queue>

#define MAX 101

using namespace std;

int n;

bool visit[MAX][MAX];
int map[MAX][MAX];

int dir_y[4] = { 1,-1,0,0 };
int dir_x[4] = { 0,0,1,-1 };


void init() {
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
visit[i][j] = false;
}
}
}

void bfs(int y, int x, int k) {
queue<pair<int, int>> q;
q.push(make_pair(y, x));
visit[y][x] = true;

while (!q.empty()) {
int yy = q.front().first;
int xx = q.front().second;
q.pop();

for (int i = 0; i < 4; i++) {
int next_y = yy + dir_y[i];
int next_x = xx + dir_x[i];
if (next_y >= 0 && next_x >= 0 && next_y < n && next_x < n) {
if (!visit[next_y][next_x] && map[next_y][next_x] >= k) {
q.push(make_pair(next_y, next_x));
visit[next_y][next_x] = true;
}
}
}
}
}


int main() {
int count = 0; // 안전지역 세는 변수
int index = 0; // 1부터 maxHeight까지의 경우를 가리키는 인덱스
int maxHeight = 0; // 가장 높은 건물의 높이
int arr[MAX];
cin >> n;

for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
cin >> map[i][j];
if (map[i][j] > maxHeight) maxHeight = map[i][j];
visit[i][j] = false;
}
}
for (int k = 0; k <= maxHeight; k++) {
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
if (!visit[i][j] && map[i][j] >= k) {
bfs(i, j, k);
count++;
}
}
}
arr[index] = count;
index++;
count = 0;
init();
}

for (int i = 0; i < index; i++) {
if (count < arr[i]) count = arr[i];
}

cout << count;


return 0;
}

728x90

'Baekjoon' 카테고리의 다른 글

백준 #2156 [c++]  (0) 2022.01.25
백준 #4963 [c++]  (0) 2021.08.30
백준 #14502 [c++]  (0) 2021.08.28
백준 #1157 [c++]  (0) 2021.08.05
백준 #1697 [c++]  (0) 2021.08.02