-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathElevator.cpp
More file actions
60 lines (59 loc) · 1.17 KB
/
Elevator.cpp
File metadata and controls
60 lines (59 loc) · 1.17 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
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define N 1000001
vector<ll> g[N];
bool visit[N];
ll dist[N]={0};
int main()
{
ll f,gi,s,u,d;
cin>>f>>s>>gi>>u>>d;
//f s g u d
//f--;
//gi--;
//s--;
for(ll i=1;i<=f;i++)
{
if(i+u<=f)
{
g[i].push_back(i+u);
//cout<<"From "<<i<<" to "<<i+u<<endl;
}
if(i-d>0)
{
g[i].push_back(i-d);
//cout<<"From "<<i<<" to "<<i-d<<endl;
}
}
queue<ll> q;
q.push(s);
ll res=0;
bool found=false;
visit[s]=true;
while(!q.empty())
{
ll curr=q.front();
if(curr==gi)
{
res=dist[curr];
found=true;
}
q.pop();
if(found)
break;
for(ll i=0;i<g[curr].size();i++)
{
if(!visit[g[curr][i]])
{
// cout<<"Node visited : "<<g[curr][i]<<endl;
q.push(g[curr][i]);
dist[g[curr][i]]=dist[curr]+1;
visit[g[curr][i]]=true;
}
}
}
if(found)
cout<<res;
else cout<<"use the stairs";
}