forked from jxixeun/jxix_java_algorithm
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBOJ1937.java
More file actions
55 lines (51 loc) · 1.63 KB
/
Copy pathBOJ1937.java
File metadata and controls
55 lines (51 loc) · 1.63 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
package baekjoon;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
public class BOJ1937 {
static int [][] map;
static int [][] dp;
static int N;
static int[] dx = {1, -1, 0, 0};
static int[] dy = {0, 0, -1, 1};
static int answer = Integer.MIN_VALUE;
static int dfs(int x, int y){
dp[x][y]=1;
for (int i = 0; i < 4; i++) {
int nx = x + dx[i];
int ny = y + dy[i];
if (nx >= 0 && ny >= 0 && nx < N && ny < N && map[x][y] < map[nx][ny]){
if (dp[nx][ny] == -1){
dp[x][y] = Math.max(dfs(nx, ny)+1, dp[x][y]);
} else {
dp[x][y] = Math.max(dp[nx][ny]+1, dp[x][y]);
}
}
}
answer = Math.max(dp[x][y], answer);
return dp[x][y];
}
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
N = Integer.parseInt(br.readLine());
StringTokenizer st;
map = new int[N][N];
dp = 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[i][j] = -1;
}
}
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
if (dp[i][j]==-1){
dfs(i, j);
}
}
}
System.out.println(answer);
}
}