Anonymous user
Maze solving: Difference between revisions
m
→{{header|Icon}} and {{header|Unicon}}
(Added Erlang) |
|||
Line 1,558:
mz := DisplayMaze(GenerateMaze(mh,mw)) # Build and show maze
QMouse(mz.maze,findStart(mz.maze),&null,0) # Start first quantum mouse
waitForCompletion() # block until all quantum mice have finished
Line 1,591:
$define EMPTY 0 # like new
class QMouse(maze, loc,
method
method getParent(); return \parent; end
method getLen(); return len; end
method atEnd(); return EMPTY ~= iand(val, FINISH); end
method goNorth(); if EMPTY ~= iand(val,NORTH) then return visit(loc.r-1, loc.c); end
Line 1,602 ⟶ 1,604:
method visit(r,c)
critical region[r,c]: if EMPTY = iand(maze[r,c],SEEN) then {
if /bestMouse | (
mark(maze, r,c)
unlock(region[r,c])
Line 1,610 ⟶ 1,612:
end
initially (m, l, p, n)
initial { # Construct critical region mutexes and completion condvar
qMice := mutex(set())
Line 1,621 ⟶ 1,623:
maze := m
loc := l
len := n+1
val := maze[loc.r,loc.c] | fail # Fail if outside maze
▲ /path := []
insert(qMice, self)
thread {
if atEnd() then {
critical bestMouseLock:
if /bestMouse | (
}
else { # Spawn more mice to look for finish
QMouse(maze, goNorth(),
QMouse(maze, goSouth(),
QMouse(maze, goEast(),
QMouse(maze, goWest(),
}
Line 1,661 ⟶ 1,662:
procedure showPath(maze)
if path := \bestMouse then { # Mark it in maze
repeat {
every p := !bestMouse.path do maze[p.r,p.c] +:= PATH▼
loc := path.getLoc()
path := \path.getParent() | break
}
return
}
|