[swea-D2] 25052. 등산로
SWEA 등산로 문제를 JAVA를 이용해 완전탐색으로 범위 조건문을 통해 해결하는 방법으로 설명합니다.
🙋♂️ 들어가며
조건을 읽어보면 가장 중요한 내용이 있다
- 더 낮은 영역이 여러개인 경우, 그 중 가장 낮은 곳으로 이동할 것
✅ 코드
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
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.BufferedReader;
public class Solution {
static int[] dr = {-1,1,0,0};
static int[] dc = {0,0,-1,1};
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int T = Integer.parseInt(br.readLine());
for (int tc = 1; tc < T+1; tc++) {
int N = Integer.parseInt(br.readLine());
int[][] arr = new int[N][N];
// 1. 배열 생성
for (int r = 0; r < N; r++) {
String[] cols = br.readLine().split(" ");
for (int c = 0; c < N; c++) {
arr[r][c] = Integer.parseInt(cols[c]);
}
}
// 2. 검사 (완전탐색)
int max_leng = 1;
for (int r = 0; r < N; r++) {
for (int c = 0; c < N; c++) {
int cr = r;
int cc = c;
int cur_leng = 1;
// 2-1. 낮은 영역이 여러개면 가장 낮은 곳으로
while (true) {
int destionation_r = -1;
int destionation_c = -1;
int min_height = Integer.MAX_VALUE;
for (int d = 0; d < 4; d++) {
int nr = cr + dr[d];
int nc = cc + dc[d];
// 2-2. 범위 초과시 -> skip
if (nr < 0 || nr >= N || nc < 0 || nc >= N) continue;
// 2-3. 더 낮은 곳 발견시
if (arr[nr][nc] < arr[cr][cc]) {
if (arr[nr][nc] < min_height) {
min_height = arr[nr][nc];
destionation_r = nr;
destionation_c = nc;
}
}
}
// 2-4. 이동 불가능하면 break
if (destionation_r == -1 && destionation_c == -1) break;
// 2-5. 이동 가능하면
cr = destionation_r;
cc = destionation_c;
cur_leng++;
}
// 3. 값 비교
max_leng = Math.max(max_leng, cur_leng);
}
}
// 4. 출력
System.out.println("#" + tc + " " + max_leng);
}
}
}
This post is licensed under CC BY 4.0 by the author.
