求八数码问题的dfs解和bfs解

来源:百度知道 编辑:UC知道 时间:2024/05/17 05:43:04
输出dfs后的解和bfs后的解,用pascal写的

program num8;
type a33=array[1..3,1..3] of 0..8;
a4=array[1..4] of -1..1;
node=record
ch:a33;
si,sj:1..3;
pnt,dep:byte;
end;
const goal:a33=((1,2,3),(8,0,4),(7,6,5));
start:a33=((2,8,3),(1,6,4),(7,0,5));
di:a4=(0,-1,0,1);
dj:a4=(-1,0,1,0);
var data:array[1..100] of node;
temp:node;
r,k,ni,nj,head,tail,depth:integer;
function check(k:integer):boolean;
begin
ni:=temp.si+di[k];nj:=temp.sj+dj[k];
if (ni in [1..3]) and (nj in [1..3]) then check:=true else check:=false;
end;
function dupe:boolean;
var i,j,k:integer;
buf:boolean;
begin
buf:=false;i:=0;
repeat
inc(i);buf:=true;
for j:=1 to 3 do
for k:=1 to 3 do
if data[i].ch[j,k]<>data[tail].ch[j,k] then buf:=false;
until buf or (i>=tail-1);
dupe:=buf;
end;
funct