"Portal": http://www.luogu.org/problem/show?pid=1135
---------------------------------------------------Topics----------------------------------------------------------
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.
Input/Output sample
Input Sample # #:
LIFT. IN5 1 53 3 1 2 5
Sample # # of output:
LIFT. OUT3
-------------------------------------------------the process of solving problems--------------------------------------------------------
Just started to write the BFS took 80 points, the wrong two test points, although the pin seems a bit redundant, but still too lazy to delete qaq,
//Initial code (BFS version)varHead,tail:longint; //Head to tail pointer temp:longint; I,n,a,b,sum:longint; Num:Array[1.. $] ofLongint; F:Array[0..201] of Record//record type, data is used to record the number in the queue, the pin is used to record how many data:longint have been gone; Pin:shortint; End; Boo:Array[1.. $] ofBoolean;//-------------------------------------------------procedureprintf (uuu:longint); //Output ProcessbeginWriteln (F[uuu].pin); Halt;End;//-------------------------------------------------beginreadln (N,A,B); fori:=1 toN DoRead (Num[i]); //Output ProcessifA=b Then begin//if the starting point equals the end point, output 0 writeln (0); Halt End; Sum:=n; //sum is used to record the remaining number of layers in the walk head:=0; tail:=1; Fillchar (Boo,sizeof (Boo), true); //Initialize f[1].data:=a; f[1].pin:=0; boo[1]:=false; Dec (sum); //The error is in this line .BFS starts-------------------------------------------------------------------- whileHead<tail Do beginInc (head); Temp:=f[head].data+num[f[head].data]; //Move up .if(temp>=1) and(temp<=n) and(Boo[temp]) Then begin ifsum=0 Then beginWriteln (-1); HaltEnd; //This is a little chicken, but lazy to delete Inc (tail); F[tail].data:=temp; f[tail].pin:=f[head].pin+1; Boo[temp]:=false; Dec (sum); ifTemp=b Thenprintf (tail); //The output is reached on the designated floorEnd; Temp:=f[head].data-num[f[head].data]; //go down.if(temp>=1) and(temp<=n) and(Boo[temp]) Then begin//Ibid .ifsum=0 Then beginWriteln (-1); HaltEnd; Inc (tail); F[tail].data:=temp; f[tail].pin:=f[head].pin+1; Boo[temp]:=false; Dec (sum); ifTemp=b Thenprintf (tail); End; End;BFS End--------------------------------------------------------------------Writeln (-1); When the BFS found no way to reach the output-1End.
Two wrong test points, one is because I used the shortint (back Shortint), and another is because of the wrong initialization when the assignment of the Boo Boolean array subscript (see line 27th)
The correct BFS version is as follows:
//correct code (BFS version)varHead,tail:longint; //Head to tail pointer temp:longint; I,n,a,b,sum:longint; Num:Array[1.. $] ofLongint; F:Array[0..201] of Record//record type, data is used to record the number in the queue, the pin is used to record how many data:longint have been gone; Pin:integer; //refusing Shortint, starting from me ...End; Boo:Array[1.. $] ofBoolean;//-------------------------------------------------procedureprintf (uuu:longint); //Output ProcessbeginWriteln (F[uuu].pin); Halt;End;//-------------------------------------------------beginreadln (N,A,B); fori:=1 toN DoRead (Num[i]); //Output ProcessifA=b Then begin//if the starting point equals the end point, output 0 writeln ('0'); Halt End; Sum:=n; //sum is used to record the remaining number of layers that can go "chicken ribs ..." head:=0; tail:=1; Fillchar (Boo,sizeof (Boo), true); f[1].data:=a; f[1].pin:=0; Boo[a]:=false; Dec (sum); //corrected errorBFS starts-------------------------------------------------------------------- whileHead<tail Do beginInc (head); Temp:=f[head].data+num[f[head].data]; //Move up .if(temp<=n) and(Boo[temp]) Then begin ifsum=0 Then beginWriteln ('-1'); HaltEnd; //This is a little chicken, but lazy to delete Inc (tail); F[tail].data:=temp; f[tail].pin:=f[head].pin+1; Boo[temp]:=false; Dec (sum); ifTemp=b Thenprintf (tail); //The output is reached on the designated floorEnd; Temp:=f[head].data-num[f[head].data]; //go down.if(temp>0) and(Boo[temp]) Then begin//Ibid .ifsum=0 Then beginWriteln ('-1'); HaltEnd; Inc (tail); F[tail].data:=temp; f[tail].pin:=f[head].pin+1; Boo[temp]:=false; Dec (sum); ifTemp=b Thenprintf (tail); End; End;BFS End--------------------------------------------------------------------Writeln ('-1'); When the BFS found no way to reach the output-1End.
The title tag is recursive, but I will only use the wide search ... But it's still barely AC.
Luo gu "P1135" strange Elevator problem Solving report