๋ฌธ์ ๋งํฌ
1937๋ฒ: ์์ฌ์์ด ํ๋ค (acmicpc.net)
ํ์ด
๋ฌธ์ ๋ฅผ ๋ณด๋ฉด ์ผ๋จ DFS๋ฌธ์ ๋ผ๋ ๊ฒ์ ์ ์ ์๋ค.
์์์ ์์ ์ํ์ข์ฐ ์ค ๋ฒ์๋ฅผ ๋ฒ์ด๋์ง ์์ผ๋ฉด์ ํ์ฌ ์ง์ ๋ณด๋ค ๊ฐ์ด ํฌ๋ฉด ๊ทธ ์ง์ ์ ๋ฐฉ๋ฌธํ๋ค.
๊ทธ๋ ๊ฒ ํด์ ๋ ์ด์ ์ด๋ํ ์ ์์๋ ๊น์ง ๋๋ฌํ์ ๋ ๊ทธ ๊น์ด ์ค ๊ฐ์ฅ ํฐ ๊ฐ์ ์ฐพ๋ ๋ฌธ์ ์ด๋ค.
๊ทธ๋ฐ๋ฐ ๋ชจ๋ ์ ์์๋ถํฐ ์ถ๋ฐํด์ ์ต๋๊ฐ์ ์ฐพ์ผ๋ฉด ๋ ๊น?? ๋น์ฐํ ์๋๋ค. ๊ทธ๋ ๊ฒ ํ๊ฒ๋๋ฉด ์ด๋ฏธ ๋ฐฉ๋ฌธํ๋ ์ง์ ์ ์์์ ์ผ๋ก ๊ฐ์ ๊ฒฝ๋ก๋ฅผ ์ฌํ์ ํ๊ฒ๋๋ ๊ฒฝ์ฐ๊ฐ ๋ฐ์ํ๊ธฐ ๋๋ฌธ์ ์๊ฐ์ด๊ณผ๊ฐ ๋๋ค.
๊ทธ๋ผ ์ด๋ป๊ฒ ํด์ผํ ๊น? DP๋ฅผ ์ด์ฉํด์ ์ฌ๋ฐฉ๋ฌธ์ ํ์ง ์๋๋ก ํด์ผํ๋ค.
์์ ๋ฅผ ๋ณด์
4
14 9 12 10
1 11 5 4
7 15 2 13
6 3 16 8
(1,1)์ ์ถ๋ฐ์ ์ผ๋ก ํ์ ๋ ์ฃผ๋ณ์ ์์ (14)๋ณด๋ค ํฐ ๊ฐ์ด ์์ผ๋ฏ๋ก ๋ ์ด์ ์ด๋์ด ๋ถ๊ฐํ๋ค.
๋ฐ๋ผ์ DP[1][1] = 1
๋ค์์ผ๋ก 9 ( 0, 1 )๋ฅผ ์ถ๋ฐ์ ์ผ๋ก ์ดํด๋ณด์ 9->11->15์ ๊ฒฝ๋ก๋ก ์ด๋ํ์ ๋ ๊ฐ์ฅ ๋ง์ ์นธ์ ์ด๋ํ๊ฒ ๋๋ค.
์ด ํ์์ผ๋ก ํ ๋ฒ์ 3๊ฐ ์ง์ ์ ์ต๋ ์ด๋ ์นธ ์๋ฅผ ์ ์ ์๋ค.
9์์๋ ์ต๋ 2ํ ์ด๋ ํ ์ ์๊ณ , 11์์๋ ์ต๋ 1ํ ์ด๋ ํ ์ ์๊ณ , 15์์๋ ์ด๋ํ ์ ์๋ค๋ ๊ฒ์ ์๋ฏธํ๋ค.
๋์ ๋ฐฉ๋ฒ์ ์ฌ๊ท๋ฅผ ์ด์ฉํด์ ๊ฐ ์ ์๋ ์ต๋์ง์ ๊น์ง ๊ฐ ํ์ 15์ ์ขํ์ธ DP[2][1] ์ 1์ ํ ๋นํ๊ณ ํจ์๋ฅผ ๋น ์ ธ๋๊ฐ๋ฉด์ 15์ ์ด์ ๋ฐฉ๋ฌธ ์ง์ ์ธ 11์ ์ขํ๊ฐ (1,1)์ด๋ฏ๋ก DP[1][1] = DP[2][1] + 1 ์ ํ ๋นํด์ฃผ๊ณ ๋๋ค์ ํจ์๋ฅผ ๋น ์ ธ๋๊ฐ๋ฉด์ 11์ ์ด์ ์ง์ 9์ ์ขํ DP[0][1] = DP[1][1] + 1 = (2 + 1)์ ๊ฐ์ ํ ๋นํด์ฃผ์๋ค. ์ด๋ 0,1์์ ์ถ๋ฐํ๋ฉด ์ต๋ 3์นธ ๋ฐฉ๋ฌธํ ์ ์๋ค๋ ๋ป์ด๋ค.
๊ทธ๋ฆฌ๊ณ DFS๋ก ํ์ํ๋ ๋์ค ์ด๋ฏธ ๋ฐฉ๋ฌธํ๋ ์ง์ ์ด๋ผ๋ฉด DP๋ฐฐ์ด์ ๊ฐ์ด ๋ค์ด์์ ๊ฒ์ด๋ค. ๊ทธ๋ผ ๊ทธ ์ง์ ๋ถํฐ ๊ฐ ์ ์๋ ํ์๋ ์ด๋ฏธ ์๊ณ ์์ผ๋ฏ๋ก ๊ทธ ๋งํผ ๋ํด์ค ํ ํจ์๋ฅผ ์ข ๋ฃํ๋ค.
์๋ฅผ ๋ค์ด ๋ค๋ฅธ ์ถ๋ฐ์ ์ผ๋ก๋ถํฐ ๊ฒฝ๋ก๋ฅผ ํ์ํ๋ค๊ฐ ์๊น ๋ฐฉ๋ฌธํ 11์ ์ขํ (1,1)์ ๋๋ฌํ๋ค๊ณ ํ์ ๊ทธ๋ผ DP[1][1]์๋ 2๊ฐ ์ ์ฅ๋์ด ์์ ๊ฒ์ด๊ณ ์ด๊ฒ์ผ๋ก ์ฌ๊ธฐ์๋ ์ต๋ 1ํ ๋ ์ด๋ํ ์ ์๋ค๋ ๊ฒ์ ์์๋์ผ๋๊น ํ์์ ๊ทธ๋งํด๋ ๋๋ค.
๊ทธ๋ผ 11 ๋ฐฉ๋ฌธ ์ด์ ์ขํ๋ฅผ (y,x)๋ผ๊ณ ํ๋ฉด DP[y][x] = DP[1][1] + 1์ด๋ค.
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
81
82
83
84
85
86
87
|
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <cstdio>
#include <string.h>
#include <string>
#include <stack>
#include <vector>
#include <math.h>
#include <queue>
#include <climits>
#include <algorithm>
#include <iomanip>
#include <map>
#define mod 1000000007
using namespace std;
typedef long long ll;
typedef priority_queue<pair<pair<int, int>, int>, vector<pair<pair<int, int>, int>>, greater<pair<pair<int, int>, int>>> Priority_queue;
const int INF = INT_MAX;
const int MAX_N = 500 + 5;
int DP[MAX_N][MAX_N];
int Forest[MAX_N][MAX_N];
int N;
int dy[] = { 0,1,0,-1 };
int dx[] = { 1,0,-1,0 };
int Max = 1;
bool is_ok(int y, int x, int num) {
if (y < 0 || y > N || x <0 || x > N || num >= Forest[y][x])
return false;
return true;
}
void dfs(pair<int, int> before, pair<int, int> now) {
int tree = Forest[now.first][now.second];
bool chk = false;
if (DP[now.first][now.second] != -1) {
DP[before.first][before.second] = max(DP[before.first][before.second],
DP[now.first][now.second] + 1);
Max = max(Max, DP[before.first][before.second]);
return;
}
for (int i = 0; i < 4; i++) {
int next_y = now.first + dy[i];
int next_x = now.second + dx[i];
if (is_ok(next_y, next_x, tree)) {
chk = true;
dfs(now, { next_y, next_x });
}
}
if (!chk) {
DP[now.first][now.second] = 1;
if (now != before) {
DP[before.first][before.second] = max(DP[before.first][before.second],
DP[now.first][now.second] + 1);
Max = max(Max, DP[before.first][before.second]);
}
return;
}
if (now == before)
return;
DP[before.first][before.second] = max(DP[before.first][before.second],
DP[now.first][now.second] + 1);
Max = max(Max, DP[before.first][before.second]);
}
int main(void) {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cin >> N;
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++)
DP[i][j] = -1;
}
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
cin >> Forest[i][j];
}
}
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
if (DP[i][j] == -1) {
dfs({ i,j }, { i,j });
}
}
}
cout << Max << "\n";
return 0;
}
|
cs |
์ด๋ ๊ฒ ํ๋ฉด ์ค๋ณต์ ํผํด ๋ชจ๋ ์ง์ ์ ํ์ํ ์ ์๋ค.
'๋ฐฑ์ค > Gold' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[BOJ] 2011 : ์ํธ์ฝ๋ (0) | 2022.03.03 |
---|---|
[BOJ] 2225 : ํฉ๋ถํด (0) | 2022.03.02 |
[BOJ] 2133 : ํ์ผ ์ฑ์ฐ๊ธฐ (0) | 2022.03.01 |
[BOJ] 13392 : ๋ฐฉ๋ฒ์ ์ถ๋ ฅํ์ง ์๋ ์ซ์ ๋ง์ถ๊ธฐ (0) | 2022.02.25 |
[BOJ] 2169 : ๋ก๋ด ์กฐ์ข ํ๊ธฐ (0) | 2022.02.24 |