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"?
 
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,向下不过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 ;
}
 

最新文章

  1. 新手学跨域之iframe
  2. Hive UDF初探
  3. 【经验】angularjs 实现带查找筛选功能的select下拉框
  4. Objective-C 编程艺术 (Zen and the Art of the Objective-C Craftsmanship 中文翻译)
  5. activity之栈管理
  6. UILabel + 导入字体
  7. Android应用开发提高篇(4)-----Socket编程(多线程、双向通信)
  8. Mybatis基本用法--上
  9. 不能执行已经释放掉的Script代码!(已解决)
  10. 开启远程桌面连接windows的方法以及遇到的问题
  11. windows上xshell6的安装
  12. django权限管理(Permission)
  13. ESP8266 ESP-01调试方法
  14. nginx 本地映射
  15. 历届试题 大臣的旅费-(树的直径+dfs)
  16. 【题解】 bzoj1864: [Zjoi2006]三色二叉树 (动态规划)
  17. [转]oracle 11g jdbc jar包在哪个文件目录
  18. centos升级glibc动态库
  19. android Timer TimerTask用法笔记
  20. 深入浅出Spring(二) IoC详解

热门文章

  1. Jeff Dean专访,有不少干货
  2. BOOST_CLASS_EXPORT
  3. POSTGRESQL NO TABLE
  4. 学习总结--Dom
  5. FreeBSD是UNIX系统的重要分支,其命令与Linux大部分通用,少部分是其特有。
  6. Debounce 和 Throttle【转载】
  7. java web应用调用python深度学习训练的模型
  8. 【Henu ACM Round#17 D】Hexagons!
  9. HDU 5384 Danganronpa (AC自己主动机模板题)
  10. 基于SVM的数据分类预測——意大利葡萄酒种类识别