Maze solving: Difference between revisions

Content added Content deleted
Line 1,437: Line 1,437:


<syntaxhighlight lang="text">
<syntaxhighlight lang="text">
size = 20
size = 15
n = 2 * size + 1
n = 2 * size + 1
endpos = n * n - 1
startpos = n + 2
#
f = 100 / (n - 0.5)
f = 100 / (n - 0.5)
len m[] n * n
len m[] n * n
Line 1,452: Line 1,449:
x = (i - 1) mod n
x = (i - 1) mod n
y = (i - 1) div n
y = (i - 1) div n
color 777
color 999
move x * f - f / 2 y * f - f / 2
move x * f - f / 2 y * f - f / 2
rect f * 1.5 f * 1.5
rect f * 1.5 f * 1.5
.
.
.
.
sleep 0.001
sleep 0.01
.
.
offs[] = [ 1 n -1 (-n) ]
offs[] = [ 1 n -1 (-n) ]
brdc[] = [ n - 2 -1 1 -1 ]
brdr[] = [ -1 n - 2 -1 1 ]
#
#
proc m_maze pos . .
proc m_maze pos . .
Line 1,469: Line 1,464:
for i = 4 downto 1
for i = 4 downto 1
d = random i
d = random i
dir = d[d]
dir = offs[d[d]]
d[d] = d[i]
d[d] = d[i]
r = (pos - 1) div n
if m[pos + dir] = 1 and m[pos + 2 * dir] <> 0
c = (pos - 1) mod n
m[pos + dir] = 0
posn = pos + 2 * offs[dir]
call m_maze pos + 2 * dir
if c <> brdc[dir] and r <> brdr[dir] and m[posn] <> 0
posn = pos + 2 * offs[dir]
m[pos + offs[dir]] = 0
call m_maze posn
.
.
.
.
.
.
endpos = n * n - 1
startpos = n + 2
proc make_maze . .
proc make_maze . .
for i = 1 to len m[]
for i = 1 to len m[]
m[i] = 1
m[i] = 1
.
for i = 1 to n
m[i] = 2
m[n * i] = 2
m[n * i - n + 1] = 2
m[n * n - n + i] = 2
.
.
call m_maze startpos
call m_maze startpos
m[endpos] = 0
m[endpos] = 0
endpos += n
.
.
call make_maze
call make_maze
Line 1,496: Line 1,496:
color col
color col
move x * f + f / 4 y * f + f / 4
move x * f + f / 4 y * f + f / 4
circle f / 4
circle f / 2.5
.
.
proc solve dir0 pos . found .
proc solve dir0 pos . found .
call mark pos 900
call mark pos 900
sleep 0.05
sleep 0.08
if pos = endpos
if pos = endpos
found = 1
found = 1
else
break 1
.
for dir = 1 to 4
posn = pos + offs[dir]
for dir = 1 to 4
if dir <> dir0 and m[posn] = 0 and found = 0
posn = pos + offs[dir]
call solve (dir + 1) mod 4 + 1 posn found
if dir <> dir0 and m[posn] = 0 and found = 0
if found = 0
posn += offs[dir]
call mark posn 777
call solve (dir + 1) mod 4 + 1 posn found
sleep 0.05
if found = 0
.
call mark posn 777
sleep 0.08
.
.
.
.
.
.
.
.
sleep 2
sleep 1
call solve 0 startpos found
call solve 0 startpos found
</syntaxhighlight>
</syntaxhighlight>