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;
queue<pair<pair<int, int>, int> > q;
int A[30005], B[30005], C[30005];
map<pair<int, int>, bool> mp;
int main()
{
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int n, m;
cin >> n >> m;
for (int i=0; i<m; i++)
{
cin >> A[i] >> B[i];
C[A[i]]=B[i];
}
q.push({{A[0], B[0]}, 0});
mp[{A[0], B[0]}]=1;
while (!q.empty())
{
int a=q.front().first.first, b=q.front().first.second, c=q.front().second;
q.pop();
if (a==A[1])
{
cout << c;
return 0;
}
if (a+b<n)
{
if (!mp[{a+b, C[a+b]}])
{
q.push({{a+b, C[a+b]}, c+1});
mp[{a+b, C[a+b]}]=1;
}
if (!mp[{a+b, b}])
{
q.push({{a+b, b}, c+1});
mp[{a+b, b}]=1;
}
}
if (a-b>=0)
{
if (!mp[{a-b, C[a-b]}])
{
q.push({{a-b, C[a-b]}, c+1});
mp[{a-b, C[a-b]}]=1;
}
if (!mp[{a-b, b}])
{
q.push({{a-b, b}, c+1});
mp[{a-b, b}]=1;
}
}
}
cout << -1;
return 0;
}
# | 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... |