ArticleDirectory
- String Conversion
- Poj1915
In fact, it is BFs. It is found when the initial status and target status are searched at the same time. However, implementation is quite troublesome. I imitatedProgramThe substring transformation of noip2002 is written, which adopts the first extension with fewer nodes. Then I wrote poj1915 myself, but the debugging failed. As a result, He imitated a program to write programs that alternate expansion nodes, and found thatCodeIt can be simplified a lot.
For programs with hundreds of lines, you must pay attention to the quality of the Code. Otherwise, understanding and debugging will be troublesome.
The following are two programs.
String Conversion
{A simple search question is written into a BFS with four points passed over. It is estimated that several substrings in the string can be transformed. In addition, the running efficiency is not high. You may need to write a hash statement. Then I wrote a two-way wide search, which is the first two-way wide search program. Two-way wide search generally has a long code, but this question is relatively simple and I can write this program, so there are less than a hundred lines. In fact, it is not hard to understand, that is, you must master the details when implementing it. In addition, the program I imitated copies strings several more digits, but it can be AC. It seems that invalid characters are automatically ignored when Pascal compares or copies strings. } Type node = record s: String ; Depth: longint end; queue = record data: array [1 .. 8000] of node; Head, tail: longint; end; var A: array [0 .. 10, 0 .. 1]String ; Q: array [0 .. 1] of queue; I, j, N: longint; s, temp: String ; Function check (s: String ; Sign: longint): longint; var I: longint; begin For I: = 1 to Q [sign]. Tail Do If Q [sign]. Data [I]. S = s then exit (-1 ); For I: = 1 to Q [1-sign]. Tail Do If Q [1-sign]. data [I]. S = s then exit (I); exit (0); end; Procedure expand (S: longint); var I, j, Len, lenx, POs, Judge: longint; x: String ; Begin Inc (Q [s]. Head); X: = Q [s]. Data [Q [s]. Head]. s; Len: = length (X ); For I: = 1 to n Do Begin lenx: = length (A [I, S]); For J: = 1 to Len-lenx + 1 Do If Copy (x, J, lenx) = A [I, S] Then begin temp: = copy (x, 1, J-1) + A [I, 1-s] + copy (X, J + lenx, Len); Judge: = check (temp, S ); If (Judge <> 0) and (Judge <>-1) then begin writeln (Q [s]. data [Q [s]. head]. depth + 1 + Q [1-s]. data [judge]. depth); halt; End Else If Judge = 0 then begin Inc (Q [s]. tail); Q [s]. data [Q [s]. tail]. s: = temp; Q [s]. data [Q [s]. tail]. depth: = Q [s]. data [Q [s]. head]. depth + 1; end; begin While (Not EOF) and (S <> 'I love sry' ) Do Begin readln (s); A [n, 0]: = copy (s, 1, pos ( '' , S)-1); A [n, 1]: = copy (S, pos ( '' , S) + 1, length (s); Inc (n); end; Dec (N ); If A [0, 0] = A [0, 1] Then begin writeln (0); halt; end; For I: = 0 to 1 Do Begin Q [I]. head: = 0; Q [I]. tail: = 1; Q [I]. data [1]. s: = A [0, I]; end; While True Do Begin If (Q [0]. Tail = 0) and (Q [1]. Tail = 0) thenBreak ; If (Q [0]. Tail <q [1]. Tail) then expand (0) Else Expand (1 ); If Q [0]. Data [Q [0]. Tail]. Depth + Q [1]. Data [Q [1]. Tail]. Depth> 10 then Break ; End; writeln ( 'No answer! ' ); End.
Poj1915
{For the horse jumping problem, the minimum number of steps from one point to another is bidirectional BFs, and nodes are extended alternately. } Const DX: array [1 .. 8] of longint = (,-1,-, 2,-2,-2); DY: array [1 .. 8] of longint = (2,-,-1); var N: longint; Q: array [0 .. 1000000] of record X, Y: longint; end; flag, step: array [0 .. 400, 0 .. 400] of longint; t, I, J, K: longint; Head, tail: longint; Function check (X, Y: longint): Boolean; begin If (X> = 0) and (y> = 0) and (x <n) and (Y <n) Then exit ( True ) Else Exit ( False ); End; Procedure main; var Sx, Sy, TX, Ty, X, Y: longint; XX, YY: longint; I, J, K: longint; begin readln (n, Sx, Sy, TX, Ty ); If (SX = Tx) and (Tx = ty) then begin writeln (0); exit; end; fillchar (flag, Sizeof (FLAG), 0); fillchar (step, Sizeof (STEP), 0); flag [SX, sy]: = 1; flag [Tx, Ty]: = 2; Q [1]. x: = SX; Q [1]. y: = sy; Q [2]. x: = TX; Q [2]. y: = ty; head: = 0; tail: = 2; While Head <tail Do Begin Inc (head); X: = Q [head]. X; Y: = Q [head]. Y; For I: = 1 to 8 Do Begin XX: = x + dx [I]; YY: = Y + dy [I]; If Not check (XX, YY) then Continue ; If Flag [XX, YY] = 0 then begin Inc (tail); Q [tail]. x: = xx; Q [tail]. y: = YY; flag [XX, YY]: = Flag [x, y]; step [XX, YY]: = step [x, y] + 1; End Else If Flag [XX, YY] <> flag [x, y] Then begin writeln (step [XX, YY] + step [x, y] + 1); exit; end; end; end; begin readln (t ); For I: = 1 to T Do Main; end.