(2)代碼單元unit2:
uses unit1;
procedure Tbhh.Execute;
begin
hsu(1);
form1.button1.enabled:=true;
form1.button2.enabled:=false;
form1.button3.enabled:=false;
end;
procedure tbhh.prt;//顯示
var i,j,ix,iy:integer;
s:real;IIS:string[2];
begin
str(tt:2,IIS);
form1.label1.caption:='第'+IIS+'幅';
form1.paintbox1.canvas.draw(0,0,q);
for i:=1 to 8 do
for j:=1 to 8 do
if a[i,j]=1 then
begin
ix:=(i-1)*50+1;
iy:=(j-1)*50+1;
form1.paintbox1.canvas.draw(iy,ix,c);
end;
if dstep=true then suspend
else begin s:=10; for i:=1 to 100000 do s:=s*s/s end;
end;
procedure tbhh.hsu(i:integer);//回溯求解
var j:integer;
begin
if i>8 then begin tt:=tt+1; synchronize(prt)end
else for j:=1 to 8 do
begin a[i,j]:=1;if pd(i,j) then hsu(i+1);a[i,j]:=0;end
end;
constructor tbhh.create(flag:boolean);//創建該線程的一實例並對有關的變量進行初始化
var i,j:integer;
begin
inherited create(flag);
q:=tbitmap.create;q.loadfromfile('e:\八皇後\backing.bmp');
c:=tbitmap.create;c.loadfromfile('e:\八皇後\queen.bmp');
for i:=1 to 8 do
for j:=1 to 8 do
a[i,j]:=0; tt:=0;
end;
end.