-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathMain.java
56 lines (44 loc) Β· 1.49 KB
/
Main.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
package DFS.P1937;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
public class Main {
static int N, K;
static int[][] map, dp;
static int[] di = {-1, 0, 1, 0};
static int[] dj = {0, 1, 0, -1};
public static void main(String[] args) throws Exception {
System.setIn(new FileInputStream("src/DFS/P1937/input.txt"));
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st;
N = Integer.parseInt(br.readLine());
map = new int[N][N];
for (int i = 0; i < N; i++) {
st = new StringTokenizer(br.readLine());
for (int j = 0; j < N; j++) map[i][j] = Integer.parseInt(st.nextToken());
}
dp = new int[N][N];
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
K = Math.max(K, dfs(i, j));
}
}
System.out.println(K);
}
static int dfs(int i, int j) {
if (dp[i][j] > 1) return dp[i][j];
dp[i][j] = 1;
for (int k = 0; k < 4; k++) {
int to_i = i + di[k];
int to_j = j + dj[k];
if (isValidPath(to_i, to_j) && map[i][j] < map[to_i][to_j]) {
dp[i][j] = Math.max(dp[i][j], dfs(to_i, to_j)+1);
}
}
return dp[i][j];
}
static boolean isValidPath(int i, int j) {
return 0 <= i && i < N && 0 <= j && j < N;
}
}