Weird elevator (Basic Search)

Source: Internet
Author: User
/*
Strange elevator (normal)

Time Limit: 1000 ms memory limit: 65536 K
Total submit: 200 accepted: 63

Description

Well, one day I dreamed of a strange elevator. The elevator is available 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?

Input

There are two rows in the input file. The first act 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.

Output

The output file contains only one line, that is, the minimum number of buttons. If the key cannot be reached, the output is-1.

Sample Input

5 1 5
3 3 1 2 5

Sample output

3

*/
# Include <stdio. h>
# Deprecision Max 501

Int A, B, N;
Int V [Max], flag = 0;

Void trytofind (int x, int total)
{
If (X-V [x] = B | x + V [x] = B)
{

If (total <flag | flag = 0)
Flag = total;

}
Else
{
If (x + V [x] <= N)
Trytofind (x + V [X], total + 1 );
If (X-V [x]> = 1)
Trytofind (X-V [X], total + 1 );
}

}
Int main (void)
{
Int I, J, K;
Scanf ("% d", & N, & A, & B );
For (I = 1; I <= N; I ++)
Scanf ("% d", & V [I]);

Trytofind (A, 1 );

If (FLAG)
Printf ("% d", flag );
Else
Printf ("-1 ");

System ("pause ");

Return 0;

}

 

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.