반응형

문제 출처 :


https://swexpertacademy.com/main/code/problem/problemDetail.do?contestProbId=AV5PoOKKAPIDFAUq



알고리즘 분석 :


문제 해결에 필요한 사항

1. BFS

2. DFS


등산로 조성 문제는 N 제한이 작기 때문에 VISIT가 없는 완전 탐색으로 해결 할 수 있다.


BFS에 대한 해설은 아래 주석을 통해 달아두었고 이 문제는 DFS로 푸는 것이 정석적인 듯 하다.


(BFS로 풀기 위해서는 너무많은 for문이 필요)







소스 코드 : 


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
 
#include <iostream>
#include <cstdio>
#include <queue>
#include <algorithm>
 
using namespace std;
 
typedef pair<intint> pii;
 
int dy[4= { -1,0,1,};
int dx[4= { 0,-1,0,};
int main() {
    freopen("input.txt""r", stdin);
    int tCase;
    scanf("%d"&tCase);
 
    for (int tc = 1; tc <= tCase; tc++) {
 
        int arr[10][10= { 0, };
 
        int n, k;
        scanf("%d %d"&n, &k);
 
        int maxH = -1;
        for (int i = 0; i < n; i++)
            for (int j = 0; j < n; j++) {
                scanf("%d"&arr[i][j]);
                maxH = max(maxH, arr[i][j]);
            }
 
        int ans = 0;
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < n; j++) {
                if (arr[i][j] == maxH) { // 최고점을 만났을 때
                    
                    for (int a = 0; a < n; a++) { // a,b를 깎는다고 생각
                        for (int b = 0; b < n; b++) {
                            for (int t = 0; t <= k; t++) { // a,b를 0~k만큼 다깎아본다.
                                arr[a][b] -= t;
                                queue<pii> q;
                                q.push({ i,j });
 
                                int dist[10][10= { 0, };
                                dist[i][j] = 1;
                                
                                while (!q.empty()) {
                                    int y = q.front().first;
                                    int x = q.front().second;
 
                                    q.pop();
 
                                    for (int w = 0; w < 4; w++) {
                                        int ny = y + dy[w];
                                        int nx = x + dx[w];
 
                                        if (<= ny && ny < n && <= nx && nx < n && arr[y][x] > arr[ny][nx]) {
                                            dist[ny][nx] = dist[y][x] + 1;
                                            ans = max(ans, dist[ny][nx]); // 최장거리 갱신
                                            q.push({ ny,nx });
                                        }
                                    }
                                }
 
                                arr[a][b] += t;
                            }
                        }
                    }
 
                }
            }
        }
        printf("#%d %d\n", tc, ans);
    }
    return 0;
}
 
cs

반응형

'Applied > 알고리즘 문제풀이' 카테고리의 다른 글

[1263번] 사람 네트워크2  (0) 2019.06.20
[11060번] 점프 점프  (0) 2019.06.15
[1265번] 달란트2  (0) 2019.06.10
[12174번] #include <Google I/O.h>  (0) 2019.06.08
[1244번] 최대 상금  (2) 2019.06.05