找回密码
 注册
Simdroid-非首页
查看: 207|回复: 7

[原创短文] 宽度优先搜索解迷宫

[复制链接]
发表于 2011-1-1 22:19:25 | 显示全部楼层 |阅读模式 来自 北京
花半个小时帮朋友解一个迷宫问题,整理出来做个备案吧。
题目描述:

Mazes can keep one captured for hours if you actually walk in one. Still there are many approaches to logically find the exit. Some may take more time, and might in practice not be possible esspecially if there would be other perons walking in the same maze messing with your trail! We can however program a few algorithms on the computer to see how well they are doing. A small list of possible algorithms is:



       - Random mouse algorithm        - Wall follower        - Pledge algorithm        - Tremaux's algorithm        - Dead-end filling        - Shortest path algorithm


If you cannot imagine what they are look them up at (http://en.wikipedia.org/wiki/Maze_solving_algorithm, if you can't acces this let us now). An example maze with solution is given by:






算法很简单,宽搜向外扩展可行区域,逆向返回即可。

  1. maze = ImageData[\!\(\*
  2. GraphicsBox[
  3. TagBox[RasterBox[CompressedData["
  4. 1:eJylUskNgDAMi8QCPFiAlRihC7D/i0NFkbHTlCOCqkprO3E6l3Upg5lN13/u
  5. /8Q4b/Qdya0ReB9J6qljVcWxvnoSj3LdHKty2CNhidbZahdqiErUpPOgUKs8
  6. UtERIEqxNALceA25LeQG1tOtua7h9J9glUovhP1i40mm1S+9BNJVbFe35Weo
  7. gpCQmXTD+vUo9NnuoyTbE+yrMHh1n2MHv03QpQ==
  8. "], {{0, 20}, {20, 0}}, {0,
  9.          255},
  10. ColorFunction->RGBColor],
  11. BoxForm`ImageTag["Byte", ColorSpace -> "RGB", Interleaving -> True],
  12. Selectable->False],
  13. BaseStyle->"ImageGraphics",
  14. ImageSize->{246., Automatic},
  15. ImageSizeRaw->{20, 20},
  16. PlotRange->{{0, 20}, {0, 20}}]\)][[;; , ;; , 3]];
复制代码



  1. shortestPath[maze_, start_, end_] := Module[
  2.   {curIndex = 1, cur = start, queue = {start},
  3.    dir = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}}, i},
  4.   While[! MemberQ[queue, end],
  5.    For[i = 1, i < 5, i++,
  6.     If[! MemberQ[queue, cur + dir[[i]]] &&
  7.        Extract[maze, cur + dir[[i]]] == 1,
  8.       AppendTo[queue, cur + dir[[i]]]
  9.       ];
  10.     ];
  11.    curIndex++;
  12.    cur = queue[[curIndex]];
  13.    ];
  14.   NestWhileList[
  15.    First@Select[queue, Function[u, Total[Abs[u - #]] == 1]] &,
  16.    end, # != start &]
  17.   ]
复制代码


  1. path = shortestPath[maze, {2, 1}, {19, 20}];
  2. Image@MapAt[0.5 &, maze, path]
复制代码

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?注册

×

评分

1

查看全部评分

发表于 2011-1-2 13:47:57 | 显示全部楼层 来自 上海
Simdroid开发平台
http://blog.wolfram.com/2010/12/ ... m-palace-continues/

Wolfram Blog 前一阵子,为了算Maze 还打起来了。

你认为他们的方法怎么样,有何评论吗?
回复 不支持

使用道具 举报

 楼主| 发表于 2011-1-2 15:15:39 | 显示全部楼层 来自 北京
2# FreddyMusic
我在google reader上看了这篇文章,当时看得时候是把它当作一篇关于图像处理的文章进行分类的。
它在进行了第一步处理(DeleteSmallComponents或者Pruning)之后,实际上已经得到了一个连通图,而且图结构高度简化,没有很多无关分支,所以通过求图的最短路就求出了迷宫答案。寻找最短路有很简单的算法,Dijstra,Floyd 都可以。

但是根据我的做题经验(ACM),迷宫问题一般是和图论没有什么联系的,迷宫问题考察的是搜索技术。所以我觉得这篇文章并没有探讨迷宫理论问题,而是侧重于如何把一个实际图片建模成一个图结构。

解迷宫我一般使用dfs,速度相对较快,但是会很难得到最短路径,而且不够稳定,最糟糕的情况会把整个地图遍历一遍。使用BFS就稳定一些,只是空间和时间复杂度都较高。
回复 不支持

使用道具 举报

发表于 2011-1-2 19:58:39 | 显示全部楼层 来自 上海
想法不错,分析也有一定的道理。

我建议你可以将自己的迷宫算法和以下的一个演示项目相结合,自己动手写个新的演示项目。

http://demonstrations.wolfram.com/InteractiveMaze/

他的程序主要是生成一个随机的迷宫题,而你的算法正好可以解题。
回复 不支持

使用道具 举报

发表于 2011-1-2 20:58:53 | 显示全部楼层 来自 广东广州
谢谢~~,好东西~~,就是看不懂。哈哈
回复 不支持

使用道具 举报

发表于 2011-1-3 21:31:39 | 显示全部楼层 来自 台湾
本帖最后由 chungyuandye 于 2011-1-4 08:26 编辑

  1. maze=ImageData[\!\(\*
  2. GraphicsBox[
  3. TagBox[RasterBox[CompressedData["
  4. 1:eJylUskNgDAMi8QCPFiAlRihC7D/i0NFkbHTlCOCqkprO3E6l3Upg5lN13/u
  5. /8Q4b/Qdya0ReB9J6qljVcWxvnoSj3LdHKty2CNhidbZahdqiErUpPOgUKs8
  6. UtERIEqxNALceA25LeQG1tOtua7h9J9glUovhP1i40mm1S+9BNJVbFe35Weo
  7. gpCQmXTD+vUo9NnuoyTbE+yrMHh1n2MHv03QpQ==
  8. "],{{0,20},{20,0}},{0,
  9. 255},
  10. ColorFunction->RGBColor],
  11. BoxForm`ImageTag["Byte",ColorSpace->"RGB",Interleaving->True],
  12. Selectable->False],
  13. BaseStyle->"ImageGraphics",
  14. ImageSize->{246.,Automatic},
  15. ImageSizeRaw->{20,20},
  16. PlotRange->{{0,20},{0,20}}]\)][[;;,;;,3]][[2;;-2,2;;-2]]

  17. maze//Dimensions

  18. maze//ArrayPlot

  19. n1=Table[If[maze[[i,j]]*maze[[i,j+1]]==1,
  20. UndirectedEdge[Length@maze*(i-1)+j,Length@maze*(i-1)+j+1]],{i,1,Length@maze},{j,1,Length@maze-1}];
  21. n2=Table[If[maze[[i,j]]*maze[[i+1,j]]==1,UndirectedEdge[Length@maze*(i-1)+j,Length@maze*i+j]],{i,1,Length@maze-1},{j,1,Length@maze}];
  22. medge=Select[Flatten[{n1,n2}],Length@#>=2&]

  23. mg=Graph[Range[Length@maze*Length@maze],
  24. medge,{FrameTicks->None,VertexCoordinates->
  25. Flatten[Table[{i,j},{i,0,Length@maze-1},{j,0,Length@maze-1}],1]}]

  26. mpath=FindShortestPath[mg,First@VertexList[mg],Last@VertexList[mg]]

  27. HighlightGraph[mg,PathGraph@FindShortestPath[mg,First@VertexList[mg],Last@VertexList[mg]]]

  28. sp[k_]:=Flatten[{UndirectedEdge[#[[1]],#[[2]]]->{Thickness[0.01],
  29. Red}}&/@Partition[FindShortestPath[mg,First@VertexList[mg],Last@VertexList[mg]],
  30. 2,1]][[1;;k]]

  31. Animate[Graph[Range[Length@maze*Length@maze],
  32. medge,{FrameTicks->None,VertexCoordinates->Flatten[
  33. Table[{i,j},{i,0,Length@maze-1},{j,0,Length@maze-1}],
  34. 1]},GraphHighlightStyle->Thick[0.01],
  35. EdgeStyle->sp[z]],{z,1,52,1}]
复制代码

评分

1

查看全部评分

回复 不支持

使用道具 举报

 楼主| 发表于 2011-1-5 14:03:34 | 显示全部楼层 来自 北京
4# FreddyMusic
正在preview状态,不知道为什么没有发表

http://demonstrations.wolfram.com/preview.html?draft/67796/000001/MazeGeneratorAndSolver
回复 不支持

使用道具 举报

发表于 2011-1-5 15:55:24 | 显示全部楼层 来自 上海
然后选 Submit for publications.就可以了.

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?注册

×
回复 不支持

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

Archiver|小黑屋|联系我们|仿真互动网 ( 京ICP备15048925号-7 )

GMT+8, 2024-4-26 01:05 , Processed in 0.049040 second(s), 18 queries , Gzip On, MemCache On.

Powered by Discuz! X3.5 Licensed

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表