HDU 1548 A strange lift(最短路&&bfs)
2024-08-31 17:26:45
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
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"?
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.
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
Sample Output
3
最短路是有向图,不是无向图,一个电梯向上不过n,向下不过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 ;
}
最新文章
- 新手学跨域之iframe
- Hive UDF初探
- 【经验】angularjs 实现带查找筛选功能的select下拉框
- Objective-C 编程艺术 (Zen and the Art of the Objective-C Craftsmanship 中文翻译)
- activity之栈管理
- UILabel + 导入字体
- Android应用开发提高篇(4)-----Socket编程(多线程、双向通信)
- Mybatis基本用法--上
- 不能执行已经释放掉的Script代码!(已解决)
- 开启远程桌面连接windows的方法以及遇到的问题
- windows上xshell6的安装
- django权限管理(Permission)
- ESP8266 ESP-01调试方法
- nginx 本地映射
- 历届试题 大臣的旅费-(树的直径+dfs)
- 【题解】 bzoj1864: [Zjoi2006]三色二叉树 (动态规划)
- [转]oracle 11g jdbc jar包在哪个文件目录
- centos升级glibc动态库
- android Timer TimerTask用法笔记
- 深入浅出Spring(二) IoC详解