-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMain.java
More file actions
77 lines (67 loc) · 2.16 KB
/
Main.java
File metadata and controls
77 lines (67 loc) · 2.16 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
65
66
67
68
69
70
71
72
73
74
75
76
77
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.StringTokenizer;
public class Main {
static int N,M,H,addLadder;
static int[][] lines;
static boolean found;
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(br.readLine());
N = Integer.parseInt(st.nextToken());
M = Integer.parseInt(st.nextToken());
H = Integer.parseInt(st.nextToken());
lines = new int[H+1][N+1];
for(int i=0; i<M; i++) {
st = new StringTokenizer(br.readLine());
int a = Integer.parseInt(st.nextToken());
int b = Integer.parseInt(st.nextToken());
lines[a][b] = 1;
}
// 추가로 놓을 수 있는 사다리는 0~3.
for(int i=0; i<=3; i++) {
addLadder = i;
putLadder(1,1,0);
if(found) break;
}
System.out.println(found? addLadder : -1);
br.close();
}
public static void putLadder(int x, int y, int ladderCnt) {
if(found) return;
if(addLadder == ladderCnt) {
if(descendingLadder()) {
found = true;
}
return;
}
for(int i=x; i<=H; i++){
for(int j=1; j<N; j++) {
if(lines[i][j]==1 || lines[i][j-1]==1 || lines[i][j+1]==1) continue;
lines[i][j] = 1;
putLadder(i, j, ladderCnt+1);
lines[i][j] = 0;
}
}
}
public static boolean descendingLadder() {
for(int actor=1; actor<=N; actor++) {
int verti = 1; // 세로줄 레벨
int hori = actor;
while(verti <= H) {
if(lines[verti][hori] == 1) {
hori++;
} else if(lines[verti][hori-1] == 1) {
hori--;
}
verti++;
}
if(actor != hori) {
return false;
}
}
return true;
}
}