Question

In: Computer Science

You are in a skyscraper and you are currently in floor s, where you see an...

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

Solutions

Expert Solution

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.


Related Solutions

Where do you see the future of CAM ?
Where do you see the future of CAM ?
As the team leader on the floor where you practice nursing, you are covering another RN’s...
As the team leader on the floor where you practice nursing, you are covering another RN’s group of patients while she is away at lunch. As you make your rounds, you notice her LPN (who is not certified for IV medication administration) administering a syringe of fluid via an IV line to a patient. What data in the scenario are pertinent? Is there a nursing practice violation? If so, what is it? What regulations and/or standards address these issues? How...
Give the signs and symptoms (S/S) you might see in a patient with hepatitis A. In...
Give the signs and symptoms (S/S) you might see in a patient with hepatitis A. In addition give the etiology, as well as how a patient might be diagnosed and treated with this disease.   What will your plan of care be for Ms. Ross?
You are the building manager for a cylindrical skyscraper with a slanted roof. You must contract...
You are the building manager for a cylindrical skyscraper with a slanted roof. You must contract a window cleaner fwho works for $35 an hour and can clean 1 square meter of glass per minute. If you know the building is 120m tall at its highest point and 100m tall at its lowest point, and 20m wide, how much should you budget for the contract? Use a line integral of a scalar function to solve. Assume there are no windows...
Consider where you currently work, where you have previously worked, or a well-known company where you...
Consider where you currently work, where you have previously worked, or a well-known company where you would like to work. How would this company use the master budgeting process? What budgets would be included in their master budget? Which employees would use the various parts of the master budget? Explain.
Consider where you currently work, where you have previously worked, or a well-known company where you...
Consider where you currently work, where you have previously worked, or a well-known company where you would like to work. How would this company utilize variance analysis? What variances do you think would be most significant to the company?
Consider where you currently work, where you have previously worked, or a well-known company where you...
Consider where you currently work, where you have previously worked, or a well-known company where you would like to work. How might this company make capital investment decisions? What capital investment decisions do you see evidence of at this company? Explain. A MINIMUM OF 150 WORDS, PLEASE.
Consider where you currently work, where you have previously worked, or a well-known company where you...
Consider where you currently work, where you have previously worked, or a well-known company where you would like to work. How would this company use the master budgeting process? What budgets would be included in their master budget? Which employees would use the various parts of the master budget? Explain in 250 words.
Consider where you currently work, where you have previously worked, or a company that you would...
Consider where you currently work, where you have previously worked, or a company that you would like to work at. What are the main considerations of implementing an ABC system? Give examples of activities that would be appropriate for the company, and explain why you selected those activities. Please answer this question in 200-300 words. Thank you.
Where do you see HRM technology headed?  Provide specifics.
Where do you see HRM technology headed?  Provide specifics.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT