-
Notifications
You must be signed in to change notification settings - Fork 0
/
사다리 조작.java
84 lines (70 loc) · 2.26 KB
/
사다리 조작.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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
import java.io.*;
import java.util.*;
public class Main {
static int N, M, H;
static int[][] ladder;
static StringTokenizer st;
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringBuilder sb = new StringBuilder();
st = new StringTokenizer(br.readLine());
N = Integer.parseInt(st.nextToken());
M = Integer.parseInt(st.nextToken());
H = Integer.parseInt(st.nextToken());
ladder = new int[H + 1][N + 1];
for (int i = 0; i < M; i++) {
st = new StringTokenizer(br.readLine());
int r = Integer.parseInt(st.nextToken());
int c = Integer.parseInt(st.nextToken());
ladder[r][c] = 1;
}
for (int i = 0; i <= 3; i++) {
comb(1, 0, i);
}
System.out.println(-1);
}
static void comb(int nr, int cnt, int size) {
if (cnt == size) {
if (check()) {
System.out.println(size);
System.exit(0);
}
return;
}
for (int r = nr; r <= H; r++) {
for (int c = 1; c < N; c++) {
if (ladder[r][c] == 1)
continue;
if (ladder[r][c - 1] == 1) {
continue;
}
if (ladder[r][c + 1] == 1) {
continue;
}
ladder[r][c] = 1;
comb(r, cnt + 1, size);
ladder[r][c] = 0;
}
}
}
static boolean check() {
for (int i = 1; i <= N; i++) {
int currPosition = i;
int start = 1;
while (start <= H) {
if (ladder[start][currPosition] == 1) {
currPosition++;
start++;
} else if (ladder[start][currPosition - 1] == 1) {
currPosition--;
start++;
} else {
start++;
}
}
if (i != currPosition)
return false;
}
return true;
}
}