BNU 25588 Elevator Trouble
D. Elevator Trouble
Time Limit: 2000msCase Time Limit: 1000msMemory Limit: 65536KB
64-bit integer IO format: %lld Java class name: Main
Submit
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.
Input
The 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].
Output
You 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 Input
Sample Input 1
10 1 10 2 1
Sample Input 2
100 2 1 1 0
Sample Output
Sample Output 1
6
Sample Output 2
use the stairs
Submit
Status
PID: 25588
code:
/** 题意:给你 f,s,g,u,d 这几个数字 总共可以走的点为从 1 到 f 要你从点 s 到达点 g 有两种走法:往前走 u , 或者往后走 d 如果能到达则输出最少的步数.否则输出 use the stairs 算法:BFS 总结:就和本人第一个BFS 【POJ 3278 Catch That Cow 】简直是一个模块, 很裸的 BFS了,稍微变换了条件脑袋就没有反应过来 。 比赛的时候居然没有做出来,还一直认为是 DP 才能解决 还好队友 Orc 水过了。。。这种状态是什么节奏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; //位置 int step; //步数 }; 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(); //取出并且弹出队首 for(int i = 1; i <= 2; i++) { if(i == 1) next.p = now.p+u; //两种走法 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) //到达终点 { flag = 1; step = next.step; return; } } } } return; } int main() { while(scanf("%d%d%d%d%d", &f,&s,&g,&u,&d) != EOF) { if(s == g) //如果起点就是终点 { printf("0\n"); continue; } flag = 0; //标记 bfs(s); //搜索起点 if(flag) printf("%d\n", step); //能到达 else printf("use the stairs\n"); } return 0; }
补充:软件开发 , C++ ,