D. elevator TroubleTime Limit: 2000 msCase Time Limit: 1000 msMemory Limit: 65536KB64-bit integer IO format: % lld Java class name: MainSubmit Status PID: 25588 Font Size: +-You are on your way to your first job interview as a program tester, and you are already late. the interview is 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 ". you conclude that the UP-button takes the elevator u floors up (if there aren't enough floors, pressing the UP-botton does nothing, or at least so you assume ), whereas the DOWN-button takes you d stories down (or none if there aren't enough ). knowing that the interview is at floor g, and that there are only f floors in the building, you quickly decide to 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 f floors, or output "use the stairs" if you cannot get from s to g by the given elevator. inputThe input will consist of one line, namely f s g u d, where 1 <= s, g <= f <= 1000000 and 0 <= u, d <= 1000000. the floors are one-indexed, I. e. if there are 10 stories, s and g be in [1, 10]. outputYou must reply with the minimum numbers of pushes you must make in order to get from s to g, or output use the stairsif it is impossible given the configuration of the elvator. sample InputSample Input 110 1 10 2 1 Sample Input 2100 2 1 1 0 Sample OutputSample Output 16 Sample Output 2use the stairsSubmit Status PID: 25588 code:
/** Question: f, s, g, u, d. The points that can be taken in total are from 1 to f. There are two ways for you to go from point s to point g: go forward to u, or, if d can be reached, the minimum number of steps is output. otherwise, output the use the stairs algorithm: BFS Summary: it is like my first BFS [POJ 3278 Catch That Cow], which is a bare BFS, after a slight change in the condition head, no response was made. I didn't even make it during the competition, but I still thought it was DP to solve the problem of my teammate Orc having passed the water... What is the rhythm of this status? Orz */# include <stdio. h> # include <string. h >#include <queue >#include <algorithm> # include <iostream> using namespace std; const int maxn = 1000000 + 10; int f, s, g, u, d; int flag, step; int vis [maxn]; struct Node {int p; // position int step; // number of steps}; queue <Node> q; void bfs (int s) {Node now, next; now = (Node) {s, 0}; while (! Q. empty () q. pop (); q. push (now); memset (vis, 0, sizeof (vis); vis [s] = 1; while (! Q. empty () {now = q. front (); q. pop (); // retrieve and bring up the first line for (int I = 1; I <= 2; I ++) {if (I = 1) next. p = now. p + u; // two walk-through methods if (I = 2) next. p = now. p-d; if (next. p <1 | next. p> f) continue; // if (! Vis [next. p]) {vis [next. p] = 1; next. step = now. step + 1; q. push (next); if (next. p = g) // reach the destination {flag = 1; step = next. step; return ;}}}return ;}int main () {while (scanf ("% d", & f, & s, & g, & u, & d )! = EOF) {if (s = g) // if the start point is the end point {printf ("0 \ n"); continue;} flag = 0; // mark bfs (s); // Search Start Point if (flag) printf ("% d \ n", step ); // else printf ("use the stairs \ n");} return 0 ;}