BNU 25588 Elevator Trouble【裸BFS】

来源:互联网 发布:亮剑online知乎 编辑:程序博客网 时间:2024/06/11 03:52

链接:

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
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 110 1 10 2 1Sample Input 2100 2 1 1 0

Sample Output

Sample Output 16Sample Output 2use 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;}


原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 护士资格证过期了怎么办 辞职护士执业证怎么办 网约护士怎么办手续 动车票待核验怎么办 店员不维护老板怎么办 准考证号会过期怎么办 驾校准考证丢了怎么办 科目二下大雨怎么办 考科目二下雨天怎么办 普通话总是二乙怎么办 科目二很紧张怎么办 18年科目四缺考怎么办 个人医保卡欠费怎么办 医保欠费不想交怎么办 怀化市驾考绿色通道怎么办? 签注易不能办理怎么办 网上怎么办护照和签证 意大利被偷护照怎么办 户口在学校 怎么办签证 户口换了身份证怎么办 广州在校大学生怎么办护照 民间借贷无法还怎么办 退伍档案没接收怎么办 档案被单位扣住怎么办 公积金提不出来怎么办 公积金还贷款怎么办手续 科一预约失败怎么办 科四忘记预约怎么办 我科目一缺考了怎么办? 无可选考试场地怎么办 早产儿脑部发育不好怎么办 宝宝脑部发育不好怎么办 小孩脑部发育不好怎么办 8岁儿童智力低下怎么办 儿童食物不耐受怎么办 忘记就诊卡号怎么办 nt检查预约不到怎么办 听力不好科目三怎么办 青岛公安不立案怎么办 长春驾照丢了怎么办 驾照超期一个月怎么办