主题:【马步遍历全棋盘且不重复问题】求助 救我
const n=4;m=3;
fx:array[1..8,1..2] of -2..2=((1,2),(2,1),(2,-1),(1,-2),(-1,-2),(-2,-1),(-2,1),(-1,2));
var
dep,i:byte;
x,y:byte;
j:integer;
cont:integer;
a:array[1..n,1..m] of byte;
procedure output;
var x,y:byte;
begin
inc(cont);
writeln('count=',cont);
for y:=1 to n do begin
for x:=1 to m do write(a[y,x]:3);writeln;
end;
end;
procedure find(y,x,dep:byte);
var i,xx,yy:integer;
begin
for i:=1 to 8 do
begin
xx:=x+fx[i,1];yy:=y+fx[i,2];
if ((xx in [1..m]) and (yy in [1..n])) and (a[yy,xx]=0) then
begin
a[yy,xx]:=dep;
if (dep=n*m) then output
else find(yy,xx,dep+1);
a[yy,xx]:=0
end
end;
end;
begin
cont:=0;
fillchar(a,sizeof(a),0);
dep:=1;
writeln('input y,x');read(y,x);
if (y>n) or (x>m) then begin writeln('x,y error!');halt;end;
a[y,x]:=1;
find(y,x,2);
if cont=0 then writeln('No answer!') else write('The End!');
readln;
readln;
end.[em10]
此程序算法看上去没有什么问题 可一到执行几步后就会出现错误Runtime error 201 at 0x004011ca 大虾们救救小弟啊。。。。 救救我吧 [em21][em21][em21]
fx:array[1..8,1..2] of -2..2=((1,2),(2,1),(2,-1),(1,-2),(-1,-2),(-2,-1),(-2,1),(-1,2));
var
dep,i:byte;
x,y:byte;
j:integer;
cont:integer;
a:array[1..n,1..m] of byte;
procedure output;
var x,y:byte;
begin
inc(cont);
writeln('count=',cont);
for y:=1 to n do begin
for x:=1 to m do write(a[y,x]:3);writeln;
end;
end;
procedure find(y,x,dep:byte);
var i,xx,yy:integer;
begin
for i:=1 to 8 do
begin
xx:=x+fx[i,1];yy:=y+fx[i,2];
if ((xx in [1..m]) and (yy in [1..n])) and (a[yy,xx]=0) then
begin
a[yy,xx]:=dep;
if (dep=n*m) then output
else find(yy,xx,dep+1);
a[yy,xx]:=0
end
end;
end;
begin
cont:=0;
fillchar(a,sizeof(a),0);
dep:=1;
writeln('input y,x');read(y,x);
if (y>n) or (x>m) then begin writeln('x,y error!');halt;end;
a[y,x]:=1;
find(y,x,2);
if cont=0 then writeln('No answer!') else write('The End!');
readln;
readln;
end.[em10]
此程序算法看上去没有什么问题 可一到执行几步后就会出现错误Runtime error 201 at 0x004011ca 大虾们救救小弟啊。。。。 救救我吧 [em21][em21][em21]