Just another update

This commit is contained in:
Ingy döt Net 2015-02-20 00:35:01 -05:00
parent a25938f123
commit 00a190b0a6
6591 changed files with 94363 additions and 23227 deletions

View file

@ -1,5 +1,4 @@
For a maze generated by [[Maze generation|this task]], write a function that
finds (and displays) the shortest path between two cells. Note that because these
mazes are generated by the [[wp:Maze_generation_algorithm#Depth-first_search|Depth-first search]]
algorithm, they contain no circular paths, and a simple depth-first tree search
can be used.
For a maze generated by [[Maze generation|this task]], write a function
that finds (and displays) the shortest path between two cells.
Note that because these mazes are generated by the [[wp:Maze_generation_algorithm#Depth-first_search|Depth-first search]] algorithm, they contain no circular paths,
and a simple depth-first tree search can be used.

View file

@ -1,4 +1,5 @@
---
category:
- Recursion
- Puzzles
note: Games

View file

@ -1,16 +1,16 @@
import std.stdio, std.random, std.string, std.array, std.algorithm,
std.file;
std.file, std.conv;
enum int cx = 4, cy = 2; // Cell size x and y.
enum int cx2 = cx / 2, cy2 = cy / 2;
enum pathSymbol = '.';
struct V2 { int x, y; }
bool solveMaze(char[][] maze, in V2 s, in V2 end) pure nothrow {
bool solveMaze(char[][] maze, in V2 s, in V2 end) pure nothrow @safe @nogc {
if (s == end)
return true;
foreach (d; [V2(0, -cy), V2(+cx, 0), V2(0, +cy), V2(-cx, 0)])
foreach (immutable d; [V2(0, -cy), V2(+cx, 0), V2(0, +cy), V2(-cx, 0)])
if (maze[s.y + (d.y / 2)][s.x + (d.x / 2)] == ' ' &&
maze[s.y + d.y][s.x + d.x] == ' ') {
maze[s.y + d.y][s.x + d.x] = pathSymbol;
@ -23,15 +23,13 @@ bool solveMaze(char[][] maze, in V2 s, in V2 end) pure nothrow {
}
void main() {
auto maze = "maze.txt".readText.splitLines.map!(r => r.dup).array;
immutable int h = ((cast(int)maze.length) - 1) / cy;
auto maze = "maze.txt".File.byLine.map!(r => r.chomp.dup).array;
immutable h = (maze.length.signed - 1) / cy;
assert (h > 0);
immutable int w = ((cast(int)maze[0].length) - 1) / cx;
immutable w = (maze[0].length.signed - 1) / cx;
immutable start = V2(cx2 + cx * uniform(0, w),
cy2 + cy * uniform(0, h));
immutable end = V2(cx2 + cx * uniform(0, w),
cy2 + cy * uniform(0, h));
immutable start = V2(cx2 + cx * uniform(0, w), cy2 + cy * uniform(0, h));
immutable end = V2(cx2 + cx * uniform(0, w), cy2 + cy * uniform(0, h));
maze[start.y][start.x] = pathSymbol;
if (!solveMaze(maze, start, end))

View file

@ -1,3 +1,28 @@
maze=:4 :0
assert.0<:n=.<:x*y
horiz=. 0$~x,y-1
verti=. 0$~(x-1),y
path=.,:here=. ?x,y
unvisited=.0 (<here+1)} 0,0,~|:0,0,~1$~y,x
while.n do.
neighbors=. here+"1 (,-)=0 1
neighbors=. neighbors #~ (<"1 neighbors+1) {unvisited
if.#neighbors do.
n=.n-1
next=. ({~ ?@#) neighbors
unvisited=.0 (<next+1)} unvisited
if.{.next=here
do. horiz=.1 (<-:here+next-0 1)} horiz
else. verti=. 1 (<-:here+next-1 0)} verti end.
path=.path,here=.next
else.
here=.{:path
path=.}:path
end.
end.
horiz;verti
)
NB. source Dijkstra_equal_weights graph
NB.
NB. + +---+---+
@ -35,7 +60,7 @@ Dijkstra_equal_weights =: 4 : 0
dist;previous
)
path =. 3 : 0
path =: 3 : 0
p =. <:#y
while. _ > {:p do.
p =. p,y{~{:p
@ -56,7 +81,7 @@ solve=:3 :0
path , > {: 0 Dijkstra_equal_weights graph
)
display=:3 :0 NB. Monadic display copied from maze generation task
display=:3 :0
size=. >.&$&>/y
text=. (}:1 3$~2*1+{:size)#"1":size$<' '
'hdoor vdoor'=. 2 4&*&.>&.> (#&,{@;&i./@$)&.> y
@ -66,16 +91,5 @@ display=:3 :0 NB. Monadic display copied from maze generation task
size=. >.&$&>/y
columns=. {: size
cells =. <"1(1 2&p.@<.@(%&columns) ,. 2 4&p.@(columns&|))x
'+' cells } a NB. exercise, replace cells with a gerund to draw arrows on the path.
'o' cells } a NB. exercise, replace cells with a gerund to draw arrows on the path.
)
4 (display~ solve)@maze 9
┌ ┬───┬───┬───┬───┬───┬───┬───┬───┐
│ + │ │ │
├ ┼───┼ ┼ ┼ ┼───┼ ┼ ┼ ┤
│ + + │ │ │ │ │
├───┼ ┼───┼───┼───┼───┼───┼ ┼───┤
│ │ + + + │ + + + │ │
├ ┼───┼───┼ ┼ ┼───┼ ┼───┼───┤
│ + + │ + + +
└───┴───┴───┴───┴───┴───┴───┴───┴───┘