[백준] 2468 안전 영역

0

백준

목록 보기
224/271
post-thumbnail
post-custom-banner

[백준] 2468 안전 영역

#include <iostream>
#include <vector>
#include <algorithm>
#include <queue>
using namespace std;

int N;
int rainBoard[101][101] = { 0 };
int visited[101][101] = { 0 };

bool inRange(int r, int c) {
	if (r < 0 || r >= N) return false;
	if (c < 0 || c >= N) return false;
	return true;
}

int dirR[4] = { 0, 0, 1, -1 };
int dirC[4] = { 1, -1, 0, 0 };

void bfs(int startR, int startC) {
	queue<pair<int, int>> q;
	q.push({ startR, startC });

	while (!q.empty()) {
		int curR = q.front().first; 
		int curC = q.front().second;
		q.pop();

		if (visited[curR][curC]) continue;
		visited[curR][curC] = 1;

		//상하좌우 인접한 칸 bfs
		for (int d = 0; d < 4; ++d) {
			int nextR = curR + dirR[d];
			int nextC = curC + dirC[d];

			if (!inRange(nextR, nextC)) continue;
			if (visited[nextR][nextC]) continue;
			if (rainBoard[nextR][nextC] == 1) continue;

			q.push({ nextR, nextC });
		}
	}
	return;
}


int main() {
	ios_base::sync_with_stdio(false);
	cin.tie(NULL); cout.tie(NULL);

	int board[101][101] = { 0 };
	cin >> N;

	int maxH = -1;
	for (int i = 0; i < N; ++i) {
		for (int j = 0; j < N; ++j) {
			int h;
			cin >> h;
			board[i][j] = h;
			maxH = max(maxH, h);
		}
	}
	
	int maxCnt = -1;
	for (int rain = 0; rain <= maxH; ++rain) {
		//초기화
		for (int i = 0; i < 101; ++i) {
			for (int j = 0; j < 101; ++j) {
				rainBoard[i][j] = 0;
				visited[i][j] = 0;
			}
		}

		//물에 잠기는 지점 표시
		for (int i = 0; i < N; ++i) {
			for (int j = 0; j < N; ++j) {
				//비에 잠기는 지역 rainBoard[i][j] = 1
				if (board[i][j] <= rain) rainBoard[i][j] = 1;
			}
		}

		//물에 잠기지 않은 영역 개수 세기
		int cnt = 0;
		for (int i = 0; i < N; ++i) {
			for (int j = 0; j < N; ++j) {
				if ((rainBoard[i][j] == 0) && (!visited[i][j])){
					bfs(i, j);
					cnt++;
				}
			}
		}
		maxCnt = max(maxCnt, cnt);
	}
	
	cout << maxCnt;
	return 0;
}

profile
Be able to be vulnerable, in search of truth
post-custom-banner

0개의 댓글