讨论 / 59行代码
火舞逆天 2011-03-09 01:31:00
点我顶贴 收藏 删除
program eightcode;

const fact:array[0..8] of longint=(1,1,2,6,24,120,720,5040,40320);

var i,j,s,t,temp,m,n,ss:longint; ch:char;

a:array[1..1000000,1..9]of longint;

l,f,step:array[0..1000000]of longint;

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

k:array[1..9]of longint;

function hashtabel(tt:longint):longint;

begin

ss:=0;

for m:=9 downto 1 do

begin

k[m]:=0;

for n:=1 to m-1 do

if a[tt,n]<a[tt,m] then inc(k[m]);

end;

for m:=1 to 9 do

ss:=ss+k[m]*fact[m-1];

hashtabel:=ss;

end;

begin

for i:=1 to 9 do

begin

read(ch);

a[1,i]:=ord(ch)-ord('0');

if a[1,i]=0 then l[1]:=i;

end;

s:=1; t:=1;

for i:=0 to 362879 do

tabel[i]:=false;

tabel[hashtabel(1)]:=true;

repeat

for i:=1 to 9 do

begin

if ((l[s]+1=i)and(l[s]<>3)and(l[s]<>6))or

((l[s]-1=i)and(l[s]<>4)and(l[s]<>7))or(l[s]+3=i)or(l[s]-3=i)

then begin

t:=t+1;

for j:=1 to 9 do a[t,j]:=a[s,j];

temp:=a[s,i];

a[t,i]:=0;

a[t,l[s]]:=temp;

if tabel[hashtabel(t)]=false then

begin

tabel[hashtabel(t)]:=true;

l[t]:=i; f[t]:=s; step[t]:=step[f[t]]+1;

if (a[t,1]=1)and(a[t,2]=2)and(a[t,3]=3)and(a[t,4]=8)and(a[t,5]=0)and(a[t,6]=4)and(a[t,7]=7)and(a[t,8]=6)

then begin

write(step[t]);

halt;

end;

end

else

t:=t-1;

end;

end;

s:=s+1;

until s>t;

end.

#1 wyhbb163@2011-03-09 01:31:00
回复 删除
回复 楼主火舞逆天 的帖子

你写这么长。。。。

const

02.c:array [0..2,1..4] of integer=((3,-3,-1,0),(3,-3,1,0),(-3,3,-1,1));

03.var

04.hash:array[1..900000] of boolean;

05.f:array[1..100000] of string;

06.d:array[1..100000] of longint;

07.s,e,i,p1,p2,h,c1:longint;

08.t:char;

09.p,te:string;

10.begin

11.readln(p);

12.f[1]:='123804765';

13.d[1]:=0;

14.s:=1;

15.e:=1;

16.repeat

17. p1:=pos('0',f[s]);

18. c1:=p1 mod 3;

19. for i:=1 to 4 do begin

20. p2:=p1+c[c1,i];

21. if (p2>0) and (p2<10) then begin

22. te:=f[s];

23. t:=te[p2];

24. te[p2]:=te[p1];

25. te[p1]:=t;

26. val(te,h);

27. h:=h mod 99983;

28. if hash[h]=false then begin

29. hash[h]:=true;

30. e:=e+1;

31. f[e]:=te;

32. d[e]:=d[s]+1;

33. end;

34. if te=p then begin

35. writeln(d[e]);

36. halt;

37. end;

38. end;

39.end;

40. s:=s+1;

41.until s>e;

42.end.

查看更多回复
提交回复