P1135 information off the valley Olympiad 1360 through a strange elevator (lift)

Original title link:

Ochiya  https://www.luogu.com.cn/problem/P1135

Orsay information through a  http://ybt.ssoier.cn:8088/problem_show.php?pid=1360

topic:

1360: Strange elevator (lift)


Time limit: 1000 ms Memory Limit: 65536 KB
Submissions: By 4824 the number: 2058

Description [title]

Each floor of the building elevator can be stopped, and the i-th floor ( . 1 i N ) has a number on the K i ( 0 ≤ = K i ≤ = N ) . Elevator only four buttons: on, off, up, down. The upper and lower layers is equal to that number on the current floor. Of course, if you can not meet the requirements, the corresponding button will fail. For example: 3 3 1 2 5it represents K I ( K . 1 = . 3 , K 2 = . 3 , ... ... ) , from the first floor. On the first floor, press "up" to be4th floor, press the "down" does not work, because there is no-2 floor. So, from A floor toBuilding B button a few times at least Yaoan it?

[Enter]

There are two lines, the first three acts positive integers separated by spaces, represents N , A , B ( . 1 N 200 is , . 1 A , B N ) , the behavior of the second N space-separated positive integer representing K I .

[Output]

Line, i.e., a minimum number of keystrokes, if not reach, output - . 1 .

[Sample input]

5 1 5
3 3 1 2 5

[Sample Output]

3



This is a search problem, where you can use broad search.
First, write a queue a, h represents the current in a few floor, C for a few steps.
Initialization is first floor initially queued,
the search process is to say the first element of the team removed the tail and then added to the first element in the floor where the floor can reach the button, and the number is incremented by one if the output reaches the specified number of times on the floor, Finally, the search continues to the next head ++,
loop condition is non-empty queue, i.e., head <= tail,
if the cycle has not reached the end of the designated floor represents not reach, output -1.

Code:
c:
#include <stdio.h>
 struct K {
     int H, C; // H represents the current which floor, C for a few steps 
} A [ 201 ]; // wide search queue 
int B [ 201 ]; // store data 
int YE [ 201 ]; // record whether the floor reaches 
int m, n-, R & lt, S;
 int head, tail;
 int main () { 
    Scanf ( " % D% D% D " , & n-, & R & lt, & S) ;
     for ( int I = . 1 ; I <= n-; I ++) Scanf ( " % D " , B + I);
     IF(R & lt == S) {the printf ( " 0 " ); return  0 ;} // If the initial floor and the destination floor is not the same as walking 
    A [ 0 ] .h = R & lt; A [ 0 ] .c = 0 ; // floor initially enqueued 
    tail head = = 0 ;
     the while (head <= tail) { // wide search cycling conditions queue is non-empty 
        IF B [A [head] .h] [head] (A + .h <= n-YE && [A [head] .h + B [A [head] .h]] == 0 ) { // go up 
            YE [A [head] .h + B [A [head] .h]] = . 1 ; / / recording has come to the floor 
            a [++ tail] .h = [head] .h a + B [a [head] .h]; // floor after the key can reach the tail floor added
            A [tail] .c = A [head] .c + . 1 ; // and add a number of 
            IF (A [tail] .h == S) {the printf ( " % D " , A [tail] .c); return  0 ;} // if the arrival floor specified frequency and outputs 
            }
         IF [A [head] .h] [head] (A .hb> = . 1 && YE [A [head] .hb [A [head] .h]] = = 0 ) { // go down 
            YE [A [head] .hb [A [head] .h]] = . 1 ; // record the floor has gone through 
            a [++ tail] .h = a [head] .hb [a [head] .h]; // floor after the key can reach the tail floor added 
            a [tail] .c = a [head] .c + . 1 ; // and add a number of times
            IF (A [tail] .h == S) {the printf ( " % D " , A [tail] .c); return  0 ;} // if the arrival floor specified frequency and outputs 
            } 
    head ++; // for the first plus. 1 
    } 
    the printf ( " -1 " ); return  0 ; // If the loop has not reached the end of the designated floor represents not reach, output -1 
}

pascal:

type K = Record 
    H: LongInt; 
    C: LongInt; // H represents the current which floor, C for a few steps
 End ;
 var 
    A: Array [ 0 .. 200 is ] of K; // wide search queue 
    B: Array [ . 1 .. 200 is ] of LongInt; // store data 
    YE: Array [ . 1 .. 200 is ] of Boolean; // if recording reaches floor 
    m: LongInt; 
    n-: LongInt; 
    R & lt: LongInt; 
    S: LongInt; 
    head: LongInt ; 
    tail: LongInt ;
    I: LongInt; 
the begin 
    Read (n-, R & lt, S); 
    for I: = . 1  to n- do Read (B [I]);
     IF R & lt S = the then  the begin 
        writeln ( 0 ); Exit (); // If the initial floor and not have to go the same destination floor
     End ; 
    A [ 0 ] .h: = R & lt; A [ 0 ] .c: = 0 ; // initial floor queued 
    head: = 0 ; tail: = head;
     the while head <= tail do  the begin // wide search cycling conditions queue is non-empty
         IF (A [head] .h + B [A [head] .h] <= n-) and(YE [A [head] .h + B [A [head] .h]] = to false) the then  the begin // go up 
            YE [A [head] .h + B [A [head] .h]]: = true; // record the floor has come 
            tail: = tail + . 1 ; 
            a [tail] .h: = [head] .h a + B [a [head] .h]; // floor can reach the button Add the tail floor 
            a [tail] .c: = a [head] .c + . 1 ; // and add a number of
             IF a [tail] .h = S the then  the begin  
                writeln (a [tail] .c); Exit () ; // if the arrival floor specified frequency and outputs
             End ;
         End ;
         IF [A [head] .h] [head] (A .hb> = . 1) And (YE [A [head] .hb [A [head] .h]] = to false) the then  the begin // go down 
            YE [A [head] .h -b [A [head] .h]]: = true; // record the floor has come 
            tail: = tail + . 1 ; 
            a [tail] .h: = [head] [a [head] .h] a .hb; // floor can reach the floor after the key Add the tail 
            a [tail] .c: = a [head] .c + . 1 ; // and add a number of
             IF a [tail] .h = S the then  the begin 
                writeln (a [tail] .c); Exit (); // If it reaches the specified number of outputs floor
             End ;
         End ; 
        head: = head + 1 ; // to first add 1 
     End ; 
    writeln ( - 1 ); // if the cycle has not reached the end of the designated floor on behalf can not be reached, the output - 1 
End .

I could write well, if there are problems, please noted, thank you.

Guess you like

Origin www.cnblogs.com/sy666/p/12403973.html