-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path사다리조작_15684.java
More file actions
92 lines (82 loc) · 2.59 KB
/
사다리조작_15684.java
File metadata and controls
92 lines (82 loc) · 2.59 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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
package baekjoon;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
public class 사다리조작_15684 {
static int N, M, H, answer;
static int[][] ladder;
public static void main(String[] args) throws Exception {
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());
answer = -1;
ladder = new int[H][N + 1];
while (M-- > 0) {
st = new StringTokenizer(br.readLine(), " ");
int i = Integer.parseInt(st.nextToken())-1;
int j = Integer.parseInt(st.nextToken());
ladder[i][j]=2;
ladder[i][j+1]=ladder[i][j-1]=1;
}
drawLine(0, 0, 1);
System.out.println(answer);
br.close();
}
static void drawLine(int cnt, int ci, int cj){
if(playLadderGame()){
answer =cnt;
return;
}
if((0< answer && answer <=cnt) || cnt==3){
return;
}
for(int j=cj; j<N; j++){
if(ladder[ci][j]==0){
ladder[ci][j]=2;
ladder[ci][j+1]=ladder[ci][j-1]=1;
drawLine(cnt+1, ci, j+1);
ladder[ci][j]=0;
if(j-2<0||ladder[ci][j-2]!=2){
ladder[ci][j-1]=0;
}
if(N<j+2||ladder[ci][j+2]!=2){
ladder[ci][j+1]=0;
}
}
}
for(int i=ci+1; i<H; i++){
for(int j=1; j<N; j++){
if(ladder[i][j]==0){
ladder[i][j]=2;
ladder[i][j+1]=ladder[i][j-1]=1;
drawLine(cnt+1, i, j+1);
ladder[i][j]=0;
if(j-2<0||ladder[i][j-2]!=2){
ladder[i][j-1]=0;
}
if(N<j+2||ladder[i][j+2]!=2){
ladder[i][j+1]=0;
}
}
}
}
}
static boolean playLadderGame(){
for(int n=0; n<N; n++){
int target = n;
for(int i=0; i<H; i++){
if(ladder[i][target]==2){
target--;
}else if(ladder[i][target+1]==2){
target++;
}
}
if(target!=n){
return false;
}
}
return true;
}
}