{$A+,B-,D+,E+,F-,G-,I+,L+,N+,O-,P-,Q+,R+,S+,T-,V+,X+}
{$M 16384,0,655360}
program kon_in_nm_matr_one_variant;
const _maxnm=8;
dx:array[1..8]of integer=(-2,-1,1,2,2,1,-1,-2);
dy:array[1..8]of integer=(1,2,2,1,-1,-2,-2,-1);
var a:array[-1.._maxnm+2,-1.._maxnm+2]of integer;
n,m,i,j:integer;
procedure solve(x,y,l:integer);
var w:array[1..8]of integer;
xn,yn,i,j,m1:integer;
begin
a[x,y]:=l;
if l=n*m then
begin
writeln;
for i:=1 to n do
begin
for j:=1 to m do write(a[i,j],' ');
writeln;
end;
halt;
end else
begin
for i:=1 to 8 do
begin
w[i]:=0;
xn:=x+dx[i];
yn:=y+dy[i];
if a[xn,yn]=0 then
begin
for j:=1 to 8 do
if a[xn+dx[j],yn+dy[j]]=0 then inc(w[i]);
end else w[i]:=-1;
end;
i:=1;
while i<=8 do
begin
m1:=1;
for j:=2 to 8 do
if w[j]<w[m1] then m1:=j;
if (w[m1]>=0) and (w[m1]<maxint) then
solve(x+dx[m1],y+dy[m1],l+1);
w[m1]:=maxint;
inc(i);
end;
end;
a[x,y]:=0;
end;
begin
readln(n,m);
for i:=-1 to n+2 do a[i,-1]:=-1;
for i:=-1 to n+2 do a[i,0]:=-1;
for i:=-1 to n+2 do a[i,m+1]:=-1;
for i:=-1 to n+2 do a[i,m+2]:=-1;
for j:=1 to m do a[-1,j]:=-1;
for j:=1 to m do a[0,j]:=-1;
for j:=1 to m do a[n+1,j]:=-1;
for j:=1 to m do a[n+2,j]:=-1;
for i:=1 to n do
for j:=1 to m do
a[i,j]:=0;
for i:=1 to n do
for j:=1 to m do
begin
solve(i,j,1);
end;
end.
все таже про коня((, помогите разобраться |