Author: eat dog food cat QQ: 283230961 E-mail: gg362710226@sina.com
Everyone must have seen this method, but as a beginner understands that it is particularly difficult. I will tell me at the time, and I will give a suggestion!
PROGRAM EIGHTQUEENS;
VAR
x: array [1..8] of integer;
A, B, C: array [-7..16] of boolean;
i: integer;
PROCEDURE PRINT;
VAR K: Integer;
Begin
Fork: = 1 to 8 do Write (x [k]: 4);
Writeln;
END;
Procedure TRY (i: integer);
VAR J: Integer;
Begin
For j: = 1 to 8 do
IF A [J] and C [i-j]
Then Begin
x: = j;
A [J]: = FALSE
B [i j]: = false;
C [I-J]: = FALSE;
IF i <8 THEN TRY (i 1)
Else Print;
A [J]: = TRUE;
B [i j]: = true;
C [I-J]: = TRUE
end
END;
Begin
For i: = - 7 to 16 do
Begin
A: = true;
B: = true;
C: = true
END;
TRY (1);
Now the cycle starts from i = 1, J: = 1 to 8 do. At this time, the computer detects that i = 1 j = 1 is simplified to (1, 1) is empty, occupying this position and allows the position corresponding to the position and The position of the horizontal direction is false, then the program starts to perform TRY (2), pay attention to this computer only pulls a loop in I = 1 (J = 1), jump to pull I = 2 this layer The computer starts from J: = 1 to 8 DO again, excludes J = 1, j = 2 gets (2, 3) Note that the computer is just pulling 3 (j = 3) cycles in the computer and then jumps to pull I = 3 This layer is subjected to (3, 5), (4, 2) (5, 4) and in the i = 6, the computer does not find a suitable location from J: = 1 to 8 do, at this time In I = 6, the computer computer will return to the i = 5, (because the delivery is delivered) and release (5, 4) this location, why is it to release? Because the reason is simple, if not released, the position of the position corresponding to the position will affect the future several layers, so that the computer is mistaken for False. At this time, J = 5 is ended in this layer, and then the computer will start to find a suitable location from J = 5 to 8. If you can't find it, you will return it to the previous layer and push it to your computer. A set of solution output, assuming that at (8, 3) this location is a set of solutions found, at which time the computer starts cycling from J = 4 to 8, if the computer can't find a computer, it will return the previous layer of i. = 7 This layer is then completed after the last jump position, and the constant return is continuously returned, jumped out, solve, solve the first few positions unchanged, from the 8th position transform, no empty position. Receive return The last floor) finally returned to i = 1 This layer, pay attention to this at this time, just pull J = 1 loop in this layer and then the computer will start with J = 2 to try .... Do you have anyone? How many cycles do you have to spend more than a solution? I want to be estimated to be 1000. In fact, the whole process is a repetitive process (ie, recursive) pouring the J = 1 position in I = 7, ie (7, 1) computational opportunity to try (8, 1) to (8, 8) this 8 A position, and at (7, 2), these 8 locations are also trying to test (7, 1) to (7, 8), etc. at (6, 1) to (7, 8).