Notice
Recent Posts
Recent Comments
Link
«   2024/05   »
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
Tags
more
Archives
Today
Total
관리 메뉴

찌로그

[BOJ] 백준 15684번 문제 사다리 조작 2020_05_19 C++ 본문

Coding

[BOJ] 백준 15684번 문제 사다리 조작 2020_05_19 C++

찌드 2020. 5. 19. 23:43

https://www.acmicpc.net/problem/15684

 

15684번: 사다리 조작

사다리 게임은 N개의 세로선과 M개의 가로선으로 이루어져 있다. 인접한 세로선 사이에는 가로선을 놓을 수 있는데, 각각의 세로선마다 가로선을 놓을 수 있는 위치의 개수는 H이고, 모든 세로선

www.acmicpc.net

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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
#include <bits/stdc++.h>
 
using namespace std;
//int dest[11] ={0,};
bool ladder[11][31={0,};
int n, m, h;
 
// void dfs(int org, int cur, int level){
//     if(level > h){ // end condition
//         dest[org] = cur;
//         return;
//     }
 
//     if(ladder[cur][level] ==1 && cur<n){
//         dfs(org, cur+1, level+1);
//     }else if(ladder[cur-1][level] == 1 && cur >1){
//         dfs(org, cur-1, level+1);
//     }else dfs(org, cur, level+1);
// }
 
bool destChk(){
    for(int i=1; i<=n; i++){
        int cur = i;
        for(int j=1; j<=h; j++){
            if(ladder[cur][j] == 1 && cur <n) cur++;
            else if(ladder[cur-1][j] == 1 && cur >1) cur--;
        }
        if(cur != i) return false;
    }
    return true;
}
bool isAvail(int a, int b){
    if(ladder[a][b] == 1return false;
    if(a <&& ladder[a+1][b] ==1return false;
    if(a >1 && ladder[a-1][b] ==1return false;
 
    return true;
}
// bool destChk(){
//     // for(int i=1; i<=n; i++) dfs(i,i,1);
//     for(int i=1; i<=n; i++) makeDest();
//     for(int i=1; i<=n; i++){
//         if(dest[i] != i) return false;
//     }
//     return true;
// }
bool Add1(){
        /* add 1 */
    for(int i=1; i<=n; i++){
        for(int j=1; j<=h; j++){
            if(!isAvail(i,j)) continue;
            ladder[i][j] =1;
            if(destChk()) return true;
            ladder[i][j] = 0;
        }
    }
    return false;
}
 
bool Add2(){
    for(int i=1; i<=n; i++){
        for(int j=1; j<=h; j++){
            if(!isAvail(i,j)) continue;
            ladder[i][j] = 1;
 
            for(int a = i; a<=n; a++){
                for(int b= 1; b<=h; b++){
                    if(!isAvail(a,b)) continue;
                    ladder[a][b] = 1;
                    if(destChk()) return true;
                    ladder[a][b] = 0;
                }
            }
            ladder[i][j] = 0;
        }
    }
    return false;
}
 
bool Add3(){
    for(int x=1; x<=n; x++){
        for(int y=1; y<=h; y++){
            if(!isAvail(x,y)) continue;
            ladder[x][y] =1;
 
            for(int i=x; i<=n; i++){
                for(int j=1; j<=h; j++){
                    if(!isAvail(i,j)) continue;
                    ladder[i][j] = 1;
 
                    for(int a = i; a<=n; a++){
                        for(int b= 1; b<=h; b++){
                            if(!isAvail(a,b)) continue;
                            ladder[a][b] = 1;
                            if(destChk()) return true;
                            ladder[a][b] = 0;
//    cout << x << "," << y << " " << i << "," << j << " " <<  a << "," << b << endl;
                        }
                    }
                    ladder[i][j] = 0;
                }
            }
            ladder[x][y] = 0;
        }
    }
    return false;
}
int main(){
    scanf("%d %d %d"&n,&m,&h); // 가로 n, 세로 h, 주어지는사다리 m개
    //for(int i=1; i<=n; i++) dest[i] = i;
    for(int i=0; i<m; i++){
        int a, b;  // (a)th dot line, b & b+1 linked
        scanf("%d %d"&a, &b);
        ladder[b][a] = 1;
    }/* ladder[n][h] */
 
    //init
    if(destChk()){ printf("%d\n"0); return 0; }
 
 
    if(Add1()){
        printf("%d\n"1);
        return 0;
    }
    if(Add2()){
        printf("%d\n"2);
        return 0;
    }
    if(Add3()){
        printf("%d\n"3);
        return 0;
    }
    else{
        printf("%d\n"-1);
        return 0;
    }
    // printf("%d\n", -1);
    return 0;
}
 
cs
Comments