104
end;
procedure solve;
var
clen, ci, cj, l, ni, nj : integer;
news, curs : byte;
a : tboard;
begin
fillchar(a, sizeof(a), 0);
fillchar(u, sizeof(u), true);
ql := 0;
qr := 0;
ci := si;
cj := sj;
clen := 0;
for l := 1 to n do setp(a, l, p[l].i, p[l].j);
repeat
curs := getstate(u);
if curs = 0 then begin
issol := true;
result := clen;
continue;
end;
for l := 1 to 8 do begin
ni := ci + dd[l, 1];
nj := cj + dd[l, 2];
if not((ni >= 1)and(ni <= 8)and(nj >= 1)and(nj <= 8)) then continue;
case a[ni, nj] of
0 : begin
if(d[ni, nj, curs] = -1)or(d[ni, nj, curs] > succ(clen)) then begin
d[ni, nj, curs] := succ(clen);
addq(ni, nj, curs, succ(clen));
end;
end;
1..8 : begin
u[a[ni, nj]] := false;
news := getstate(u);
if(d[ni, nj, news] = -1)or(d[ni, nj, news] > succ(clen)) then begin
d[ni, nj, news] := succ(clen);
addq(ni, nj, news, succ(clen));
end;
u[a[ni, nj]] := true;
end;
end;
end;
inc(ql);
extract(ci, cj, clen, a, u);
until (ql > qr) or (issol);
end;