AutoCAD 3DMAX C语言 Pro/E UG JAVA编程 PHP编程 Maya动画 Matlab应用 Android
Photoshop Word Excel flash VB编程 VC编程 Coreldraw SolidWorks A Designer Unity3D
 首页 > Delphi

基于Delphi的“八皇后”问题动态实现

51自学网 http://www.wanshiok.com

 

  程序清单:

  (1)代码单元unit1:

procedure TForm1.Button1Click(Sender: TObject);
begin
 dstep:=true;
 bhh:=tbhh.create(false);
 button1.enabled:=false;
 button2.enabled:=true;
 button3.enabled:=true;
end;

procedure TForm1.Button2Click(Sender: TObject);
begin
 if dstep=false then begin bhh.suspend; dstep:=true end
 else bhh.resume
end;

procedure TForm1.Button3Click(Sender: TObject);
begin
 dstep:=false; bhh.resume;
end;

  (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.

 
 

上一篇:Delphi初学者应小心的六大陷阱  下一篇:Delphi.NET多层应用系统开发技术研讨