讨论 / 帮忙看看为何不对啊
woshimishuo 2012-01-22 03:23:00
点我顶贴 收藏 删除
var q:array[0..1000000,1..3] of longint;st:string;

v:array[0..362880] of boolean;

i,j,h,t,k,pos,temp,num,n,goal,blank,tt,now,pp:longint;

fac:array[0..9] of longint=(1,1,2,6,24,120,720,5040,40320,362880);

a:array[0..9] of longint;

d:array[1..4] of integer=(3,-3,-1,1);

function cantor:longint;

var i,j:longint;

begin

num:=0;

for i:=1 to 9-1 do

begin

temp:=0;

for j:=i+1 to 9 do

if a[j]<a[i] then inc(temp);

num:=num+temp*fac[9-i];

end;

exit(num+1);

end;

begin

readln(st);

for i:=1 to length(st) do

begin

a[i]:=ord(st[i])-48;

if a[i]=0 then blank:=i;

end;

goal:=46686;

h:=0;t:=1;q[1,1]:=cantor;q[1,2]:=blank;v[q[1,1]]:=true;

repeat

inc(h);

for i:=1 to 4 do

begin

pp:=q[h,2]+d[i];

if (pp>9) or (pp<1) then continue;

if ((pp mod 3=0) and (q[h,2] mod 3=1)) or ((pp mod 3=1) and (q[h,2] mod 3=0))

then continue;

tt:=a[pp];a[pp]:=a[q[h,2]];a[q[h,2]]:=tt;

now:=cantor;if now=goal then begin writeln(q[h,3]+1);halt;end;

if not v[now] then

begin

inc(t);

q[t,1]:=now;q[t,2]:=pp;v[now]:=true;q[t,3]:=q[h,3]+1;

end;

end;

until h>=t;

end.

查看更多回复
提交回复