Mathematica 实现A*迷宫搜索

该代码示例展示了如何在20x20的迷宫中,从点(4,4)到点(16,16)寻找路径,分别使用曼哈顿距离和欧几里得距离作为启发式函数。通过开放列表和关闭列表进行A*搜索算法,动态更新节点的估值,并用WolframMathematica生成路径动画。
摘要由CSDN通过智能技术生成

迷宫为20*20区域,其中起点和终点为(4,4)和(16,16) 

(*Manhattan*)
{start, end} = {{4, 4}, {16, 16}};
hManhattan[i_, j_] := Abs[i - end[[1]]] + Abs[j - end[[2]]]
maze = Table[0, {i, 20}, {j, 20}];
Table[maze[[i, j]] = 1, {i, 6, 9}, {j, 6, 11}];
Table[maze[[i, j]] = 1, {i, 13, 17}, {j, 10, 13}];
directions = {{0, 1}, {1, 0}, {0, -1}, {-1, 0}, {1, 1}, {-1, 
    1}, {-1, -1}, {1, -1}};
valf = Table[100, {i, 20}, {j, 20}];
openlist = {};
closelist = {};
g = 0;
f = g + hManhattan[start[[1]], start[[2]]];
valf[[start[[1]], start[[2]]]] = f;
AppendTo[openlist, start];
img = {};
step = 50;
visit = openlist[[1]];
Print["路径为:"]
While[Count[openlist, end] == 0,
 {c = Table[
    valf[[openlist[[i, 1]], openlist[[i, 2]]]], {i, Length[openlist]}],
  {d} = FirstPosition[c, Min[c]],
  visit = openlist[[d]],
  
  maze[[visit[[1]], visit[[2]]]] = step, step += 5,
  openlist = DeleteElements[openlist, {visit}],
  closelist = Append[closelist, visit],
  Print[visit],
  img = Append[img, 
    MatrixPlot[maze, PlotTheme -> "Scientific", Mesh -> True]],
  Do[{nodenext = visit + directions[[i]],
    If[0 < nodenext[[1]] < 20 && 0 < nodenext[[2]] < 20 && 
      maze[[nodenext[[1]], nodenext[[2]]]] != 1 && 
      Count[closelist, nodenext] == 0,
     {
      nodenextg = 
       g + Abs[directions[[i, 1]]] + Abs[directions[[i, 2]]],
      nodenexth = hManhattan[nodenext[[1]], nodenext[[2]]],
      nodenextf = nodenexth + nodenextg,
      If[nodenextf < valf[[nodenext[[1]], nodenext[[2]]]],
       {valf[[nodenext[[1]], nodenext[[2]]]] = nodenextf,
        AppendTo[openlist, nodenext]
        }
       ]
      }]}, {i, Length[directions]}]
  }]
maze[[end[[1]], end[[2]]]] = step + 5;
img = Append[img, 
   MatrixPlot[maze, PlotTheme -> "Scientific", Mesh -> True]];
Print[end]
page = Table[Graphics[img[[i]]], {i, Length[img]}];
ListAnimate[img]
Export["Manhattan.gif", page]

以上为使用曼哈顿距离八个方向代码。

效果如下图所示

(*Euclid*)
{start, end} = {{4, 4}, {16, 16}};
hEuclid[i_, j_] := Sqrt[(i - end[[1]])^2 + (j - end[[2]])^2]
maze = Table[0, {i, 20}, {j, 20}];
Table[maze[[i, j]] = 1, {i, 6, 9}, {j, 6, 11}];
Table[maze[[i, j]] = 1, {i, 13, 17}, {j, 10, 13}];
directions = {{0, 1}, {1, 0}, {0, -1}, {-1, 0}, {1, 1}, {-1, 
    1}, {-1, -1}, {1, -1}};
valf = Table[100, {i, 20}, {j, 20}];
openlist = {};
closelist = {};
g = 0;
f = g + hEuclid[start[[1]], start[[2]]];
valf[[start[[1]], start[[2]]]] = f;
AppendTo[openlist, start];
img = {};
step = 50;
visit = openlist[[1]];
Print["路径为:"]
While[Count[openlist, end] == 0,
 {c = Table[
    valf[[openlist[[i, 1]], openlist[[i, 2]]]], {i, 
     Length[openlist]}], {d} = FirstPosition[c, Min[c]],
  visit = openlist[[d]],
  
  maze[[visit[[1]], visit[[2]]]] = step, step += 5,
  openlist = DeleteElements[openlist, {visit}],
  closelist = Append[closelist, visit],
  Print[visit],
  img = Append[img, 
    MatrixPlot[maze, PlotTheme -> "Scientific", Mesh -> True]],
  Do[{nodenext = visit + directions[[i]],
    If[0 < nodenext[[1]] < 20 && 0 < nodenext[[2]] < 20 && 
      maze[[nodenext[[1]], nodenext[[2]]]] != 1 && 
      Count[closelist, nodenext] == 0,
     {
      nodenextg = 
       g + Sqrt[directions[[i, 1]]^2 + directions[[i, 2]]^2],
      nodenexth = hEuclid[nodenext[[1]], nodenext[[2]]],
      nodenextf = nodenexth + nodenextg,
      If[nodenextf < valf[[nodenext[[1]], nodenext[[2]]]],
       {valf[[nodenext[[1]], nodenext[[2]]]] = nodenextf,
        AppendTo[openlist, nodenext]
        }
       ]
      }]}, {i, Length[directions]}]
  }]
maze[[end[[1]], end[[2]]]] = step + 5;
Print[end]
img = Append[img, 
   MatrixPlot[maze, PlotTheme -> "Scientific", Mesh -> True]];
page = Table[Graphics[img[[i]]], {i, Length[img]}];
ListAnimate[img]
Export["Euclid.gif", page]

 以上为使用欧几里得距离八个方向代码。

效果如下图所示

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值