Maze solving: Difference between revisions

m
(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, pathparent, len, val)
 
method getPathgetLoc(); return pathloc; end
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 | (*pathlen <= *bestMouse.pathgetLen()) then { # Keep going?
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
/pathparent := []p
len := n+1
val := maze[loc.r,loc.c] | fail # Fail if outside maze
/path := []
if \p then path := copy(p)
put(path, loc)
insert(qMice, self)
thread {
if atEnd() then {
critical bestMouseLock:
if /bestMouse | (*pathlen < bestMouse.pathgetLen()) then bestMouse := self
}
else { # Spawn more mice to look for finish
QMouse(maze, goNorth(), pathself, len)
QMouse(maze, goSouth(), pathself, len)
QMouse(maze, goEast(), pathself, len)
QMouse(maze, goWest(), pathself, len)
}
 
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()
every p := !bestMouse.path do maze[ploc.r,ploc.c] +:= PATH
path := \path.getParent() | break
}
return
}