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