Repository navigation
Expand file tree
/
Copy pathSolution1949.java
More file actions
64 lines (59 loc) · 1.96 KB
/
Copy pathSolution1949.java
File metadata and controls
64 lines (59 loc) · 1.96 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
package swea;
import java.util.*;
public class Solution1949 {
static int n, k;
static int[][] map;
static boolean[][] visited;
static int[] dx = { 0, 0, -1, 1 };
static int[] dy = { -1, 1, 0, 0 };
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int t = sc.nextInt();
for (int i = 1; i <= t; i++) {
n = sc.nextInt();
k = sc.nextInt();
map = new int[n][n];
int max = 0;
for (int j = 0; j < n; j++) {
for (int k = 0; k < n; k++) {
map[j][k] = sc.nextInt();
max = Math.max(max, map[j][k]);
}
}
int ans = 0;
for (int j = 0; j < n; j++) {
for (int k = 0; k < n; k++) {
if (map[j][k] == max) {
visited = new boolean[n][n];
ans = Math.max(ans, dfs(j, k, 0));
}
}
}
System.out.println("#" + i + " " + ans);
}
}
public static int dfs(int x, int y, int z) {
visited[x][y] = true;
int cnt = 0;
for (int i = 0; i < 4; i++) {
int nx = x + dx[i];
int ny = y + dy[i];
if (0 <= nx && nx < n && 0 <= ny && ny < n && !visited[nx][ny]) {
if (map[x][y] > map[nx][ny]) {
cnt = Math.max(cnt, dfs(nx, ny, z));
} else if (z == 0) {
if (map[nx][ny] - map[x][y] + 1 <= k) {
if (map[x][y] - 1 < 0)
continue;
int tmp = map[nx][ny];
map[nx][ny] = map[x][y] - 1;
cnt = Math.max(cnt, dfs(nx, ny, 1));
map[nx][ny] = tmp;
}
}
}
}
visited[x][y] = false;
return cnt + 1;
}
}