This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#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 (stderr)
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 |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |