-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path[BOJ] 15684 사다리 조작(브루트포스).cpp
117 lines (107 loc) · 2.97 KB
/
[BOJ] 15684 사다리 조작(브루트포스).cpp
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
#include <iostream>
#include <tuple>
#include <vector>
using namespace std;
int garo[100][100];
int w, m, h;
// 사다리 타기 함수(출구 column을 반환)
int start(int c) {
int r = 1;
while(r <= h) {
// 가로선이 시작되는 경우
if (garo[r][c] == 1) {
// 우측으로 이동
c += 1;
}
// 가로선이 끝나는 경우
else if (garo[r][c] == 2) {
// 좌측으로 이동
c -= 1;
}
r += 1;
}
return c;
}
// i번째 결과가 i인지 확인하는 함수
bool go() {
for (int i = 1; i <= w; i++) {
int res = start(i);
if (res != i) return false; // i번째가 i로 끝나지 않는 경우
}
return true; // i번째가 i로 끝나는 경우
}
int main() {
cin >> w >> m >> h;
while (m--) {
int x, y;
cin >> x >> y;
garo[x][y] = 1;
garo[x][y+1] = 2;
}
// 가로선을 둘 수 있는 후보가 되는 위치
vector<pair<int, int>> a;
for (int i = 1; i <= h; i++) {
for (int j = 1; j <= w-1; j++) {
if (garo[i][j] != 0) continue;
if (garo[i][j+1] != 0) continue;
a.push_back({i, j});
}
}
// 사다리를 추가 안해도 되는 경우 return 0
int ans = -1;
if (go()) {
cout << 0 << endl;
return 0;
}
int len = a.size();
// 1번째 가로선 추가하기
for (int i = 0; i < len; i++) {
int x1 = a[i].first;
int y1 = a[i].second;
if (garo[x1][y1] != 0 || garo[x1][y1+1] != 0) continue;
garo[x1][y1] = 1;
garo[x1][y1+1] = 2;
if (go()) {
if (ans == -1 || ans > 1) {
ans = 1;
}
}
// 2번째 가로선 추가하기
for (int j = i+1; j < len; j++) {
int x2 = a[j].first;
int y2 = a[j].second;
if (garo[x2][y2] != 0 || garo[x2][y2+1] != 0) continue;
garo[x2][y2] = 1;
garo[x2][y2+1] = 2;
if (go()) {
if (ans == -1 || ans > 2) {
ans = 2;
}
}
// 3번째 가로선 추가하기
for (int k = j+1; k < len; k++) {
int x3 = a[k].first;
int y3 = a[k].second;
if (garo[x3][y3] != 0 || garo[x3][y3+1] != 0) continue;
garo[x3][y3] = 1;
garo[x3][y3+1] = 2;
if (go()) {
if (ans == -1 || ans > 3) {
ans = 3;
}
}
// 3번째 가로선 지우기
garo[x3][y3] = 0;
garo[x3][y3+1] = 0;
}
// 2번째 가로선 지우기
garo[x2][y2] = 0;
garo[x2][y2+1] = 0;
}
// 1번째 가로선 지우기
garo[x1][y1] = 0;
garo[x1][y1+1] = 0;
}
cout << ans << endl;
return 0;
}