๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
๋ฐฑ์ค€/Gold

[BOJ] 1937 : ์š•์‹ฌ์Ÿ์ด ํŒ๋‹ค

by Jaeguk 2022. 3. 1.
๋ฌธ์ œ ๋งํฌ

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<intint>int>vector<pair<pair<intint>int>>, greater<pair<pair<intint>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<intint> before, pair<intint> 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

์ด๋ ‡๊ฒŒ ํ•˜๋ฉด ์ค‘๋ณต์„ ํ”ผํ•ด ๋ชจ๋“  ์ง€์ ์„ ํƒ์ƒ‰ํ•  ์ˆ˜ ์žˆ๋‹ค.

 

728x90