#include <bits/stdc++.h>
using namespace std;
const int N = 30004;
struct ban
{
int x, p;
int d;
ban(){}
ban(int x, int p, int d)
{
this->x = x;
this->p = p;
this->d = d;
}
};
int n, m;
int b[N], p[N];
vector<int> a[N];
bool c[N][N];
int bfs()
{
queue<ban> q;
for (int i = 0; i < a[b[0]].size(); ++i)
{
if (!c[b[0]][a[b[0]][i]])
{
c[b[0]][a[b[0]][i]] = true;
q.push(ban(b[0], a[b[0]][i], 0));
}
}
while (!q.empty())
{
ban t;
t = q.front();
q.pop();
if (t.x == b[1])
return t.d;
if (t.x + t.p < n)
{
if (!c[t.x + t.p][t.p])
{
c[t.x + t.p][t.p] = true;
q.push(ban(t.x + t.p, t.p, t.d + 1));
}
for (int i = 0; i < a[t.x + t.p].size(); ++i)
{
ban h(t.x + t.p, a[t.x + t.p][i], t.d + 1);
if (!c[h.x][h.p])
{
c[h.x][h.p] = true;
q.push(h);
}
}
}
if (t.x - t.p >= 0)
{
if (!c[t.x - t.p][t.p])
{
c[t.x - t.p][t.p] = true;
q.push(ban(t.x - t.p, t.p, t.d + 1));
}
for (int i = 0; i < a[t.x - t.p].size(); ++i)
{
ban h(t.x - t.p, a[t.x - t.p][i], t.d + 1);
if (!c[h.x][h.p])
{
c[h.x][h.p] = true;
q.push(h);
}
}
}
}
return -1;
}
int main()
{
cin >> n >> m;
for (int i = 0; i < m; ++i)
{
cin >> b[i] >> p[i];
a[b[i]].push_back(p[i]);
}
cout << bfs() << endl;
return 0;
}
Compilation message
skyscraper.cpp: In function 'int bfs()':
skyscraper.cpp:27:23: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (int i = 0; i < a[b[0]].size(); ++i)
~~^~~~~~~~~~~~~~~~
skyscraper.cpp:49:31: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (int i = 0; i < a[t.x + t.p].size(); ++i)
~~^~~~~~~~~~~~~~~~~~~~~
skyscraper.cpp:66:31: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (int i = 0; i < a[t.x - t.p].size(); ++i)
~~^~~~~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
1024 KB |
Output is correct |
2 |
Correct |
3 ms |
1024 KB |
Output is correct |
3 |
Correct |
3 ms |
1024 KB |
Output is correct |
4 |
Correct |
3 ms |
1024 KB |
Output is correct |
5 |
Correct |
3 ms |
1024 KB |
Output is correct |
6 |
Correct |
3 ms |
1024 KB |
Output is correct |
7 |
Correct |
3 ms |
1152 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
1024 KB |
Output is correct |
2 |
Correct |
2 ms |
1024 KB |
Output is correct |
3 |
Correct |
3 ms |
1024 KB |
Output is correct |
4 |
Correct |
3 ms |
1024 KB |
Output is correct |
5 |
Correct |
2 ms |
1024 KB |
Output is correct |
6 |
Correct |
3 ms |
1024 KB |
Output is correct |
7 |
Correct |
5 ms |
1152 KB |
Output is correct |
8 |
Correct |
4 ms |
1024 KB |
Output is correct |
9 |
Correct |
3 ms |
1152 KB |
Output is correct |
10 |
Correct |
3 ms |
1408 KB |
Output is correct |
11 |
Correct |
5 ms |
1408 KB |
Output is correct |
12 |
Correct |
4 ms |
1408 KB |
Output is correct |
13 |
Correct |
6 ms |
1536 KB |
Output is correct |
14 |
Correct |
6 ms |
1536 KB |
Output is correct |
15 |
Correct |
5 ms |
1536 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
1024 KB |
Output is correct |
2 |
Correct |
3 ms |
1024 KB |
Output is correct |
3 |
Correct |
5 ms |
1024 KB |
Output is correct |
4 |
Correct |
4 ms |
1024 KB |
Output is correct |
5 |
Correct |
4 ms |
1024 KB |
Output is correct |
6 |
Correct |
3 ms |
1024 KB |
Output is correct |
7 |
Correct |
3 ms |
1024 KB |
Output is correct |
8 |
Correct |
2 ms |
1068 KB |
Output is correct |
9 |
Correct |
3 ms |
1152 KB |
Output is correct |
10 |
Correct |
4 ms |
1408 KB |
Output is correct |
11 |
Correct |
5 ms |
1408 KB |
Output is correct |
12 |
Correct |
6 ms |
1408 KB |
Output is correct |
13 |
Correct |
5 ms |
1536 KB |
Output is correct |
14 |
Correct |
5 ms |
1536 KB |
Output is correct |
15 |
Correct |
5 ms |
1536 KB |
Output is correct |
16 |
Correct |
3 ms |
1024 KB |
Output is correct |
17 |
Correct |
9 ms |
4352 KB |
Output is correct |
18 |
Correct |
4 ms |
1024 KB |
Output is correct |
19 |
Correct |
3 ms |
1152 KB |
Output is correct |
20 |
Correct |
16 ms |
9216 KB |
Output is correct |
21 |
Correct |
4 ms |
1152 KB |
Output is correct |
22 |
Correct |
4 ms |
1152 KB |
Output is correct |
23 |
Correct |
11 ms |
7808 KB |
Output is correct |
24 |
Correct |
15 ms |
9856 KB |
Output is correct |
25 |
Correct |
6 ms |
2816 KB |
Output is correct |
26 |
Correct |
13 ms |
7884 KB |
Output is correct |
27 |
Correct |
12 ms |
8576 KB |
Output is correct |
28 |
Correct |
16 ms |
11384 KB |
Output is correct |
29 |
Correct |
12 ms |
9216 KB |
Output is correct |
30 |
Correct |
14 ms |
9216 KB |
Output is correct |
31 |
Correct |
13 ms |
9216 KB |
Output is correct |
32 |
Correct |
13 ms |
9216 KB |
Output is correct |
33 |
Correct |
18 ms |
9316 KB |
Output is correct |
34 |
Correct |
16 ms |
9344 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
1024 KB |
Output is correct |
2 |
Correct |
4 ms |
1024 KB |
Output is correct |
3 |
Correct |
3 ms |
1024 KB |
Output is correct |
4 |
Correct |
3 ms |
1052 KB |
Output is correct |
5 |
Correct |
2 ms |
1024 KB |
Output is correct |
6 |
Correct |
3 ms |
1024 KB |
Output is correct |
7 |
Correct |
3 ms |
1152 KB |
Output is correct |
8 |
Correct |
3 ms |
1024 KB |
Output is correct |
9 |
Correct |
3 ms |
1152 KB |
Output is correct |
10 |
Correct |
4 ms |
1280 KB |
Output is correct |
11 |
Correct |
4 ms |
1408 KB |
Output is correct |
12 |
Correct |
5 ms |
1400 KB |
Output is correct |
13 |
Correct |
5 ms |
1536 KB |
Output is correct |
14 |
Correct |
6 ms |
1536 KB |
Output is correct |
15 |
Correct |
5 ms |
1536 KB |
Output is correct |
16 |
Correct |
4 ms |
1024 KB |
Output is correct |
17 |
Correct |
9 ms |
4352 KB |
Output is correct |
18 |
Correct |
4 ms |
1152 KB |
Output is correct |
19 |
Correct |
3 ms |
1152 KB |
Output is correct |
20 |
Correct |
14 ms |
9216 KB |
Output is correct |
21 |
Correct |
4 ms |
1024 KB |
Output is correct |
22 |
Correct |
7 ms |
1024 KB |
Output is correct |
23 |
Correct |
12 ms |
7808 KB |
Output is correct |
24 |
Correct |
14 ms |
9856 KB |
Output is correct |
25 |
Correct |
7 ms |
2816 KB |
Output is correct |
26 |
Correct |
12 ms |
7936 KB |
Output is correct |
27 |
Correct |
12 ms |
8576 KB |
Output is correct |
28 |
Correct |
20 ms |
11520 KB |
Output is correct |
29 |
Correct |
13 ms |
9216 KB |
Output is correct |
30 |
Correct |
16 ms |
9216 KB |
Output is correct |
31 |
Correct |
15 ms |
9216 KB |
Output is correct |
32 |
Correct |
12 ms |
9216 KB |
Output is correct |
33 |
Correct |
19 ms |
9344 KB |
Output is correct |
34 |
Correct |
21 ms |
9228 KB |
Output is correct |
35 |
Correct |
34 ms |
10368 KB |
Output is correct |
36 |
Correct |
10 ms |
4480 KB |
Output is correct |
37 |
Correct |
30 ms |
11172 KB |
Output is correct |
38 |
Correct |
37 ms |
13688 KB |
Output is correct |
39 |
Correct |
24 ms |
2560 KB |
Output is correct |
40 |
Correct |
34 ms |
5880 KB |
Output is correct |
41 |
Correct |
53 ms |
12664 KB |
Output is correct |
42 |
Correct |
26 ms |
8440 KB |
Output is correct |
43 |
Correct |
32 ms |
9084 KB |
Output is correct |
44 |
Correct |
38 ms |
9848 KB |
Output is correct |
45 |
Correct |
54 ms |
10616 KB |
Output is correct |
46 |
Correct |
46 ms |
10360 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
1024 KB |
Output is correct |
2 |
Correct |
3 ms |
1024 KB |
Output is correct |
3 |
Correct |
3 ms |
1152 KB |
Output is correct |
4 |
Correct |
3 ms |
1024 KB |
Output is correct |
5 |
Correct |
3 ms |
1024 KB |
Output is correct |
6 |
Correct |
3 ms |
1024 KB |
Output is correct |
7 |
Correct |
3 ms |
1024 KB |
Output is correct |
8 |
Correct |
2 ms |
1024 KB |
Output is correct |
9 |
Correct |
9 ms |
1152 KB |
Output is correct |
10 |
Correct |
4 ms |
1280 KB |
Output is correct |
11 |
Correct |
5 ms |
1408 KB |
Output is correct |
12 |
Correct |
4 ms |
1408 KB |
Output is correct |
13 |
Correct |
5 ms |
1536 KB |
Output is correct |
14 |
Correct |
7 ms |
1536 KB |
Output is correct |
15 |
Correct |
4 ms |
1536 KB |
Output is correct |
16 |
Correct |
4 ms |
1024 KB |
Output is correct |
17 |
Correct |
10 ms |
4352 KB |
Output is correct |
18 |
Correct |
4 ms |
1152 KB |
Output is correct |
19 |
Correct |
4 ms |
1152 KB |
Output is correct |
20 |
Correct |
12 ms |
9216 KB |
Output is correct |
21 |
Correct |
3 ms |
1024 KB |
Output is correct |
22 |
Correct |
5 ms |
1152 KB |
Output is correct |
23 |
Correct |
11 ms |
7808 KB |
Output is correct |
24 |
Correct |
17 ms |
9728 KB |
Output is correct |
25 |
Correct |
7 ms |
2816 KB |
Output is correct |
26 |
Correct |
11 ms |
7936 KB |
Output is correct |
27 |
Correct |
12 ms |
8568 KB |
Output is correct |
28 |
Correct |
17 ms |
11392 KB |
Output is correct |
29 |
Correct |
14 ms |
9216 KB |
Output is correct |
30 |
Correct |
14 ms |
9216 KB |
Output is correct |
31 |
Correct |
13 ms |
9216 KB |
Output is correct |
32 |
Correct |
14 ms |
9216 KB |
Output is correct |
33 |
Correct |
18 ms |
9344 KB |
Output is correct |
34 |
Correct |
15 ms |
9344 KB |
Output is correct |
35 |
Correct |
29 ms |
10488 KB |
Output is correct |
36 |
Correct |
10 ms |
4480 KB |
Output is correct |
37 |
Correct |
35 ms |
11128 KB |
Output is correct |
38 |
Correct |
39 ms |
13688 KB |
Output is correct |
39 |
Correct |
32 ms |
2552 KB |
Output is correct |
40 |
Correct |
25 ms |
6016 KB |
Output is correct |
41 |
Correct |
41 ms |
12536 KB |
Output is correct |
42 |
Correct |
26 ms |
8516 KB |
Output is correct |
43 |
Correct |
28 ms |
9080 KB |
Output is correct |
44 |
Correct |
26 ms |
9728 KB |
Output is correct |
45 |
Correct |
60 ms |
10616 KB |
Output is correct |
46 |
Correct |
54 ms |
10364 KB |
Output is correct |
47 |
Correct |
65 ms |
35376 KB |
Output is correct |
48 |
Correct |
25 ms |
1912 KB |
Output is correct |
49 |
Correct |
19 ms |
1792 KB |
Output is correct |
50 |
Correct |
23 ms |
1656 KB |
Output is correct |
51 |
Correct |
276 ms |
192812 KB |
Output is correct |
52 |
Correct |
299 ms |
212160 KB |
Output is correct |
53 |
Correct |
62 ms |
24184 KB |
Output is correct |
54 |
Correct |
66 ms |
62712 KB |
Output is correct |
55 |
Correct |
92 ms |
82808 KB |
Output is correct |
56 |
Correct |
143 ms |
124056 KB |
Output is correct |
57 |
Correct |
6 ms |
2944 KB |
Output is correct |
58 |
Correct |
154 ms |
123568 KB |
Output is correct |
59 |
Correct |
137 ms |
114296 KB |
Output is correct |
60 |
Correct |
150 ms |
114296 KB |
Output is correct |
61 |
Correct |
237 ms |
111100 KB |
Output is correct |
62 |
Correct |
420 ms |
226132 KB |
Output is correct |
63 |
Correct |
485 ms |
140920 KB |
Output is correct |
64 |
Correct |
538 ms |
135080 KB |
Output is correct |
65 |
Correct |
567 ms |
133148 KB |
Output is correct |
66 |
Correct |
857 ms |
131064 KB |
Output is correct |
67 |
Correct |
570 ms |
127584 KB |
Output is correct |