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;
typedef pair<int,int> ii;
int n,m,S,T;
bool Visited[30005];
map<int,int> d[30005];
map<int,bool> Free[30005];
vector<int> g[30005];
queue<ii> Q;
int main()
{
ios_base::sync_with_stdio(false);
//freopen("TEST.INP","r",stdin);
cin>>n>>m;
int pos,step;
for(int i=1;i<=m;i++)
{
cin>>pos>>step;
g[pos+1].push_back(step);
if(i==1)
S=pos+1;
if(i==2)
T=pos+1;
}
for(int i=0; i<g[S].size(); i++)
{
Free[S][g[S][i]]=true;
Q.push(ii(S,g[S][i]));
}
while(Q.size()>0)
{
int u=Q.front().first;
int v=Q.front().second;
Q.pop();
if(u==T)
{
cout<<d[u][v];
return 0;
}
if(Visited[u]==false)
{
Visited[u]=true;
for(int i=0; i<g[u].size(); i++)
{
if(u-g[u][i]>=1&&Free[u-g[u][i]][g[u][i]]==false)
{
Free[u-g[u][i]][g[u][i]]=true;
d[u-g[u][i]][g[u][i]]=d[u][v]+1;
Q.push(ii(u-g[u][i],g[u][i]));
}
if(u+g[u][i]<=n&&Free[u+g[u][i]][g[u][i]]==false)
{
Free[u+g[u][i]][g[u][i]]=true;
d[u+g[u][i]][g[u][i]]=d[u][v]+1;
Q.push(ii(u+g[u][i],g[u][i]));
}
}
}
if(u-v>=1&&Free[u-v][v]==false)
{
Free[u-v][v]=true;
d[u-v][v]=d[u][v]+1;
Q.push(ii(u-v,v));
}
if(u+v<=n&&Free[u+v][v]==false)
{
Free[u+v][v]=true;
d[u+v][v]=d[u][v]+1;
Q.push(ii(u+v,v));
}
}
cout<<-1;
}
Compilation message (stderr)
skyscraper.cpp: In function 'int main()':
skyscraper.cpp:27:19: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i=0; i<g[S].size(); i++)
~^~~~~~~~~~~~
skyscraper.cpp:45:27: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i=0; i<g[u].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... |