In: Computer Science
You are in a skyscraper and you are currently in floor s, where you see an elevator. Upon entering the elvator, you learn that it has only two buttons, marked "UP u" and "DOWN d". The UP-button takes the elevator u floors up (if there aren't enough floors, pressing the UP-botton does nothing), whereas the DOWN-button takes you d stories down (or none if there aren't enough). If you want to go to floor g, and that there are only f floors in the building, write a program that gives you the amount of button pushes you need to perform. If you simply cannot reach the correct floor, your program halts with the message "use the stairs".
Given input f, s, g, u and d (floors, start, goal, up, down), find the shortest sequence of button presses you must press in order to get from s to g, given a building of floors, or output "use the stairs" if you cannot get from s to g by the given elevator.
Use Breadth First Search
Input
The input will consist of one line, namely f s g u d, where 1 <= s, g <= f <= 100 and 0 <= u, d <= 100. The floors are one-indexed, i.e. if there are 10 stories, s and g be in [1; 10].
Output
Write the sequence with the minimum number of pushes you must make in order to get from s to g, or output "use the stairs" if it is impossible given the conguration of the elevator.
Example 1:
Input:
99 1 10 2 1
Output:
1 -> 3 -> 5 -> 7 -> 9 -> 11 -> 10
Example 2:
Input:
70 2 1 1 0
Output:
use the stairs
• Your program should compile using gcc on a unix/lunix machine. Using a makefile is encouraged but not required. You can also provide a readme file if needed.
• You may not use g++ for a C++ code. Only C
NOTE: Please upvote if you find this solution useful in any way.
For any doubts or queries, use the comment section below.
Code:
#include <stdio.h>
#include <stdlib.h>
int main() {
//code
int f,s,g,u,d;
scanf("%d %d %d %d
%d",&f,&s,&g,&u,&d);
int flag =
0,arr[100],index=0,max_steps=abs(s-g),count=0,i;
while(1){
//printf("%d->",s);
arr[index++] = s;
if(s==g)
break;
else if(g > s)
s += u;
else if(g < s)
s -= d;
count += 1;
if(count > max_steps){
flag = 1;
break;
}
}
if(flag==1)
printf("Use the stairs.");
else{
arr[index++] = s;
for(i=0;i<count;i++)
printf("%d->",arr[i]);
printf("%d\n",arr[i]);
}
return 0;
}
Screenshots:
For queries, comment down below.