Pascal 与预测不同的奇怪结果(骑士之旅)
我正在对骑士之旅问题进行编码,以便在n*n棋盘中为骑士找到一个旅程。我做了两个答案,我认为是相同的。但是,在编译时,两个代码会产生两个不同的结果。我想知道我的两个密码之间的区别 这是我的第一个代码:Pascal 与预测不同的奇怪结果(骑士之旅),pascal,Pascal,我正在对骑士之旅问题进行编码,以便在n*n棋盘中为骑士找到一个旅程。我做了两个答案,我认为是相同的。但是,在编译时,两个代码会产生两个不同的结果。我想知道我的两个密码之间的区别 这是我的第一个代码: `const max=10; 类型square=longint的数组[-1..max+1,-1..max+1]; 向量=longint的数组[1..max*max]; 变量n:长型; x:longint=(-2,-2,-1,-1,+1,+1,+2,+2)的数组[1..8]; y:longint=(-
`const max=10;
类型square=longint的数组[-1..max+1,-1..max+1];
向量=longint的数组[1..max*max];
变量n:长型;
x:longint=(-2,-2,-1,-1,+1,+1,+2,+2)的数组[1..8];
y:longint=(-1,+1,-2,+2,-2,+2,-1,+1)的数组[1..8];
c、 r:正方形;
a、 b:向量;
检查:布尔;
程序输入;
开始
readln(n);
结束;
程序回溯(i、u、v:纵向);
变量j:长型;
开始
如果(i>n*n)那么
开始
检查:=正确;
出口
结束;
对于j:=1到8 do
开始
如果检查,则退出;
公司(u,x[j]);
inc(v,y[j]);
如果(u>0)和(u0)和(v0)和(u0)和(vr[u,v]:=i;
出现在j:=1到8的之前,则在第二个代码中,而不是第一个代码中
有几种方法可以用来区分两个文本文件的不同之处。熟悉这些工具是一个好主意。我认为它的位置对我的代码的结果没有任何影响。我想知道,如果我把它放在上面代码中的两个不同位置,是否会有区别。它怎么会没有区别,因为它位于第一个代码位于if
语句的主体中(因此可能并不总是执行),但它在第二个代码中的位置是无条件的?
`const max=10;
type square=array [-1..max+1,-1..max+1] of longint;
vector=array [1..max*max] of longint;
var n:longint;
x:array [1..8] of longint=(-2,-2,-1,-1,+1,+1,+2,+2);
y:array [1..8] of longint=(-1,+1,-2,+2,-2,+2,-1,+1);
c,r:square;
a,b:vector;
checking:boolean;
procedure input;
begin
readln(n);
end;
procedure backTrack(i,u,v:longint);
var j:longint;
begin
if (i>n*n) then
begin
checking:=true;
exit;
end;
for j:=1 to 8 do
begin
if checking then exit;
inc(u,x[j]);
inc(v,y[j]);
if (u>0) and (u<=n) and (v>0) and (v<=n) and (i<=n*n) and (c[u,v]=0) then
begin
c[u,v]:=1;
r[u,v]:=i;
backTrack(i+1,u,v);
c[u,v]:=0;
end;
dec(u,x[j]);
dec(v,y[j]);
end;
end;
procedure solve;
begin
fillchar(c,sizeof(c),0);
c[1,1]:=1;
r[1,1]:=1;
checking:=false;
backTrack(2,1,1);
end;
procedure output;
var j,i:longint;
begin
for j:=1 to n do
begin
for i:=1 to n do write(r[i,j],' ');
writeln;
end;
readln;
end;
begin
input;
solve;
output;
end.`
`const max=10;
type square=array [-1..max+1,-1..max+1] of longint;
vector=array [1..max*max] of longint;
var n:longint;
x:array [1..8] of longint=(-2,-2,-1,-1,+1,+1,+2,+2);
y:array [1..8] of longint=(-1,+1,-2,+2,-2,+2,-1,+1);
c,r:square;
a,b:vector;
checking:boolean;
procedure input;
begin
readln(n);
end;
procedure backTrack(i,u,v:longint);
var j:longint;
begin
if (i>n*n) then
begin
checking:=true;
exit;
end;
r[u,v]:=i;
for j:=1 to 8 do
begin
if checking then exit;
inc(u,x[j]);
inc(v,y[j]);
if (u>0) and (u<=n) and (v>0) and (v<=n) and (i<=n*n) and (c[u,v]=0) then
begin
c[u,v]:=1;
backTrack(i+1,u,v);
c[u,v]:=0;
end;
dec(u,x[j]);
dec(v,y[j]);
end;
end;
procedure solve;
begin
fillchar(c,sizeof(c),0);
c[1,1]:=1;
r[1,1]:=1;
checking:=false;
backTrack(1,1,1);
end;
procedure output;
var j,i:longint;
begin
for j:=1 to n do
begin
for i:=1 to n do write(r[i,j],' ');
writeln;
end;
end;
begin
input;
solve;
output;
end.`