Notice
Recent Posts
Recent Comments
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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
- Sort
- Set
- 2018
- 레벨3
- 프로그래머스
- 코딩테스트
- dp
- 모의SW역량테스트
- 백준
- 백트래킹
- C++
- priority_queue
- 레벨2
- substr
- Map
- find
- 문자열
- 삼성
- KAKAO
- 시뮬레이션
- swea
- 삼성SW테스트
- 삼성SW역량테스트
- 이런게4문제
- STL
- 코딩스킬
- 완전탐색
- dfs
- BFS
- 브루트포스
Archives
- Today
- Total
-
[DFS] 백준 2468번 - 안전영역 (정답률 33%) 본문
https://www.acmicpc.net/problem/2468
2468번: 안전 영역
재난방재청에서는 많은 비가 내리는 장마철에 대비해서 다음과 같은 일을 계획하고 있다. 먼저 어떤 지역의 높이 정보를 파악한다. 그 다음에 그 지역에 많은 비가 내렸을 때 물에 잠기지 않는 안전한 영역이 최대로 몇 개가 만들어 지는 지를 조사하려고 한다. 이때, 문제를 간단하게 하기 위하여, 장마철에 내리는 비의 양에 따라 일정한 높이 이하의 모든 지점은 물에 잠긴다고 가정한다. 어떤 지역의 높이 정보는 행과 열의 크기가 각각 N인 2차원 배열 형태로 주어
www.acmicpc.net
백준 DFS 연습문제다.
보통 이런 문제에 대해 한 가지 경우에 대한 문제는 50~60%정도 정답률을 보이지만,
이 문제와 같이 다양한 경우에 대해 dfs 탐색을 요구하는 문제는 정답률이 약 30%대로 떨어진다.
전자의 문제와 다를 바 없이, 매 조건에 대해 새롭게 방문여부 벡터를 초기화해주고, 조건에 맞게 세팅을 한 뒤에 dfs 탐색을 똑같이 하면 쉽게 풀 수 있다.
아래는 제출한 코드 (16ms)
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
|
#include <iostream>
#include <vector>
#include <algorithm>
#define INF 987654321
using namespace std;
int N;
int maxheight = -1;
int answer = -INF;
int cur_safety_fields = 0;
vector<vector<int> > height;
vector<vector<bool> > check;
typedef struct dir{
int dr, dc;
}dir;
dir direction[4] = { { 0, 1 }, { 0, -1 }, { 1, 0 }, { -1, 0 } };
void reset(){
for (int i = 0; i < N; i++)
for (int j = 0; j < N; j++)
check[i][j] = false;
}
void dfs(int r, int c){
for (int i = 0; i < 4; i++){
int nr = r + direction[i].dr;
int nc = c + direction[i].dc;
if (nr >= 0 && nr < N && nc >= 0 && nc < N){
if (!check[nr][nc]){
check[nr][nc] = true;
dfs(nr, nc);
}
}
}
}
int main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin >> N;
for (int i = 0; i < N; i++){
for (int j = 0; j < N; j++){
cin >> height[i][j];
if (height[i][j] > maxheight)
maxheight = height[i][j];
}
}
for (int limit = 0; limit <= maxheight; limit++){
//limit 이하 지역 모두 침수, 안전영역 구하기.
// 1. limit 이하 지역 모두 침수
for (int i = 0; i < N; i++){
for (int j = 0; j < N; j++){
if (height[i][j] <= limit){
check[i][j] = true;
}
}
}
// 2. 이제 안전영역의 개수를 구해보자.
for (int i = 0; i < N; i++){
for (int j = 0; j < N; j++){
if (!check[i][j]){
check[i][j] = true;
dfs(i, j);
cur_safety_fields++;
}
}
}
answer = max(answer, cur_safety_fields);
cur_safety_fields = 0;
reset();
}
cout << answer << '\n';
return 0;
}
http://colorscripter.com/info#e" target="_blank" style="color:#4f4f4ftext-decoration:none">Colored by Color Scripter
|
http://colorscripter.com/info#e" target="_blank" style="text-decoration:none;color:white">cs |
'3. DFS & 백트래킹' 카테고리의 다른 글
[백준] 2668번 - 숫자 고르기 (0) | 2020.02.07 |
---|---|
[DFS/백트래킹] 백준 17136번 - 색종이 붙이기 (0) | 2020.01.28 |
[DFS] 백준 2583번 - 영역 구하기 (정답률 56%) (0) | 2020.01.13 |
[DFS] 백준 2667번 - 단지번호붙이기 (정답률 38%) (0) | 2020.01.07 |
[DFS] 백준 1012번 - 유기농 배추 (정답률 34%) (0) | 2020.01.07 |
Comments