level 8
年华似火燃烧
楼主
广搜,但是不知道为什么会不出结果
type aa=array[0..8]of integer;
const dx:array[1..4]of integer=(0,0,1,-1);
dy:array[1..4]of integer=(1,-1,0,0);
var a:array[1..10000]of aa;
goal:aa;
hash:array[1..1000]of integer;
t,w:integer;
function panduan(s,ss:aa):boolean;
var i:integer;
begin
panduan:=true;
for i:=0to 8 do
if s[i]<>ss[i] then begin panduan:=false;exit;end;
end;
function h(s:aa):integer;
var i:integer;
begin
h:=0;
for i:=0to 8 do
h:=h+i*s[i];
end;
procedure init;
var i:integer;
begin
for i:=0to 8 do
read(a[1][i]);
t:=1;w:=1;
for i:=0to 8 do
read(goal[i]);
fillchar(hash,sizeof(hash),0);
hash[h(a[1])]:=1;
end;
procedure bfs;
var i,x,y,z,newx,newy,newz:integer;
st,stt:aa;
begin
while t<=w do
begin
st:=a[t];
if panduan(st,goal) then begin writeln(t); exit;end;
for z:=0 to 8 do
if st[z]=0 then break;
x:=z div 3;y:=z mod 3;
for i:=1 to 4 do
begin
newx:=x+dx[i];newy:=y+dy[i];
newz:=newx*3+newy;
if (newx>=0)and(newx<3)and(newy>=0)and(newy<3) then
begin
stt:=st;
stt[z]:=st[newz];
stt[newz]:=st[z];
if hash[h(stt)]=0 then begin inc(w);a[w]:=stt;hash[h(stt)]:=1;end;
end;
end;
inc(t);
end;
end;
begin
assign(input,'game.in');
assign(output,'game.out');
reset(input);
rewrite(output);
init;
bfs;
close(input);
close(output);
end.
输出时姑且输出的是t,没有考虑多少层(步),但这样竟然也没有输出。
求大神,给解答
这个题给搁了好几天,大神大神来解答 OTL
2014年07月14日 09点07分
1
type aa=array[0..8]of integer;
const dx:array[1..4]of integer=(0,0,1,-1);
dy:array[1..4]of integer=(1,-1,0,0);
var a:array[1..10000]of aa;
goal:aa;
hash:array[1..1000]of integer;
t,w:integer;
function panduan(s,ss:aa):boolean;
var i:integer;
begin
panduan:=true;
for i:=0to 8 do
if s[i]<>ss[i] then begin panduan:=false;exit;end;
end;
function h(s:aa):integer;
var i:integer;
begin
h:=0;
for i:=0to 8 do
h:=h+i*s[i];
end;
procedure init;
var i:integer;
begin
for i:=0to 8 do
read(a[1][i]);
t:=1;w:=1;
for i:=0to 8 do
read(goal[i]);
fillchar(hash,sizeof(hash),0);
hash[h(a[1])]:=1;
end;
procedure bfs;
var i,x,y,z,newx,newy,newz:integer;
st,stt:aa;
begin
while t<=w do
begin
st:=a[t];
if panduan(st,goal) then begin writeln(t); exit;end;
for z:=0 to 8 do
if st[z]=0 then break;
x:=z div 3;y:=z mod 3;
for i:=1 to 4 do
begin
newx:=x+dx[i];newy:=y+dy[i];
newz:=newx*3+newy;
if (newx>=0)and(newx<3)and(newy>=0)and(newy<3) then
begin
stt:=st;
stt[z]:=st[newz];
stt[newz]:=st[z];
if hash[h(stt)]=0 then begin inc(w);a[w]:=stt;hash[h(stt)]:=1;end;
end;
end;
inc(t);
end;
end;
begin
assign(input,'game.in');
assign(output,'game.out');
reset(input);
rewrite(output);
init;
bfs;
close(input);
close(output);
end.
输出时姑且输出的是t,没有考虑多少层(步),但这样竟然也没有输出。
求大神,给解答
这个题给搁了好几天,大神大神来解答 OTL