Weird elevator.

Source: Internet
Author: User

Title Description

Oh, one day I had a dream, dreamed of a very strange elevator. Elevators can be parked on each floor of the building, and there is a number Ki (0<=ki<=n) on the first floor (1<=i<=n). Elevators have only four buttons: on, off, up, down. The number of layers above and below equals that number on the current floor. Of course, if the requirements are not met, the corresponding buttons will fail. For example: 3 3 1 2 5 represents Ki (K1=3,k2=3,......), starting from the first floor. On the first floor, press "up" can go to the 4 floor, press "down" is not working, because there is no-2 floor. So, how many buttons do I have to press a few times from floor A to floor B?

input/output format

Input format:

There are two lines in the input file, the first act three positive integers separated by spaces, representing N,a,b (1≤n≤200, 1≤a,b≤n), and the second act N a positive integer separated by a space, representing Ki.

Output format:

Output file only one line, that is, the minimum number of keystrokes, if not reachable, then output-1.

Problem Solving Ideas:

Heard should use recursion, but pushed for a while and did not launch a ghost, so the DFS search.

Here's the code:

#include <iostream>
#include <cstdio>
using namespace Std;
int lift[2008],step[2008];
int n,a,b;
int main ()
{
memset (lift,0,sizeof (lift));
memset (step,0,sizeof (step));
cin>>n>>a>>b;
for (int i=1;i<=n;i++)
{
cin>>lift[i];
Step[i]=-1;
}
step[1]=0;
int l=1,p=0,j=0;
while (l>0&&p<=n)
{
l=0;
for (int i=1;i<=n;i++)
if (step[i]==p)
{
J=i+lift[i];
if (j<=n&&step[j]==-1)
{
step[j]=p+1;
l=1;
}
J=i-lift[i];
if (j>0&&step[j]==-1)
{
step[j]=p+1;
l=1;
}
}
p++;
}
cout<<step[b];
return 0;
}

                                                                                                                                                                                                                                                                                          

Strange Elevator

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.