HDU 1548 A strange lift(最短路&&bfs)
A strange lift
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 26943 Accepted Submission(s): 9699
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 least he has
to press the button "UP" or "DOWN"?
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.
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".
- #include <iostream>
- #include <algorithm>
- #include <cstring>
- #include <cstdio>
- #include <vector>
- #include <queue>
- #include <cstdlib>
- #include <iomanip>
- #include <cmath>
- #include <ctime>
- #include <map>
- #include <set>
- using namespace std;
- #define lowbit(x) (x&(-x))
- #define max(x,y) (x>y?x:y)
- #define min(x,y) (x<y?x:y)
- #define MAX 100000000000000000
- #define MOD 1000000007
- #define pi acos(-1.0)
- #define ei exp(1)
- #define PI 3.141592653589793238462
- #define ios() ios::sync_with_stdio(false)
- #define INF 0x3f3f3f3f
- #define mem(a) (memset(a,0,sizeof(a)))
- typedef long long ll;
- int dis[],g[][],vis[];
- int a[],n,x,y,k;
- void init()
- {
- for(int i=;i<=n;i++)
- {
- for(int j=;j<i;j++)
- {
- g[i][j]=g[j][i]=INF;
- }
- g[i][i]=;
- }
- }
- void dij(int x)
- {
- for(int i=;i<=n;i++)
- {
- dis[i]=g[x][i];
- vis[i]=;
- }
- vis[x]=;
- int minn,v=x;
- for(int i=;i<n;i++)
- {
- minn=INF;
- for(int j=;j<=n;j++)
- {
- if(!vis[j] && minn>dis[j])
- {
- minn=dis[j];
- v=j;
- }
- }
- if(minn==INF) break;
- vis[v]=;
- for(int j=;j<=n;j++)
- {
- if(!vis[j]) dis[j]=min(dis[j],dis[v]+g[v][j]);
- }
- }
- }
- int main()
- {
- while(scanf("%d",&n)&&n)
- {
- init();
- scanf("%d%d",&x,&y);
- for(int i=;i<=n;i++)
- {
- scanf("%d",&k);
- if(i-k>=) g[i][i-k]=;
- if(i+k<=n) g[i][i+k]=;
- }
- dij(x);
- printf("%d\n",dis[y]==INF?-:dis[y]);
- }
- return ;
- }
bfs
- #include <iostream>
- #include <algorithm>
- #include <cstring>
- #include <cstdio>
- #include <vector>
- #include <queue>
- #include <cstdlib>
- #include <iomanip>
- #include <cmath>
- #include <ctime>
- #include <map>
- #include <set>
- using namespace std;
- #define lowbit(x) (x&(-x))
- #define max(x,y) (x>y?x:y)
- #define min(x,y) (x<y?x:y)
- #define MAX 100000000000000000
- #define MOD 1000000007
- #define pi acos(-1.0)
- #define ei exp(1)
- #define PI 3.141592653589793238462
- #define ios() ios::sync_with_stdio(false)
- #define INF 0x3f3f3f3f
- #define mem(a) (memset(a,0,sizeof(a)))
- typedef long long ll;
- int vis[],x,n;
- int dis[],y,xx;
- struct node
- {
- int x;
- int step;
- }ans,pos;
- int bfs(int A,int B)
- {
- queue<node>q;
- memset(vis,,sizeof(vis));
- vis[A]=;
- pos.x=A;pos.step=;
- q.push(pos);
- while(!q.empty())
- {
- pos=q.front();
- q.pop();
- if(pos.x==B) return pos.step;
- xx=pos.x+dis[pos.x];
- if(x<=n && !vis[xx])
- {
- ans.x=xx;
- ans.step=pos.step+;
- vis[xx]=;
- q.push(ans);
- }
- xx=pos.x-dis[pos.x];
- if(x>= && !vis[xx])
- {
- ans.x=xx;
- ans.step=pos.step+;
- vis[xx]=;
- q.push(ans);
- }
- }
- return -;
- }
- int main()
- {
- while(scanf("%d",&n)&&n)
- {
- scanf("%d%d",&x,&y);
- for(int i=;i<=n;i++)
- {
- scanf("%d",&dis[i]);
- }
- printf("%d\n",bfs(x,y));
- }
- return ;
- }
HDU 1548 A strange lift(最短路&&bfs)的更多相关文章
- hdu 1548 A strange lift 宽搜bfs+优先队列
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1548 There is a strange lift.The lift can stop can at ...
- HDU 1548 A strange lift(Dijkstra,简单BFS)
题目大意: 电梯有两个选项向上或向下,每层楼有一个参数ki,代表电梯可以再该楼层的基础上向上或向下移动ki层,限制条件是向上不能超过楼层总数n,向下不能少于一.输入总层数n和当前所在层数以及目标层数, ...
- hdu 1548 A strange lift
题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=1548 A strange lift Description There is a strange li ...
- HDU 1548 A strange lift (bfs / 最短路)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1548 A strange lift Time Limit: 2000/1000 MS (Java/Ot ...
- HDU 1548 A strange lift (最短路/Dijkstra)
题目链接: 传送门 A strange lift Time Limit: 1000MS Memory Limit: 32768 K Description There is a strange ...
- hdu 1548 A strange lift (bfs)
A strange lift Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) T ...
- HDU 1548 A strange lift(BFS)
Problem Description There is a strange lift.The lift can stop can at every floor as you want, and th ...
- HDU 1548 A strange lift (Dijkstra)
A strange lift http://acm.hdu.edu.cn/showproblem.php?pid=1548 Problem Description There is a strange ...
- HDU 1548 A strange lift 搜索
A strange lift Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) T ...
随机推荐
- (hdu step 7.2.1)The Euler function(欧拉函数模板题——求phi[a]到phi[b]的和)
题目: The Euler function Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Othe ...
- springMVC --全局异常处理(两种方式)
首先看springMVC的配置文件: <!-- 全局异常配置 start --> <bean id="exceptionResolver" class=" ...
- js插件---图片懒加载lazyload
js插件---图片懒加载lazyload 一.总结 一句话总结:使用异常简单,src里面放加载的图片,data-original里面放原图片,不懂的位置去官网或者github找API就好. 1.laz ...
- Java读取txt文件和覆盖写入txt文件和追加写入txt
//创建文件 public static void createFile(File filename) { try { if(!filename.exists()) { filename.create ...
- 联想杨天 S4130-12 win10改win7 bios参数设置
一.进入bios 开机后按 F1 二.改bion参数 1.移动到 save& Exit ,修改 OS optimized defaults 为“Disbled” 再 “F9” 保存 2. ...
- 重写prototype原型后哪些东西改变了
参考<JavaScript高级教程>实例看: 1.重写原型对象后,首先原型对象的constructor属性值(constructor的指向)会发生改变. function Person() ...
- 【Uva 1601】The Morning after Halloween
[Link]: [Description] 给你一张平面图; 最多可能有3只鬼; 给出这几只鬼的初始位置; 然后,这几只鬼有各自的终点; 每秒钟,这几只鬼能同时移动到相邻的4个格子中的一个 任意两只鬼 ...
- 几种类型的db,以及最新的db排名,看一下
5月数据库排名: http://geek.csdn.net/news/detail/196118 另外这篇文章里面提到了一些内嵌式数据库: http://blog.csdn.net/leagoal/a ...
- 对string的一些扩展函数
对string作了一些扩展,包含string转化为int.string转化为double.string转化为bool.打印系统当前时间.但没有解决数据溢出的问题,请大神帮忙解决! //头文件 /*pa ...
- ZOJ 3690 & HDU 3658 (矩阵高速幂+公式递推)
ZOJ 3690 题意: 有n个人和m个数和一个k,如今每一个人能够选择一个数.假设相邻的两个人选择同样的数.那么这个数要大于k 求选择方案数. 思路: 打表推了非常久的公式都没推出来什么可行解,好不 ...