Skip to content

Commit 9f6aecc

Browse files
authored
[20260218] BOJ / G3 / 사다리 조작 / 이준희
1 parent a4cff52 commit 9f6aecc

File tree

1 file changed

+71
-0
lines changed

1 file changed

+71
-0
lines changed
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
```java
2+
import java.io.*;
3+
import java.util.*;
4+
5+
public class Main {
6+
static int N, M, H, answer;
7+
static int[][] map;
8+
static boolean finish = false;
9+
10+
public static void main(String[] args) throws IOException {
11+
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
12+
StringTokenizer st = new StringTokenizer(br.readLine());
13+
14+
N = Integer.parseInt(st.nextToken());
15+
M = Integer.parseInt(st.nextToken());
16+
H = Integer.parseInt(st.nextToken());
17+
18+
map = new int[H + 1][N + 1];
19+
20+
for (int i = 0; i < M; i++) {
21+
st = new StringTokenizer(br.readLine());
22+
int a = Integer.parseInt(st.nextToken());
23+
int b = Integer.parseInt(st.nextToken());
24+
map[a][b] = 1;
25+
}
26+
27+
for (int i = 0; i <= 3; i++) {
28+
answer = i;
29+
dfs(1, 0);
30+
if (finish) break;
31+
}
32+
33+
System.out.println(finish ? answer : -1);
34+
}
35+
36+
static void dfs(int startR, int count) {
37+
if (finish) return;
38+
if (answer == count) {
39+
if (check()) finish = true;
40+
return;
41+
}
42+
43+
for (int i = startR; i <= H; i++) {
44+
for (int j = 1; j < N; j++) {
45+
if (map[i][j] == 1) continue;
46+
if (map[i][j - 1] == 1) continue;
47+
if (j + 1 <= N && map[i][j + 1] == 1) continue;
48+
49+
map[i][j] = 1;
50+
dfs(i, count + 1);
51+
map[i][j] = 0;
52+
}
53+
}
54+
}
55+
56+
static boolean check() {
57+
for (int i = 1; i <= N; i++) {
58+
int currentPos = i;
59+
for (int j = 1; j <= H; j++) {
60+
if (map[j][currentPos] == 1) {
61+
currentPos++;
62+
} else if (map[j][currentPos - 1] == 1) {
63+
currentPos--;
64+
}
65+
}
66+
if (currentPos != i) return false;
67+
}
68+
return true;
69+
}
70+
}
71+
```

0 commit comments

Comments
 (0)