1078. weird elevator, 1078 strange elevator

Source: Internet
Author: User

1078. weird elevator, 1078 strange elevator
1078. weird elevator (Standard IO) Time Limit: 1000 MS space limit: 262144 KB Specific Limit

The elevator can be stopped on each floor of the building, and there is a digital Ki (0 <= Ki <= N) on floor I (1 <= I <= N ). The elevator has only four buttons: On, off, up, down. The upper and lower layers are equal to the number on the current floor. Of course, if the requirements cannot be met, the corresponding button will fail. For example, 3 3 1 2 5 represents Ki (K1 = 3, K2 = 3 ,......), From the first floor. On the first floor, you can press "up" to go to the fourth floor. clicking "down" does not work because there is no-2 floor. How many buttons should be pushed from building A to Building B?   There are two rows in the input file. The first row is A positive integer separated by spaces, indicating N, A, B (1 ≤ N ≤ 200, 1 ≤ A, B ≤ N ), the second act is N positive integers separated by spaces, representing Ki. The output file contains only one line, that is, the minimum number of buttons. If it cannot be reached, the output is-1.   Sample Input
5 1 53 3 1 2 5
Sample output
  Data range restrictions
 1 #include<iostream> 2 using namespace std; 3 int lc[1000001]; 4 int tot=10001; 5 int n; 6 int beginn; 7 int endn; 8 int vis[1001]; 9 void dfs(int now,int step)10 {11     if(step>tot)12     return;13     if(now==endn)14     {15         if(step<tot)16         tot=step;17         return;18     }19     else20     {21         vis[now]=1;22         if(now-lc[now]>0&&vis[now-lc[now]]==0)23         {24             dfs(now-lc[now],step+1);25         }26         if(now+lc[now]<=n&&vis[now+lc[now]]==0)27         {28             dfs(now+lc[now],step+1);29         }30         vis[now]=0;31     }32 }33 int main()34 {35     36     cin>>n>>beginn>>endn;37     for(int i=1;i<=n;i++)38     {39         cin>>lc[i];40     }41     dfs(beginn,0);42     if(tot!=10001)43     cout<<tot;44     else 45     {46         cout<<-1;47     }48     return 0;49 }


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.