HDU 1548 A strange lift-bfs
来源:互联网 发布:c语言水平制表符 编辑:程序博客网 时间:2024/05/10 11:49
题目地址:HDU 1548 http://acm.hdu.edu.cn/showproblem.php?pid=1548
A strange lift
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 19009 Accepted Submission(s): 7025
Problem Description
There is a strange lift.The lift can stop can at every floor as you want, and there is a number Ki(0 <= Ki <= N) on every floor.The lift have just two buttons: up and down.When you at floor i,if you press the button "UP" , you will go up Ki floor,i.e,you will go to the i+Ki th floor,as the same, if you press the button "DOWN" , you will go down Ki floor,i.e,you will go to the i-Ki th floor. Of course, the lift can't go up high than N,and can't go down lower than 1. For example, there is a buliding with 5 floors, and k1 = 3, k2 = 3,k3 = 1,k4 = 2, k5 = 5.Begining from the 1 st floor,you can press the button "UP", and you'll go up to the 4 th floor,and if you press the button "DOWN", the lift can't do it, because it can't go down to the -2 th floor,as you know ,the -2 th floor isn't exist.
Here comes the problem: when you are on floor A,and you want to go to floor B,how many times at leasthe has to press the button "UP" or "DOWN"?
Here comes the problem: when you are on floor A,and you want to go to floor B,how many times at leasthe has to press the button "UP" or "DOWN"?
Input
The input consists of several test cases.,Each test case contains two lines.
The first line contains three integers N ,A,B( 1 <= N,A,B <= 200) which describe above,The second line consist N integers k1,k2,....kn.
A single 0 indicate the end of the input.
The first line contains three integers N ,A,B( 1 <= N,A,B <= 200) which describe above,The second line consist N integers k1,k2,....kn.
A single 0 indicate the end of the input.
Output
For each case of the input output a interger, the least times you have to press the button when you on floor A,and you want to go to floor B.If you can't reach floor B,printf "-1".
Sample Input
5 1 53 3 1 2 50
Sample Output
3
题目大意:
每层楼的电梯,只有两个按钮:上、下。你只能上或下所给出的层数,当然,电梯不能升得很高,而且不能低于1。例如,有一个5层的建筑,k1 = 3, k2 = 3,k3 = 1,k4 = 2, k5 = 5。从1楼开始,你可以按下按钮“上”,你会走到4楼,如果你按“下”的按钮,电梯不能做它,因为它不能去 -2 楼,你知道的,在 -2 楼是不存在的。 问题是:当你在地上一个,你要去地乙,至少按多少次按钮“上”或“下”?
解题思路
这求最短路问题,使用简单的bfs就可以解决,注意起点终点位置一致问题、不能越界问题、访问数组初始化问题、队列清空问题。
代码:
#include<cstdio>#include<queue>#include<string.h>using namespace std;struct node{ int x,t;//x表示的是当前层数,t表示总步数;cz表示上下走}s;//s表示起点queue<node> q;int n,a,b;int p[210]={};bool vis[210]={};//存是否走过当前层int bfs(){ q.push(s); vis[s.x]=1; while (!q.empty()) { node now,nnew; now=q.front(); q.pop(); //向上 nnew.x=now.x+p[now.x]; nnew.t=now.t+1; if(!vis[nnew.x]&&nnew.x<210) { q.push(nnew); vis[nnew.x]=1; } if(nnew.x==b) return nnew.t; //向下 nnew.x=now.x-p[now.x]; nnew.t=now.t+1; if(nnew.x==b) return nnew.t; if(nnew.x>0&&!vis[nnew.x]) { q.push(nnew); vis[nnew.x]=1; } } return -1;}int main(){ while(scanf("%d",&n)&&n!=0) { scanf("%d%d",&a,&b); s.x=a; s.t=0; for(int i=1;i<=n;++i) scanf("%d",&p[i]); if(a==b) printf("0\n"); else printf("%d\n",bfs()); memset(vis,0,sizeof(vis));//初始化访问数组!! while(!q.empty())//队列清空!! { q.pop(); } } return 0 ;}
1 0
- hdu 1548 A strange lift BFS 解法
- hdu 1548 A strange lift (bfs)
- hdu 1548 A strange lift (bfs)
- 【BFS/Dijkstra】hdu 1548 A Strange Lift
- hdu 1548 A strange lift (BFS、Dijkstra)
- hdu 1548 A strange lift (bfs)
- HDU 1548 A strange lift(BFS)
- hdu 1548 A strange lift(搜索:BFS)
- hdu 1548 A strange lift(水题,bfs)
- HDU--1548:A strange lift (BFS)
- HDU 1548 A strange lift (BFS)
- hdu 1548 A strange lift(bfs)
- hdu 1548 A strange lift(bfs)
- HDU 1548 A strange lift(bfs)
- hdu 1548 A strange lift(bfs)
- bfs入门 hdu 1548 a strange lift
- hdu 1548 A strange lift(BFS)
- HDU 1548 A strange lift(BFS)
- BZOJ 4523 CQOI 2016 路由表 字典树
- SAP ABAP编辑器----HippoEDIT(破解汉化版)
- android学习之读取联系人
- 学习 xstream
- HTML5,让 Ajax 动态改变 URL 且支持后退
- HDU 1548 A strange lift-bfs
- 解决ping邮箱服务器域名,提示找不到主机域名的问题
- Jenkins进阶系列之——01使用email-ext替换Jenkins的默认邮件通知
- 旧的Eclipse迁移到Android Studio中碰到的图片iCCP问题解决
- Given a non-negative integer num, repeatedly add all its digits until the result has only one digit
- 怎样计算阶乘
- 调优JVM内存,并解决OutOfMemoryError,StackOverflowError等异常问题
- showFilesName.cpp
- hamilyjing:APP开发框架