BNU 25588 Elevator Trouble【裸BFS】
来源:互联网 发布:亮剑online知乎 编辑:程序博客网 时间:2024/05/17 07:38
链接:
http://www.bnuoj.com/bnuoj/problem_show.php?pid=25588
http://www.bnuoj.com/bnuoj/contest_show.php?cid=2321#problem/25865
D. Elevator Trouble
2000ms
1000ms
65536KB
64-bit integer IO format: %lld Java class name: Main
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 110 1 10 2 1Sample Input 2100 2 1 1 0
Sample Output
Sample Output 16Sample Output 2use the stairs
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;}
- BNU 25588 Elevator Trouble【裸BFS】
- Elevator Trouble
- hrbust 哈理工OJ 1498Elevator Trouble【BFS过】
- Elevator Trouble解题报告
- spoj Elevator Trouble
- HDU 4334 & BNU 126130 -- Trouble
- BNU 26351 Space Elevator 数位dp
- BNU 26582 Gregory the Grasshopper【简单BFS】
- bnu 4289 Theta Puzzle[bfs, 打表]
- BNU 49102进化之地(Evoland) BFS
- BNU 49102 进化之地(Evoland)【Bfs】
- BNU
- trouble
- trouble
- HDU 4166 & BNU 32715 Robot Navigation (记忆化bfs)
- bnu 14327 The Water Bowls[bfs,状态压缩]
- bnu 51641 Certain Maze(bfs)(北师16校赛)
- BNU 1642 简单的图论问题? 最短路+BFS
- socket多线程网络服务器 java
- jboss7配置mysql数据源
- 哈夫曼树的构造
- 1047. Student List for Course (25)
- 计算机科学中最重要的32个算法
- BNU 25588 Elevator Trouble【裸BFS】
- 边复习边写(树的操作)
- JAVA_WEB Struts2学习:使用验证框架进行校验JSP页面元素 反编译工具的配置 Strtuts2 国际化学习
- 面试题45:圆圈中最后剩下的数字
- linux wait()函数
- HibernateTemplate全解
- [C#]打开指定路径的文件夹
- Java多线程设计模式之线程池模式
- mac 搭建svn