1999 - PHÂN HOẠCH HÌNH CHỮ NHẬT (DÀNH CHO HỌC SINH THPT){RECOMM...

Bài 13/1999 - Phân hoạch hình chữ nhật

(Dành cho học sinh THPT)

{Recommend:m,n<5}

const m=4;n=4;max=m*n;

var

a: array[1..m,1..n] of byte;

i1,j1,dem,daxep,tg: integer;

f: text;

time: longint absolute $0:$46C;

save: longint;

{---}

procedure init;

begin

for i1:=1 to m do

for j1:=1 to n do a[i1,j1]:=0;

dem:=0; daxep:=0; tg:=0;

end;

procedure kq;

for i1:=1 to m do

begin

for j1:=1 to n do write(f,a[i1,j1],' ');

writeln(f);

end;

procedure try(i,j: integer);

var i2,j2,flag: integer;

if (daxep=max) then begin kq; writeln(f); tg:=tg+1; end

else

flag:=j;

while (flag

if (a[i,flag]<>0) then flag:=flag-1;

for i2:=i to m do for j2:=j to flag do

begin

dem:=dem+1;

for i1:=i to i2 do for j1:=j to j2 do a[i1,j1]:=dem;

daxep:=daxep+(i2-i+1)*(j2-j+1);

i1:=i;j1:=j2;

while (a[i1,j1]<>0) do

begin

j1:=j1+1;

if j1=n+1 then begin j1:=1; i1:=i1+1; end;

end;

try(i1,j1);

daxep:=daxep-(i2-i+1)*(j2-j+1);

for i1:=i to i2 do

for j1:=j to j2 do a[i1,j1]:=0;

dem:=dem-1;

end;

BEGEN

init;

assign(f,'kq.dat'); rewrite(f);

save:=time;

try(1,1);

write(f,tg);

close(f);

write('Time is about:',(time-save)/18.2);

readln;

END.