看板 Mathematica 關於我們 聯絡資訊
(*將圖形檔轉成適當尺寸,{313,313}為迷宮大小*) mazepic= Import["http://www.unikatissima.de/uploads/unikatissima_maze_7qB.png"]; test=Image[Binarize[ImageResize[mazepic,{3130,3130}]]]; (*將圖形檔轉成迷宮資料陣列*) temp=Map[Abs[#-1]&,ImageData[test],{1}]; maze=Map[If[Mean[Flatten[#[[4;;7,4;;7]]]]>0.47,1,0]&, Partition[temp,{10,10}],{2}]; ArrayPlot[maze,FrameTicks->Automatic] (*迷宮維度*) wh=Dimensions@maze; (*建立迷宮通道資料*) n1=Table[ If[maze[[i,j]]+maze[[i,j+1]]==0, UndirectedEdge[wh[[1]]*(i-1)+j,wh[[1]]*(i-1)+j+1]], {i,1,wh[[1]]},{j,1,wh[[2]]-1}]; n2=Table[ If[maze[[i,j]]+maze[[i+1,j]]==0, UndirectedEdge[wh[[1]]*(i-1)+j,wh[[1]]*i+j]], {i,1,wh[[1]]-1},{j,1,wh[[2]]}]; medge=Select[Flatten[{n1,n2}],Length@#>=2&]; (*產生迷宮圖*) mg=Graph[Range[wh[[1]]*wh[[2]]], medge,{FrameTicks->True, VertexCoordinates-> Flatten[Table[{i,j},{i,1,wh[[1]]},{j,1,wh[[2]]}],1]}]; (*計算走出迷宮的最短路徑,315,313*312-1分別為左下角及右上角白色端點*) mpath=FindShortestPath[mg,315,313*312-1]; HighlightGraph[mg,PathGraph@FindShortestPath[mg,315,313*312-1]] -- 養花種魚數月亮賞星星 http://chungyuandye.twbbs.org -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 218.173.131.69
LPH66:推一個 07/07 21:11
qoodeva:大師的文一定要推一下 07/09 21:35
jurian0101:最近被graph搞得頭大中...能舉重若輕玩圖形~~膜拜之 07/10 22:55