Skip to content

Commit c20b610

Browse files
committed
Java Algorithm
1 parent 5d22e0f commit c20b610

File tree

2 files changed

+89
-0
lines changed

2 files changed

+89
-0
lines changed
Binary file not shown.

BOJ_JAVA/src/SWEA/Solution_1949.java

+89
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
package SWEA;
2+
3+
import java.io.BufferedReader;
4+
import java.io.IOException;
5+
import java.io.InputStreamReader;
6+
import java.util.StringTokenizer;
7+
8+
public class Solution_1949 {
9+
private static int N, K, maxLen, maxHigh;
10+
private static int[][] graph;
11+
private static boolean[][] visited;
12+
private static int[] dx = {-1 ,1, 0, 0};
13+
private static int[] dy = {0, 0, -1, 1};
14+
15+
private static void DFS(int x, int y, int h, int l, int cnt) {
16+
for (int dir = 0; dir < 4; dir++) {
17+
if (maxLen < l) maxLen = l;
18+
19+
int nx = x + dx[dir];
20+
int ny = y + dy[dir];
21+
22+
if (0 <= nx && nx < N && 0 <= ny && ny < N && !visited[nx][ny]) {
23+
if (h <= graph[nx][ny]) {
24+
if (cnt == 0) {
25+
if (h > graph[nx][ny] - K) {
26+
visited[nx][ny] = true;
27+
DFS(nx, ny, h - 1, l + 1, cnt + 1);
28+
visited[nx][ny] = false;
29+
}
30+
}
31+
} else {
32+
visited[nx][ny] = true;
33+
DFS(nx, ny, graph[nx][ny], l + 1, cnt);
34+
visited[nx][ny] = false;
35+
}
36+
}
37+
}
38+
}
39+
40+
41+
private static void Solution() {
42+
for (int i = 0; i < N; i++) {
43+
for (int j = 0 ;j < N; j++) {
44+
if (graph[i][j] == maxHigh) {
45+
visited[i][j] = true;
46+
DFS(i, j, maxHigh, 1, 0);
47+
visited[i][j] = false;
48+
}
49+
}
50+
}
51+
}
52+
53+
54+
55+
private static void input() throws IOException {
56+
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
57+
StringBuilder sb = new StringBuilder();
58+
int T = Integer.parseInt(br.readLine());
59+
60+
for (int tc = 1; tc <= T; tc++) {
61+
String[] line = br.readLine().split(" ");
62+
N = Integer.parseInt(line[0]);
63+
K = Integer.parseInt(line[1]);
64+
maxLen = maxHigh = 0;
65+
graph = new int[N][N];
66+
visited = new boolean[N][N];
67+
68+
69+
for (int i = 0 ; i < N; i++) {
70+
StringTokenizer st = new StringTokenizer(br.readLine());
71+
for (int j = 0; j < N; j++) {
72+
graph[i][j] = Integer.parseInt(st.nextToken());
73+
if (maxHigh < graph[i][j]) {
74+
maxHigh = graph[i][j];
75+
}
76+
}
77+
}
78+
Solution();
79+
sb.append("#").append(tc).append(" ").append(maxLen).append("\n");
80+
}
81+
System.out.println(sb.toString());
82+
br.close();
83+
84+
}
85+
86+
public static void main(String[] args) throws Exception {
87+
input();
88+
}
89+
}

0 commit comments

Comments
 (0)