매스 매 티카 491 323
주석이 달린 골퍼
절차는 마무리 ( "*")에서 시작하여 시작점에 도달 할 때까지 화살표를 찾습니다.
기능, f [maze].
(* positions of the arrowheads *)
aHeads[a_]:=Position[m,#]&/@{"^","v",">","<"}
(* position of the final labyrinth exit*)
final[a_]:=Position[a,"*"][[1]];
(* find arrowheads that point to the current location at {r,c} *)
aboveMe[{r_,c_},a_]:=Cases[aHeads[a][[2]],{r1_,c}/;r1<r]
belowMe[{r_,c_},a_]:=Cases[aHeads[a][[1]],{r1_,c}/;r1>r]
rightOfMe[{r_,c_},a_]:=Cases[aHeads[a][[4]],{r,c1_}/;c1>c]
leftOfMe[{r_,c_},a_]:=Cases[aHeads[a][[3]],{r,c1_}/;c1<c]
(*find the precursor arrowhead*)
precursor[{loc_,a_,list_:{}}]:=
(* end the search when the precursor has already been traversed or when there is no precursor *)
Which[MemberQ[list,loc],list,
loc=={},list,True,
(* otherwise find the next precursor *)
전구체 [{Flatten [{aboveMe [loc, a], 아래 Me [loc, a], rightOfMe [loc, a], leftOfMe [loc, a]}, 2], a, Prepend [list, loc]}]]
(* return the path through the maze from start to finish *)
f[maze_]:= precursor[{final[maze[[1]]],maze[[1]]}]
골프
f@z_:=Module[{p,h,m=z[[1]]},h@a_:=Position[a,#]&/@{"^","v",">","<","*"};
p[{v_,a_,j_:{}}]:=Module[{r,c,x=Cases},{r,c}=v;
Which[MemberQ[j,v],j,v=={},j,True,
p[{Flatten[{x[h[a][[2]],{r1_,c}/;r1<r],x[h[a][[1]],{r1_,c}/;r1>r],
x[h[a][[4]],{r,c1_}/;c1>c],x[h[a][[3]],{r,c1_}/;c1<c]},2],a,Prepend[j,v]}]]];
p[{Position[m,"*"][[1]],m}]]
예
미로. 정렬 된 각 쌍에는 셀의 행과 열이 포함됩니다. 예를 들어 {2, 3}은 2 행 3 열의 셀을 나타냅니다.
maze=Grid[Normal@ SparseArray[{{5, 5} -> "*",{1, 2} -> "v", {1, 5} -> "<",{2, 1} -> ">",
{2, 3} -> "v",{3, 3} -> ">", {3, 5} -> "^",{4, 1} -> ">", {4, 5} -> "v",{5, 1} -> "^",
{5, 2} -> "<",{_, _} -> " "}]]

입력
f[maze]
출력 : 시작부터 끝까지의 경로입니다.
{{2, 1}, {2, 3}, {3, 3}, {3, 5}, {1, 5}, {1, 2}, {5, 2}, {5, 1}, { 4, 1}, {4, 5}, {5, 5}}