1007 最短路

来源:互联网 发布:map生成json 编辑:程序博客网 时间:2024/06/05 12:32
                                                                       A strange lift
Time Limit : 2000/1000ms (Java/Other) Memory Limit : 65536/32768K (Java/Other)
Total Submission(s) : 23 Accepted Submission(s) : 8
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 53 3 1 2 50

Sample Output
3


题意:神奇的电梯 只能按上或者按下 第二行输入的是每次向上或者向下的楼层、、、、

没想出来的题,后来看了代码将每次上升或者下降的步长计数为1 然后求最短路

代码:

#include<iostream>#include<cstring>using namespace std;const int manx=99999;bool vis[manx];int map[300][300];int dir[300];int n;void dijksta(int v){    int u;    int temp;   // cout<<v<<"       12312313"<<endl;    memset(vis,0,sizeof(vis));    for (int i=1;i<=n;i++)    {        dir[i]=map[v][i];           }    vis[v]=1;    dir[v]=0;    for (int i=1;i<=n;i++)    {        temp=manx;        for (int j=1;j<=n;j++)        {            if (!vis[j]&&dir[j]<temp)            {                temp=dir[j];                u=j;            }        }        vis[u]=1;        for (int j=1;j<=n;j++)        {            if (!vis[j]&&dir[j]>dir[u]+map[u][j])                dir[j]=dir[u]+map[u][j];        }    }}int main(){int k;int a,b;while(cin>>n){if(n==0) break;     cin>>a>>b;for(int i=1;i<=n;i++) for(int j=1;j<=n;j++)  { map[i][j]=map[j][i]=manx;      }   for(int i=1;i<=n;i++) { cin>>k;  if((i+k)<=n) map[i][i+k]=1; if((i-k)>=1) map[i][i-k]=1; }/*    for(int i=1;i<=n;i++) {    for(int j=1;j<=n;j++)  {  cout<<map[i][j]<<" ";  }  cout<<endl; }*/    dijksta(a); if(dir[b]==manx) cout<<"-1"<<endl; else// cout<<b<<endl;   cout<<dir[b]<<endl;}    return 0;}


 

 

 

原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 孩子不起床上学怎么办 孕晚期起床困难怎么办 腰间盘疼痛起床困难怎么办 小学生做作业拖拉怎么办 被手机贷起诉怎么办 大人睡颠倒了怎么办 熬夜长斑了怎么办 ps遇到文件尾怎么办 pdf用ps打不开怎么办 ps安装不上怎么办 工作原因经常熬夜怎么办 熬夜皮肤暗黄怎么办 经常熬夜睡眠不好怎么办 经常熬夜皮肤不好怎么办 皮肤熬夜变暗黄怎么办 晚上上夜班白天睡不着怎么办 熬夜肾虚怎么办才能好 经常熬夜口气重怎么办 晚上睡不着白天起不来怎么办 晚上熬夜白天睡不着怎么办 在外打工孩子上学怎么办 婴儿脸干燥发红怎么办 宝宝脸上红点点怎么办 药流出血量少怎么办 药流喝药第一天发烧了怎么办 药流第一天量很少怎么办 小孩不想吃饭口臭怎么办 晚上上班白天睡不着怎么办 新车刮漆了怎么办 药流出血多怎么办 药流第二天没血怎么办 有异味怎么办%3f 养狗后悔了怎么办 买狗后悔了怎么办 小孩子不想去幼儿园怎么办 小孩子不想上幼儿园怎么办 想当兵视力不够怎么办 当兵裸眼视力差怎么办 去公司报道行李怎么办 报道第一天出糗怎么办 痛经一直想拉屎怎么办