반응형
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 |
Tags
- navmesh
- 스프레드시트 사용법
- unity 받기
- 한달리뷰
- 백준
- 유니티
- 알고리즘
- 진수 변환기
- 스프레드시트
- 라이더
- navmeshagent
- 다른 시트값
- 무장cg추가하기
- rider 설치
- 유니티 해상도 고정
- 스프레드 시트
- 엑셀 내보내기
- Rider
- C#
- unity
- monocraft
- unity 구버전
- git
- ilcode
- 테크스트림
- ilviewer
- 다각형 중점
- 엑셀 가져오기
- Mac
- cmd키 변경
Archives
- Today
- Total
코스모스 공작소
백준 2468 안전영역 (bfs) [성공] 본문
반응형
https://www.acmicpc.net/problem/2468
2468번: 안전 영역
재난방재청에서는 많은 비가 내리는 장마철에 대비해서 다음과 같은 일을 계획하고 있다. 먼저 어떤 지역의 높이 정보를 파악한다. 그 다음에 그 지역에 많은 비가 내렸을 때 물에 잠기지 않는 안전한 영역이 최대로 몇 개가 만들어 지는 지를 조사하려고 한다. 이때, 문제를 간단하게 하기 위하여, 장마철에 내리는 비의 양에 따라 일정한 높이 이하의 모든 지점은 물에 잠긴다고 가정한다. 어떤 지역의 높이 정보는 행과 열의 크기가 각각 N인 2차원 배열 형태로 주어
www.acmicpc.net
이 문제는 다 풀어놓고 한가지 기준을 생각을 못해 꽤 시간이 걸렸던 문제
bfs dfs 둘다 풀이가 가능하다.
하지만 나는 bfs로 풀어보겠다.
영역을 그리고 다시 탐색하지 않는 과정으로 조금 더 단축시켜 진행하겠다.
#include <iostream>
#include<vector>
#include<queue>
#include<algorithm>
using namespace std;
vector<vector<int>> list;
vector<vector<int>> list_rain;
queue<pair<int,int>> q;
vector<int> count_list;
int dx[4] = { 1,0,-1,0 };
int dy[4] = { 0,1,0,-1 };
int min_ = 10;
int max_ = 0;
int n;
void init_list() {
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
list_rain[i][j] = 0;
}
}
}
void bfs(int depth) {
while (!q.empty())
{
pair<int, int> current = make_pair(q.front().first, q.front().second);
q.pop();
int y = current.second;
int x = current.first;
for (int w = 0; w < 4; w++) {
if (x + dx[w] < 0 || y + dy[w] < 0 || y + dy[w] >= n || x + dx[w] >= n) {
continue;
}
else {
if (list[y + dy[w]][x + dx[w]] > depth&& list_rain[y + dy[w]][x + dx[w]] == 0) {
q.push(make_pair(x + dx[w], y + dy[w]));
list_rain[y + dy[w]][x + dx[w]] = 1;
}
}
}
}
}
void solution() {
for (int depth = 0; depth <= max_; depth++) {
int count = 0;
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
if (list[i][j] > depth && list_rain[i][j] == 0) {
pair<int, int> x_y = make_pair(j, i);
q.push(x_y);
list_rain[i][j] = 1;
count++;
bfs(depth);
}
}
}
init_list();
count_list.push_back(count);
}
}
int max_count() {
int result=0;
for (int i = 0; i < count_list.size(); i++) {
if (result <= count_list[i]) {
result = count_list[i];
}
}
return result;
}
int main() {
cin >> n;
int temp;
vector<int> temp_int;
vector<int> temp_list_int;
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
cin >> temp;
temp_int.push_back(temp);
if (min_ >= temp) {
min_ = temp;
}
if (max_ <= temp) {
max_ = temp;
}
temp_list_int.push_back(0);
}
list.push_back(temp_int);
list_rain.push_back(temp_list_int);
temp_int.clear();
temp_list_int.clear();
}
solution();
cout << max_count() << endl;
return 0;
}
bfs 연습하기에는 정말 좋았던 문제
반응형
'프로그래밍 > 알고리즘' 카테고리의 다른 글
백준 1654 랜선 자르기 (성공) (0) | 2020.01.20 |
---|---|
힙(heap) 과 힙정렬, 그리고 우선순위 큐 (0) | 2019.12.18 |
백준 11047번 동전 0 [성공] (0) | 2019.12.08 |
백준 11399번 ATM [성공] (0) | 2019.12.06 |
백준 1145번 RGB거리 [성공] (0) | 2019.12.01 |
Comments