Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | ||
6 | 7 | 8 | 9 | 10 | 11 | 12 |
13 | 14 | 15 | 16 | 17 | 18 | 19 |
20 | 21 | 22 | 23 | 24 | 25 | 26 |
27 | 28 | 29 | 30 | 31 |
Tags
- 문제풀이
- 정석학술정보관
- vector
- Stack
- 개발
- 스택
- c++
- DP
- 너비우선탐색
- Operating System
- 브루트포스
- 컴퓨터공학과
- 북리뷰
- 오에스
- 정석
- 자료구조
- 그래프
- 컴공과
- 코테
- 코딩
- 오퍼레이팅시스템
- 구현
- OS
- 알고리즘
- 컴공
- coding
- bfs
- cs
- Computer science
- 백준
Archives
- Today
- Total
Little Jay
[C++] 백준 2468번 - 안전영역 본문
문제 조건에
아무 지역도 물에 잠기지 않을 수도 있다.
이 말을 듣고 음 그런군..... 이라고 생각을 했었는데
이것때문에 계속 틀렸다;;;
이 말은 즉슨 level이 0부터 시작될 수도 있다는거다
#include <iostream>
#include <queue>
#include <algorithm>
#include <vector>
#include <cstring>
using namespace std;
int n, safe, level;
int map[101][101];
bool visited[101][101];
int map_copy[101][101];
vector<int> v;
const int dx[] = { 1, -1, 0, 0 };
const int dy[] = { 0, 0, 1, -1 };
void copy(int flood_level) {
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
if (map[i][j] > flood_level)
map_copy[i][j] = 1;
}
}
}
void bfs(int y, int x) {
queue<pair<int, int>> q;
visited[y][x] = true;
q.push({ y, x });
while (!q.empty()) {
auto current = q.front();
q.pop();
for (int dir = 0; dir < 4; dir++) {
int nx = current.second + dx[dir];
int ny = current.first + dy[dir];
if (nx < 0 || ny < 0 || nx >= n || ny >= n) continue;
if (visited[ny][nx] || map_copy[ny][nx] == 0) continue;
q.push({ ny, nx });
visited[ny][nx] = true;
}
}
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cin >> n;
int max_level = 0;
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
cin >> map[i][j];
if (max_level < map[i][j])
max_level = map[i][j];
}
}
for (int k = 0; k <= max_level; k++) {
copy(k);
int count = 0;
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
if (map_copy[i][j] && !visited[i][j]) {
bfs(i, j);
count++;
}
}
}
v.emplace_back(count);
memset(map_copy, 0, sizeof(map_copy));
memset(visited, false, sizeof(visited));
}
sort(v.begin(), v.end());
cout << v[v.size() - 1] << '\n';
return 0;
}
'알고리즘 > BOJ' 카테고리의 다른 글
[C++] 백준 7662번 - 이중 우선순위 큐(multiset) (0) | 2022.01.13 |
---|---|
[C++] 백준 1302번 - 베스트셀러 (0) | 2022.01.11 |
[C++] 백준 5076번 - Web Pages (0) | 2021.11.16 |
[C++] 백준 7562번 - 나이트의 이동 (0) | 2021.11.15 |
[C++] 백준 2178번 - 미로 탐색 (0) | 2021.11.12 |
Comments