Notice
Recent Posts
Recent Comments
-
[DFS] 백준 2468번 - 안전영역 (정답률 33%) 본문
https://www.acmicpc.net/problem/2468
백준 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