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 least he 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.
 
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 5
3 3 1 2 5
0
 
Sample Output
3
 
 
题意:一个n层的奇怪的电梯,每层有两个键,上、下。在第i层有一个数字啊a[i],表示能从这一层到第i-a[i]层和第i+a[i]层。问从指定层到指定层的最少按键次数。
解析:两种方法。BFS和单源最短路。
 
代码如下:
 
BFS:
 # include<iostream>
# include<cstdio>
# include<queue>
# include<cstring>
# include<algorithm>
using namespace std;
struct node
{
int pos,time;
};
int n,a,b;
int f[];
int mark[];
void BFS()
{
queue<node>q;
memset(mark,,sizeof(mark));
node now;
now.pos=a,now.time=;
q.push(now);
mark[a]=;
while(!q.empty())
{
now=q.front();
q.pop();
if(now.pos==b){
printf("%d\n",now.time);
return ;
}
node nxt;
nxt.pos=now.pos+f[now.pos];
nxt.time=now.time+;
if(nxt.pos<=n&&!mark[nxt.pos]){
q.push(nxt);
mark[nxt.pos]=;
}
nxt.pos=now.pos-f[now.pos];
nxt.time=now.time+;
if(nxt.pos>=&&!mark[nxt.pos]){
q.push(nxt);
mark[nxt.pos]=;
}
}
printf("-1\n");
}
int main()
{
while(scanf("%d",&n)&&n)
{
scanf("%d%d",&a,&b);
for(int i=;i<=n;++i)
scanf("%d",&f[i]);
BFS();
}
}

spfa:

 # include<iostream>
# include<cstdio>
# include<queue>
# include<cstring>
# include<algorithm>
using namespace std;
const int INF=<<;
int mp[][];
int f[],a,b,n,dis[];
void init()
{
for(int i=;i<=n;++i)
for(int j=;j<=n;++j)
mp[i][j]=(i==j)?:INF;
for(int i=;i<=n;++i){
if(i+f[i]<=n)
mp[i][i+f[i]]=;
if(i-f[i]>=)
mp[i][i-f[i]]=;
}
}
void spfa()
{
init();
fill(dis+,dis+n+,INF);
queue<int>q;
q.push(a);
dis[a]=;
while(!q.empty()){
int u=q.front();
q.pop();
for(int i=;i<=n;++i){
if(dis[i]>dis[u]+mp[u][i]){
dis[i]=dis[u]+mp[u][i];
q.push(i);
}
}
}
if(dis[b]==INF)
printf("-1\n");
else
printf("%d\n",dis[b]);
}
int main()
{
while(scanf("%d",&n)&&n)
{
scanf("%d%d",&a,&b);
for(int i=;i<=n;++i)
scanf("%d",f+i);
spfa();
}
return ;
}
 

最新文章

  1. Account problem-There may be a problem with your account. Please contact us. Sign out
  2. POJ 2398 - Toy Storage 点与直线位置关系
  3. a标签的背景图在ie8下不显示的问题
  4. Ubuntu下 hadoop2.5.1 (伪分布模式) 配置工作
  5. GCD之死锁体会
  6. Express4.x API (二):Request (译)
  7. 诶西,JavaScript学习记录。。。。。。
  8. bzoj3756pty的字符串(后缀自动机+计数)
  9. leetcode笔记--SUM问题
  10. 0.2:Game and Art
  11. linux驱动编写之中断处理
  12. #ifndef/#define/#endif使用详解
  13. Cognos11中Dashboard和HTML页面的简单集成
  14. Android调用系统软键盘
  15. 3-20 标准库:find库; 学习编程语言3节课(大多是旧识,全*栈)3-21 面向对象. Percent Strings; 元编程和Rails的相互理解
  16. @ModelAttribute三个作用:
  17. soj1762.排座椅
  18. 基尼系数(Gini coefficient),洛伦茨系数
  19. android 软键盘回车键捕获
  20. Vue学习笔记之Vue的箭头函数

热门文章

  1. Antlr4 SQL Query 解析实例
  2. Python数据分析入门之pandas基础总结
  3. java 如果仅输出一位和要输出多位格式的输出问题,利用boolean值.
  4. 如何在Twitter开发者平台上注册自己的应用
  5. SublimeText3常用快捷键和优秀插件(亲测)
  6. UVa 1610 聚会游戏
  7. linux awk命令详解--转载
  8. Cocos2d-x学习笔记(二)AppDelegate类详解
  9. python 等比数列
  10. MongoDB(课时21 索引)