C - A strange lift 解题报告

来源:互联网 发布:费正清 中国新史 知乎 编辑:程序博客网 时间:2024/06/07 00:20
C - A strange lift
Time Limit:1000MS    Memory Limit:32768KB    64bit IO Format:%I64d & %I64u

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后直接结束程序了啊啊啊啊啊啊啊,真是智硬了啊。

贴上代码:

//A strange lift#include <stdio.h>int Line[1000];int Times[1000];int LineI,LineJ;int Visited[201] = {0};int ki[201];int n,a,b;void AddToLine(int Floor);int main(){int i,flag;while (1){scanf("%d",&n);if (n == 0) break;scanf("%d %d",&a,&b);for (i = 1;i <= n;i ++){scanf("%d",&ki[i]);Visited[i] = 0;}flag = 1;Line[0] = a;Visited[a] = 1;Times[0] = 0;LineI = 0;LineJ = 1;while (Line[LineI] != b){AddToLine(Line[LineI] + ki[Line[LineI]]);AddToLine(Line[LineI] - ki[Line[LineI]]);LineI ++;if (LineI >= LineJ){flag = 0;}}if (flag)printf("%d\n",Times[LineI]);elseprintf("-1\n");}return 0;}void AddToLine(int Floor){if (Floor <= n && Floor >= 1 && Visited[Floor] == 0){Line[LineJ] = Floor;Visited[Floor] = 1;Times[LineJ] = Times[LineI] + 1;//printf("Floor %d Times %d\n",Line[LineJ],Times[LineJ]);LineJ ++;}}


0 0
原创粉丝点击