日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程资源 > 编程问答 >内容正文

编程问答

poj 1077 eight

發(fā)布時(shí)間:2025/3/21 编程问答 21 豆豆
生活随笔 收集整理的這篇文章主要介紹了 poj 1077 eight 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

分析:經(jīng)典的八數(shù)碼問(wèn)題,這里用的是heap+A*。

A*很明顯是用那個(gè)經(jīng)典的A*,heap的維護(hù)和k短路非常像。

第二個(gè)簡(jiǎn)單A*

代碼(模仿某人的):

constnx:array[1..9] of integer=(1,1,1,2,2,2,3,3,3);ny:array[1..9] of integer=(1,2,3,1,2,3,1,2,3);ji:array[0..9] of longint=(1,1,2,6,24,120,720,5040,40320,362880); typearr=array[1..9] of integer; varq:array[0..10000] of integer;f:array[0..10000] of arr;d:array[0..10000] of string;v:array[0..730000] of boolean;p,i,j,k,st,pp:longint;a:arr;ch:char;procedure swap(var x,y:integer); beginx:=x xor y;y:=x xor y;x:=x xor y; end;procedure swaa(var x,y:arr); vark:arr; begink:=x;x:=y;y:=k; end;procedure swas(var x,y:string); vark:string; begink:=x;x:=y;y:=k; end;procedure ok; vark:longint; begink:=0;for i:=2 to 9 doif a[i]<>0 thenfor j:=1 to i-1 doif a[j]>a[i] then inc(k);if odd(k) thenbeginwriteln('unsolvable');halt;end; end;function hash:longint; vari,j,k:longint; beginhash:=0;for i:=1 to 8 doif f[p,i]<>0 thenbegink:=0;for j:=i+1 to 9 doif (f[p,j]<f[p,i])and(f[p,j]<>0) then inc(k);hash:=hash+ji[9-i]*k;end;for i:=1 to 9 doif f[p,i]=0 thenhash:=hash+ji[9-i]*(9-i);hash:=hash+1; end;procedure up; varfa,son:longint; beginson:=p; fa:=p>>1;while fa>1 dobeginif q[son]<q[fa] thenbeginswap(q[son],q[fa]);swas(d[son],d[fa]);swaa(f[son],f[fa]);end else exit;son:=fa; fa:=son>>1;end; end;procedure down; varfa,son:longint; beginfa:=1; son:=2;while son<=p dobeginif (son<p)and(q[son+1]<q[son]) then inc(son);if q[son]<q[fa] thenbeginswap(q[son],q[fa]);swas(d[son],d[fa]);swaa(f[son],f[fa]);end else exit;fa:=son; son:=fa*2;end; end;procedure find(l,r:longint; ch:char); vari:longint; begininc(p);f[p]:=f[1];swap(f[p,l],f[p,r]);i:=hash;if v[i] thenbegindec(p);exit;end else v[i]:=true;d[p]:=d[1]+ch;q[p]:=0;for i:=1 to 9 doif f[p,i]<>0 theninc(q[p],abs(nx[i]-nx[f[p,i]])+abs(ny[i]-ny[f[p,i]]));up; end;beginfor i:=1 to 3 dofor j:=1 to 3 dobeginrepeat read(ch); until ch<>' ';inc(k);if ch='x' then continue;a[k]:=ord(ch)-48;inc(st,abs(i-nx[a[k]]));inc(st,abs(j-ny[a[k]]));end;ok;p:=1;q[1]:=st;f[1]:=a;v[hash]:=true;while p>0 dobeginif q[1]=0 thenbeginwriteln(d[1]);halt;end;for i:=1 to 9 doif f[1,i]=0 thenbegink:=i;break;end;if k-3>0 then find(k,k-3,'u');if k+3<9 then find(k,k+3,'d');if (k mod 3<>1)and(k>1) then find(k,k-1,'l');if (k mod 3<>0)and(k<9) then find(k,k+1,'r');q[1]:=q[p];d[1]:=d[p];f[1]:=f[p];q[p]:=0;d[p]:='';dec(p);down;end; end.

  

轉(zhuǎn)載于:https://www.cnblogs.com/reflec94/archive/2011/10/20/2218440.html

總結(jié)

以上是生活随笔為你收集整理的poj 1077 eight的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。