Problem Link
#include <iostream>
#include <vector>
#include <queue>
using namespace std;
const long N = 1000000;
long dist[N + 1];
void bfs(long s, long g, long u, long d, long f)
{
queue<long> q;
q.push(s);
while(!q.empty())
{
long currentFloor = q.front();
q.pop();
long floorDown = currentFloor - d;
long floorUp = currentFloor + u;
if(currentFloor == g)
{
break;
}
if(floorDown > 0 && floorDown <= f && dist[floorDown] == -1)
{
dist[floorDown] = dist[currentFloor] + 1;
q.push(floorDown);
}
if(floorUp <= f && dist[floorUp] == -1)
{
dist[floorUp] = dist[currentFloor] + 1;
q.push(floorUp);
}
}
}
int main()
{
long f,s, g, u, d;
cin >> f >> s >> g >> u >> d;
vector<long> graph[f + 1];
for(int i = 0; i < N + 1; i++)
{
dist[i] = -1;
}
dist[s] = 0;
bfs(s, g, u, d, f);
dist[g] != -1 ? cout << dist[g] : cout << "use the stairs";
}
Comments
Post a Comment