主题:回溯问题(帮我改改)
把1-8不重复添入下图(附件里,图片001为合适填法,002为不合适填法,毒死全家).求所有填法.
我编了一个..但......帮我改改吧....
const xx:array[1..12] of -3..3=(2,-2,2,-2,2,-2,3,-3,0,0,1,-1);
yy:array[1..12] of -3..3=(0,0,1,-1,-1,1,0,0,-2,2,-2,2);
var tt,t:array[1..4] of 1..4;
zb:array[1..4,1..3] of 0..1;
a,b,i,j,k,x,y:integer;
f:boolean;
begin
fillchar(tt,sizeof(tt),0);
fillchar(t,sizeof(t),0);
fillchar(zb,sizeof(zb),0);
tt[1]:=2;t[1]:=2;{我先把程序弄成起点2,2,但要求所有的解法}
zb[2,2]:=1;{1:填过了,0:没填}
i:=1;k:=2;
repeat
x:=tt[k-1]+xx[i];y:=t[k-1]+yy[i];
if (x>4) or (x<1) or (y<1) or (y>3) or (((x<2) or (x>3)) and (y<>2))then begin{出界}
inc(i);
if i=13 then begin{所有走法都试过了}
dec(k);i:=1;{退一步}
end;
end else{没出界}
if zb[x,y]=0 then begin{没走过}
tt[k]:=x;t[k]:=y;{横纵坐标记录}
zb[x,y]:=1;
inc(k);
i:=1;
end else begin
inc(i);{如果走过就换走法}
end;
until k=1;
for j:=1 to 8 do write(tt[j],t[j],' ');
readln
end.
帮帮我。...
我编了一个..但......帮我改改吧....
const xx:array[1..12] of -3..3=(2,-2,2,-2,2,-2,3,-3,0,0,1,-1);
yy:array[1..12] of -3..3=(0,0,1,-1,-1,1,0,0,-2,2,-2,2);
var tt,t:array[1..4] of 1..4;
zb:array[1..4,1..3] of 0..1;
a,b,i,j,k,x,y:integer;
f:boolean;
begin
fillchar(tt,sizeof(tt),0);
fillchar(t,sizeof(t),0);
fillchar(zb,sizeof(zb),0);
tt[1]:=2;t[1]:=2;{我先把程序弄成起点2,2,但要求所有的解法}
zb[2,2]:=1;{1:填过了,0:没填}
i:=1;k:=2;
repeat
x:=tt[k-1]+xx[i];y:=t[k-1]+yy[i];
if (x>4) or (x<1) or (y<1) or (y>3) or (((x<2) or (x>3)) and (y<>2))then begin{出界}
inc(i);
if i=13 then begin{所有走法都试过了}
dec(k);i:=1;{退一步}
end;
end else{没出界}
if zb[x,y]=0 then begin{没走过}
tt[k]:=x;t[k]:=y;{横纵坐标记录}
zb[x,y]:=1;
inc(k);
i:=1;
end else begin
inc(i);{如果走过就换走法}
end;
until k=1;
for j:=1 to 8 do write(tt[j],t[j],' ');
readln
end.
帮帮我。...