문제 링크
https://www.acmicpc.net/problem/2468
2468번: 안전 영역
재난방재청에서는 많은 비가 내리는 장마철에 대비해서 다음과 같은 일을 계획하고 있다. 먼저 어떤 지역의 높이 정보를 파악한다. 그 다음에 그 지역에 많은 비가 내렸을 때 물에 잠기지 않는
www.acmicpc.net
풀이 전략
가능한 모든 수위에 대해 물에 잠기는 지점과 안전한 지점을 표시한다. 서로 연결된 안전한 지점을 BFS를 통해 찾는다. BFS가 호출된 수가 안전 영역의 개수이다.
코드
#include <iostream>
#include <vector>
#include <queue>
using namespace std;
int board[101][101];
int land[101][101]; //-1 : 침수, 1 : 안전지역
int N;
int mmax;
int answer = 0;
void BFS(const int y, const int x);
void Solve() {
mmax = 0;
answer = 0;
cin >> N;
for (int i = 0; i < N; ++i) {
for (int j = 0; j < N; ++j) {
int temp;
cin >> temp;
board[i][j] = temp;
mmax = max(mmax, temp);
}
}
/*
수위 : 0 ~ 최고높이 - 1 탐색
가능한 모든 수위에 대해 물에 잠기는 지역과 안전한 지역을 표시
*/
for(int w = 0; w < mmax; ++w) { //w : water level
for (int i = 0; i < N; ++i) {
for (int j = 0; j < N; ++j) {
land[i][j] = board[i][j] > w ? 1 : -1;
}
}
int cur = 0;
for(int i = 0; i < N; ++i) {
for (int j = 0; j < N; ++j) {
if (land[i][j] == 1) { //이미 visit -> 0으로 표시
BFS(i, j);
cur++;
}
}
}
answer = cur > answer ? cur : answer;
}
cout << answer << '\n';
}
int main() {
Solve();
return 0;
}
void BFS(const int y, const int x) {
const int dy[] = { -1,1,0,0 }; //상하좌우
const int dx[] = { 0,0,-1,1 };
land[y][x] = 0;
queue<pair<int, int>> q;
q.push({ y, x });
while (!q.empty()) {
int popCnt = q.size();
while(popCnt--) {
int cy = q.front().first;
int cx = q.front().second;
q.pop();
for(int dir = 0; dir < 4; ++dir) { /*direction : 상하좌우*/
int ny = cy + dy[dir];
int nx = cx + dx[dir];
if (ny < 0 || ny > N - 1 || nx < 0 || nx > N - 1 || land[ny][nx] != 1) {
continue;
}
land[ny][nx] = 0;
q.push({ ny, nx });
}
}
}
}
'algorithm & data structure' 카테고리의 다른 글
[프로그래머스] 네트워크 / DFS, BFS 그래프 탐색 / C++ 풀이 (0) | 2023.08.04 |
---|---|
[프로그래머스] 단속카메라 / 탐욕법, Greedy / C++ 풀이 (0) | 2023.07.31 |
[프로그래머스] 선입 선출 스케줄링 / 이분탐색, binary search / C++ 풀이 (0) | 2023.07.29 |
[프로그래머스] 등굣길 / DP, Dynamic Programming (0) | 2023.07.24 |
[프로그래머스] 연속 펄스 부분 수열의 합 / Prefix Sum, 구간 합 (0) | 2023.07.19 |