Jump to content

Maze solving: Difference between revisions

Line 4,115:
│ · · · · · · · · · │ │ │ │ │ │ │ │ │ ╵ │
└───────────────────┴───────┴───┴───────────────┴───────┴───────────┴───────┴───────────────────┴───────┴──────── │ ┘</pre></small>
=={{header|Red}}==
(imports maze generation code, see http://rosettacode.org/wiki/Maze_generation#Red)
<lang Red>Red ["Maze solver"]
 
do %mazegen.red
print [
"start:" start: random size - 1x1
"end:" end: random size - 1x1
]
isnew?: function [pos] [not find visited pos]
open?: function [pos d] [
o: pos/y * size/x + pos/x + 1
0 = pick walls/:o d
]
expand: function [pos][
either any [
all [pos/x > 0 isnew? p: pos - 1x0 open? p 1]
all [pos/x < (size/x - 1) isnew? p: pos + 1x0 open? pos 1]
all [pos/y > 0 isnew? p: pos - 0x1 open? p 2]
all [pos/y < (size/y - 1) isnew? p: pos + 0x1 open? pos 2]
][append visited p insert path p][remove path]
path/1
]
path: reduce [start]
visited: []
until [end = expand path/1]
print reverse path</lang>
{{out}}
<pre>Maze width: 15
Maze height: 15
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _
| | | | _ |_ | _ _ _|
| _|_| |_ _| | | _| |_ | |
| |_ _| _ _| | _ _ _|_ _| |
|_ |_ | | _ _|_ _| _ | |
| _| _| | | _ _ _|_ _|_|
|_ _ _| _ _| |_| _ _ |_| |
| _| _|_ _ _ | | _|_ _ _| |
| | _| _ _ |_ |_ _ | | |
| _| _| _|_ |_ _ _|_ _| |
|_ | | _ | _| _ _ |_| |
| |_ _| | |_ _ _| | | _| |
| | _ _| |_ _ _ | |_ _|_ |_|
| |_ | |_ |_ _ _|_ _ _ |_ |
| _| | |_ | | | _| |
|_ _ _ _|_ _|_ _|_ _|_ _ _ _|_|
start: 2x4 end: 12x3
2x4 3x4 3x3 2x3 2x2 3x2 3x1 3x0 4x0 4x1 5x1 5x0 6x0 7x0 7x1 7x2 7x3 6x3 5x3 5x4 5x5 4x5 3x5 3x6 2x6 2x7 1x7 1x8 0x8 0x9 1x9 1x10 2x10 2x9 2x8 3x8 3x7 4x7 5x7 6x7 6x8 7x8 7x9 6x9 6x10 7x10 8x10 8x9 9x9 10x9 11x9 11x10 11x11 10x11 10x10 9x10 9x11 9x12 10x12 11x12 12x12 12x13 11x13 11x14 12x14 13x14 13x13 14x13 14x12 13x12 13x11 12x11 12x10 13x10 13x9 14x9 14x8 14x7 14x6 14x5 13x5 13x6 12x6 11x6 11x5 10x5 9x5 8x5 8x6 8x7 7x7 7x6 6x6 6x5 6x4 7x4 8x4 9x4 10x4 10x3 11x3 12x3
</pre>
 
=={{header|Ruby}}==
Anonymous user
Cookies help us deliver our services. By using our services, you agree to our use of cookies.