迷宫为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]
以上为使用欧几里得距离八个方向代码。
效果如下图所示