forked from b-chae/AlgorithmStudy
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path15684.cpp
More file actions
92 lines (84 loc) · 1.38 KB
/
Copy path15684.cpp
File metadata and controls
92 lines (84 loc) · 1.38 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
#include <iostream>
using namespace std;
bool origladder[11][31];
bool ladder[12][31];
int N, M, H;
bool check(int n)
{
int x = n, y = 1;
while (y <= M)
{
if (ladder[x][y])
{
x++;
}
else if (ladder[x - 1][y])
{
x--;
}
y++;
}
return x == n;
}
bool checkall()
{
int i;
for (i = 1; i <= N; i++)
{
if (!check(i))
return false;
}
return true;
}
bool addladder(int n, int i, int j, int now)
{
if (now == n)
{
if (checkall())
return true;
return false;
}
if (i < N)
{
i++;
}
else if (j < M)
{
i = 0;
j++;
}
else
{
return false;
}
bool flag = false;
if (i < N && !ladder[i][j] && !ladder[i + 1][j] && !ladder[i - 1][j])
{
ladder[i][j] = true;
flag = addladder(n, i, j, now + 1);
ladder[i][j] = false;
}
return flag || addladder(n, i, j, now);
}
int main()
{
ios::sync_with_stdio(0);
cin.tie(0);
cin >> N >> H >> M;
int i, x, y;
for (i = 0; i < H; i++)
{
cin >> x >> y;
ladder[y][x] = true;
}
for (i = 0; i <= 3; i++)
{
if (addladder(i, -1, 0, 0))
{
cout << i << "\n";
return 0;
}
}
cout << "-1\n";
return 0;
}