hrbust 哈理工OJ 1498Elevator Trouble【BFS过】
来源:互联网 发布:淘宝新店铺怎么上货 编辑:程序博客网 时间:2024/05/18 00:13
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 fromstog, given a building of f floors, or output "use the stairs" if you cannot get from s to g by the given elevator.
There are several test cases. For each test case, 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].
OutputFor each test case,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 con guration of the elvator.
10 1 10 2 1
100 2 1 1 0Sample Output
6
use the stairsSourceNCPC2011
一道并不是很难的搜索题,不需要剪枝、暴力搜一发就行了、
这个题和hdu 1548是一模一样的题目、大家有兴趣可以做做这个题目、
思路:从起点出发,两种走法,一种向上,一种向下,走到终点输出当前方法的步数。如果一直没有输出,就输出use the stairs
这里直接上AC代码:
#include<stdio.h>#include<string.h>#include<queue>using namespace std;struct floor{ int pos,output;}now,nex;int f,s,g,u,d;int vis[1000005];void bfs(int x){ memset(vis,0,sizeof(vis)); vis[x]=1; now.pos=x; now.output=0; queue<floor>s; s.push(now); while(!s.empty()) { now=s.front(); if(now.pos==g) { printf("%d\n",now.output); return ; } s.pop(); for(int i=0;i<2;i++) { if(i==0) { nex.pos=now.pos+u; nex.output=now.output+1; if(nex.pos>=1&&nex.pos<=f&&vis[nex.pos]==0) { vis[nex.pos]=1; s.push(nex); } } if(i==1) { nex.pos=now.pos-d; nex.output=now.output+1; if(nex.pos>=1&&nex.pos<=f&&vis[nex.pos]==0) { vis[nex.pos]=1; s.push(nex); } } } } printf("use the stairs\n"); return ;}int main(){ while(~scanf("%d%d%d%d%d",&f,&s,&g,&u,&d)) { bfs(s); }}
- hrbust 哈理工OJ 1498Elevator Trouble【BFS过】
- hrbust/哈理工oj 1617 回家【BFS+BFS】
- hrbust 哈理工oj 2188 星际旅行【BFS+传送阵】
- hrbust 哈理工oj 1989 营救小组【BFS】
- hrbust 哈理工oj 1613 迷宫问题 bfs
- hrbust 哈理工OJ 2133 最快通过【dijkstra过】
- hrbust 哈理工oj 2179 组合【基础dfs过】
- hrbust 哈理工oj 1743 Word Search【DFS过】
- hrbust/哈理工oj 1495 Robots on a grid【BFS+BFS+dp】
- hrbust 1286/哈理工oj 1286 迷宫与宝藏【较难bfs】
- hrbust 1214 哈理工oj 1214 方格取数【BFS广搜+dp】
- hrbust 哈理工oj 1352 中间的球【floyd递推关系过】
- hrbust 1658,哈理工oj 1658 一笔画【并查集过】
- hrbust 哈理工oj 1588 神医【贪心】
- hrbust 哈理工oj 网线【MST+Prim】
- 哈理工oj/hrbust 1790 武林【DP】
- hrbust哈理工oj 1674 充电【贪心】
- hrbust 哈理工oj 1330 邂逅【模拟】
- Shell 数组简介
- 设计模式之单例模式
- Oracle数据库索引
- C语言 switch语句的的用法
- usaco sprime
- hrbust 哈理工OJ 1498Elevator Trouble【BFS过】
- ContentValues的用法
- Java笔记---迭代器
- Java-day05集合
- Spring学习
- usaco subset
- git命令之git tag 给当前分支打标签
- usaco transform
- 关于利用trml2pdf生成PDF出现【flowable not yet implemented】错误的原因