A strange lift
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 24630 Accepted Submission(s): 8898
Problem DescriptionThere 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"?
InputThe 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.
OutputFor 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 Input5 1 53 3 1 2 50
Sample Output3
BFS
附AC代码:
1 #include2 using namespace std; 3 4 int n,s,e; 5 int ss[210],vis[210]; 6 7 struct node{ 8 int x,step; 9 };10 11 int bfs(){12 queue Q;13 node a,next;14 a.x=s;15 a.step=0;16 vis[s]=1;17 Q.push(a);18 while(!Q.empty()){19 a=Q.front();20 Q.pop();21 if(a.x==e)22 return a.step;23 for(int i=-1;i<=1;i+=2){24 next=a;25 next.x+=i*ss[next.x];26 if(next.x<=0||next.x>n||vis[next.x])27 continue;28 vis[next.x]=1;29 next.step++;30 Q.push(next);31 }32 }33 return -1;34 } 35 36 int main(){37 while(cin>>n&&n){38 cin>>s>>e;39 for(int i=1;i<=n;i++){40 cin>>ss[i];41 }42 memset(vis,0,sizeof(vis));43 cout< <