thieumao

1 lời giải - Bài 8 quân hậu

Sep 14th, 2013
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Pascal 1.25 KB | None | 0 0
  1. Program TamQuanHau;
  2. Uses Crt;
  3. Const
  4.   Hau='Q';
  5.   ov='#';
  6. Var
  7.   x: array[1..8] of Integer;
  8.   a: array[1..8] of Boolean;
  9.   b: array[2..16] of Boolean;
  10.   c: array[-7..7] of Boolean;
  11.   i,j: Integer;
  12.   antoan: Boolean;
  13. Begin
  14.   clrscr;
  15.   for i:=1 to 8 do a[i]:=true;
  16.   for i:=2 to 16 do b[i]:=true;
  17.   for i:=-7 to 7 do c[i]:=true;
  18.   j:=1; i:=0;
  19.   repeat
  20.     repeat
  21.       i:=i+1;
  22.       antoan:=a[i] and b[i+j] and c[i-j];
  23.     until antoan or (i=8);
  24.     if antoan then
  25.       begin
  26.         x[j]:=i;
  27.         a[i]:=false; b[i+j]:=false; c[i-j]:=false;
  28.         j:= j+1; i:= 0
  29.       end
  30.     else
  31.       begin
  32.         j:=j-1;
  33.         if j>=1 then
  34.           begin
  35.             i:=x[j];
  36.             a[i]:=true; b[i+j]:=true; c[i-j]:=true;
  37.             if i=8 then
  38.               begin
  39.                 j:=j-1;
  40.                 if j>=1 then
  41.                   begin
  42.                     i:=x[j];
  43.                     a[i]:=true; b[i+j]:=true; c[i-j]:=true
  44.                   end
  45.               end
  46.           end
  47.       end
  48.   until (j>8) or (j<1);
  49.   if j<1 then writeln('Khong co loi giai!')
  50.   else
  51.     for i:=1 to 8 do
  52.       begin
  53.         for j:=1 to 8 do
  54.           if x[j]=i then write(Hau) else write(ov);
  55.         writeln;
  56.       end;
  57.   readln;
  58. end.
Advertisement
Add Comment
Please, Sign In to add comment