Bài pascal tám quân hậu trên bàn cờ

1 3.2K 32
Bài pascal tám quân hậu trên bàn cờ

Đang tải... (xem toàn văn)

Thông tin tài liệu

Viết chương trình để tìm các phương án đặt 8 quân hậu trên bàn cờ vua (ma trận 8 x 8) sao cho các quân hậu không ăn được nhau. Có bao nhiêu cách đặt. const max = 8; Var n,k:integer; x: array1..max of integer; a: array1..max of boolean; b: array2..2max of boolean; c: array1max..max1 of boolean; Procedure Print; BEGIN For k:=1 to max do Write((,k,;,xk,); ); writeln; End; Procedure Quaylui(i:integer); Var j:integer; BEGIN For j:=1 to max do if aj and bi+j and cij then BEGIN xi:=j; If i = Max Then BEGIN Print ; n:=n+1; End Else BEGIN aj:=false; bi+j:=false; cij:=false; Quaylui(i+1); aj:=true; bi+j:=true; cij:=true; end; end; End; .................

const max = 5; Var n,k:integer; x: array[1 max] of integer; a: array[1 max] of boolean; b: array[2 2*max] of boolean; c: array[1-max max-1] of boolean; Procedure Print; Begin For k:=1 to max Write('(',k,';',x[k],'); '); writeln; End; Procedure Quaylui(i:integer); var j:integer; Begin For j:=1 to max if a[j] and b[i+j] and c[i-j] then begin x[i]:=j; if i=max then begin print ; n:=n+1; end else begin a[j]:=false; b[i+j]:=false; c[i-j]:=false; Quaylui(i+1); a[j]:=true; b[i+j]:=true; c[i-j]:=true; end; end; End; BEGIN Fillchar(a,sizeof(a),true); Fillchar(b,sizeof(b),true); Fillchar(c,sizeof(c),true); Quaylui(1); Writeln(n); Readln; END

Ngày đăng: 22/03/2016, 08:06

Tài liệu cùng người dùng

Tài liệu liên quan