Код:
01 program Queens;
02 uses
03 CRT;
04
05 const
06 M=8; {Board Size, 8 }
07 MaxL=5; {Number of Queens, 5 }
08
09 type
10 tCell=byte;
11 tBo=array[1..M,1..M]of tCell;
12 tCo=record
13 x,y:integer
14 end;
15
16 var
17 Bo:tBo;
18 BoCo:array[1..MaxL]of tBo;
19 Co:array[1..MaxL]of tCo;
20 L:integer;
21 i,j:integer;
22
23 procedure Show;
24 var
25 i,j,k,f:integer;
26 begin
27 for j:=M downto 1 do begin
28 Write(j:2,' ');
29 for i:=1 to M do begin
30 f:=0;
31 for k:=1 to L do if (Co[k].x=i)and(Co[k].y=j) then f:=k;
32 if f=0 then Write(' ') else Write(f);
33 end;
34 Write(' ');
35 for i:=1 to M do if Bo[i,j]=0 then Write(' ') else Write(Bo[i,j]);
36 WriteLn;
37 end;
38 Write(' ');
39 for i:=1 to M do Write(Char(i+96));
40 end;
41
42
43 procedure Put(x,y:integer);
44 var
45 i,j:integer;
46 t:boolean;
47 begin
48 Inc(L);
49 BoCo[L]:=Bo;
50 Co[L].x:=x;
51 Co[L].y:=y;
52 for i:=1 to M do begin
53 Bo[i,y]:=L;
54 Bo[x,i]:=L;
55 end;
56 for i:=-M to M do begin
57 if ((x+i)>0)and((y+i)>0)and((x+i)<=M)and((y+i)<=M) then Bo[x+i,y+i]:=L;
58 if ((x+i)>0)and((y-i)>0)and((x+i)<=M)and((y-i)<=M) then Bo[x+i,y-i]:=L;
59 end;
60
61 if L=MaxL then begin
62 t:=true;
63 for i:=1 to M do for j:=1 to M do t:=t and(Bo[i,j]<>0);
64 if t then begin
65 WriteLn;
66 Write('Found: ');
67 for i:=1 to L do Write(Char(96+Co[i].x),Co[i].y,' ');
68 WriteLn;
69 WriteLn;
70 Show;
71 ReadLn;
72 end;
73 end
74 else for i:=1 to M do for j:=1 to M do Put(i,j);
75 Bo:=BoCo[L];
76 Dec(L);
77 end;
78
79 begin
80 for i:=1 to M do for j:=1 to M do Bo[i,j]:=0;
81 L:=0;
82 Put(1,1);
83 end.
Ферзи на доске [Java] |