HDU 1548 最短路---A strange life

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层有个数字k,那么这一层只能上k层或下k层,但是不能低于一层或高于n层(即超出有效范围), 给定起点与终点,求出最少要按几次键才能到达。
思路:这道题是单向边,我们只要让map里面的值全部为1就可以统计次数了。 代码实现:BFS
   
   
  1. #include <stdio.h>  
  2. #include <string.h>  
  3. #include <algorithm>  
  4. #include <queue>  
  5. using namespace std;  
  6.   
  7. int n,s,e;  
  8. int ss[205],vis[205];  
  9.   
  10. struct node  
  11. {  
  12.     int x,step;  
  13. };  
  14.   
  15. int check(int x)  
  16. {  
  17.     if(x<=0 || x>n)  
  18.     return 1;  
  19.     return 0;  
  20. }  
  21.   
  22. int BFS()  
  23. {  
  24.     queue<node> Q;  
  25.     node a,next;  
  26.     int i;  
  27.     a.x = s;  
  28.     a.step = 0;  
  29.     vis[s] = 1;  
  30.     Q.push(a);  
  31.     while(!Q.empty())  
  32.     {  
  33.         a = Q.front();  
  34.         Q.pop();  
  35.         if(a.x == e)  
  36.         return a.step;  
  37.         for(i = -1;i<=1;i+=2)  
  38.         {  
  39.             next = a;  
  40.             next.x +=i*ss[next.x];  
  41.             if(check(next.x) || vis[next.x])  
  42.             continue;  
  43.             vis[next.x] = 1;  
  44.             next.step++;  
  45.             Q.push(next);  
  46.         }  
  47.     }  
  48.     return -1;  
  49. }  
  50.   
  51. int main()  
  52. {  
  53.     int i,j;  
  54.     while(~scanf("%d",&n),n)  
  55.     {  
  56.         scanf("%d%d",&s,&e);  
  57.         for(i = 1;i<=n;i++)  
  58.         scanf("%d",&ss[i]);  
  59.         memset(vis,0,sizeof(vis));  
  60.         printf("%d\n",BFS());  
  61.     }  
  62.   
  63.     return 0;  
  64. }  






 Dijkstra:

  1. #include <stdio.h>  
  2. #include <string.h>  
  3. #include <algorithm>  
  4. using namespace std;  
  5.   
  6. const int inf = 1<<30;  
  7.   
  8. int n;  
  9. int map[205][205];  
  10. int a[205],cnt;  
  11. int vis[205],cast[205];  
  12.   
  13. void Dijkstra(int s,int e)  
  14. {  
  15.     int i,j,min,pos;  
  16.     memset(vis,0,sizeof(vis));  
  17.     for(i = 0; i<n; i++)  
  18.         cast[i] = map[s][i];  
  19.     cast[s] = 0;  
  20.     vis[s] = 1;  
  21.     for(i = 1; i<n; i++)  
  22.     {  
  23.         min = inf;  
  24.         for(j = 0; j<n; j++)  
  25.         {  
  26.             if(cast[j]<min && !vis[j])  
  27.             {  
  28.                 pos = j;  
  29.                 min = cast[j];  
  30.             }  
  31.         }  
  32.         if(min == inf)  
  33.             break;  
  34.         vis[pos] = 1;  
  35.         for(j = 0; j<n; j++)  
  36.         {  
  37.             if(cast[pos]+map[pos][j]<cast[j] && !vis[j])  
  38.                 cast[j] = cast[pos]+map[pos][j];  
  39.         }  
  40.     }  
  41. }  
  42.   
  43. int main()  
  44. {  
  45.     int i,j,s,e,x,y;  
  46.     while(~scanf("%d",&n),n)  
  47.     {  
  48.         scanf("%d%d",&s,&e);  
  49.         s--,e--;  
  50.         for(i = 0; i<n; i++)  
  51.             for(j = 0; j<n; j++)  
  52.                 map[i][j] = inf;  
  53.         for(i = 0; i<n; i++)  
  54.         {  
  55.             scanf("%d",&a[i]);  
  56.             if(i+a[i]<n)  
  57.                 map[i][i+a[i]] = 1;  
  58.             if(i-a[i]>=0)  
  59.                 map[i][i-a[i]] = 1;  
  60.         }  
  61.         Dijkstra(s,e);  
  62.         printf("%d\n",cast[e]==inf?-1:cast[e]);  
  63.     }  
  64.   
  65.     return 0;  


 
 
1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。 1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。 1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。
1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。 1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。 1、资源项目源码均已通过严格测试验证,保证能够正常运行; 、2项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 、资5源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值