-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBOJ1937.java
More file actions
65 lines (57 loc) · 1.93 KB
/
BOJ1937.java
File metadata and controls
65 lines (57 loc) · 1.93 KB
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
package quki.algorithm.dp;
import java.util.Scanner;
public class BOJ1937 {
static int dx[] = {0, 0, 1, -1};
static int dy[] = {1, -1, 0, 0};
/**
* @return d[i][j]: 해당 지점에 도달하는 데 최대 경로 수.
*/
public static int go(int a[][], int d[][], int i, int j) {
int n = a[0].length - 1;
if (d[i][j] != 0)
return d[i][j];
for (int k = 0; k < 4; k++) {
int newI = i + dy[k];
int newJ = j + dx[k];
if (newI >= 1 && newI <= n && newJ >= 1 && newJ <= n) {
if (a[i][j] > a[newI][newJ]) {
int t2 = go(a, d, newI, newJ) + 1; // depth 1증가(현재 위치)
d[i][j] = Math.max(d[i][j], t2);
}
}
}
// 더 이상 갈 곳이 없는 지점(말단)에서 return을 하는데
// 이때, 현재 위치의 값이 0인 경우(해당 지점에 처음 온 경우)에는 1로 만들어 return 한다.
if (d[i][j] == 0)
return d[i][j] = 1;
return d[i][j];
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int a[][] = new int[n + 1][n + 1];
int d[][] = new int[n + 1][n + 1];
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
a[i][j] = sc.nextInt();
}
}
int max = 0;
//d[i][j]: 해당 지점에 도달하는 데 최대 경로 수.
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
if (d[i][j] == 0)
max = Math.max(max, go(a, d, i, j));
}
}
StringBuffer sb = new StringBuffer();
for (int i = 1; i <= n; i++) {
sb.append("\n");
for (int j = 1; j <= n; j++) {
sb.append(d[i][j] + " ");
}
}
System.out.println(sb);
System.out.println(max);
}
}