2016SDAU編程練習二1013


A strange lift 


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 &lt;= Ki &lt;= N) on every floor.The lift have just two buttons: up and down.When you at floor i,if you press the button &quot;UP&quot; , 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 &quot;DOWN&quot; , 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 &quot;UP&quot;, and you'll go up to the 4 th floor,and if you press the button &quot;DOWN&quot;, 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.<br>Here comes the problem: when you is on floor A,and you want to go to floor B,how many times at least he havt to press the button &quot;UP&quot; or &quot;DOWN&quot;?<br>
 


Input
The input consists of several test cases.,Each test case contains two lines.<br>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.<br>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<br>3 3 1 2 5<br>0 


Sample Output

3


題意:有一個特別的電梯,第i層有一個對應的數字ki, 對於第i層按上升鍵up可升上到i+k[i]層,按下降鍵down到達i-k[i]層,到達的樓層最高不能超過n層,最低不能小於1層。給你一個起點A和終點B,問最少要按幾次上升鍵或者下降鍵到達目的地。

思路:乍一看有點像貪心,BFS

感想:搜索好。。。。長。。。。。

AC代碼:

#include<iostream>
#include<string.h>
#include<set>
#include<stdio.h>
#include<vector>
#include<algorithm>
#include<numeric>
#include<math.h>
#include<string.h>
#include<sstream>
#include<stdio.h>
#include<string>
#include<cstdlib>
#include<algorithm>
#include<iostream>
#include<queue>
#include<queue>
#include<iomanip>
#include<cstdio>
using namespace std;
struct node
{
   int x ;
   int y ;
}n1,n2,m;
int c[2000],d[2000];  //標記數組,和一個記錄樓層狀態的數組
int flag ;                 //記錄能否到達目標樓層
int n , a , b ;          //n層,a,起始樓層,b,目的樓層
int main(  )
{
    while( scanf("%d" , &n ) , n )
    {
       if( n== 0 ) break ;        //結束
       cin>>a>>b;
       for( int i= 1;  i<= n ; i++ )     //初始化
       { cin>>d[i] ; c[i] = 0 ; }
       flag = 0 ;                      //初始爲0
       n1.x = a ; n1.y = 0 ;        //把a開始所在樓層賦給結構體, 所走步數爲0
       queue<node>Q;               //建立隊列Q
       Q.push(n1);                        //入隊
       c[n1.x] = 1 ;          //標記已經走過
       while( !Q.empty() )       //直到爲空爲止
       {
          m = Q.front() ;            //把隊列中第一個值賦給結構體變量m
          Q.pop();                   //出隊
          if( m.x == b )            //如果到達終點便退出
          { flag = 1 ; break ; }
          n1.x = m.x - d[m.x];  //按向下的按鈕
          n2.x = m.x + d[m.x]; //按向上的按鈕
          if( n1.x>0 && n1.x<= n&& !c[n1.x] )//向下
          {
                n1.y = m.y + 1 ;         //步數加一
                c[n1.x] = 1 ;       //標記
                Q.push(n1);
              }
            if( n2.x>0 && n2.x<= n && !c[n2.x] )//向上
             {
                n2.y = m.y+1 ;
                c[n2.x] = 1 ;
                Q.push(n2);
             }
       }
       if( flag ) cout<<m.y<<endl;
       else cout<<"-1"<<endl;
    }
    return 0;
}

發佈了59 篇原創文章 · 獲贊 0 · 訪問量 7469
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章