Comment detail
必ず解ける迷路 (Nested Flatten)This comment is reply for 5322 にしお: >グラフの最小全域木を求めるPri...(必ず解ける迷路). Go to thread root.
というわけで、通路の最小全域木を作るものを実装してみました。穴掘りです。
Pentium4/2.8GHzで1分半ほど。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 | class Maze
WALL, VAC = '#', ' '
DIRS = [[+1, 0], [-1, 0], [0, +1], [0, -1]]
def initialize(m, n)
@rows = m
@cols = n
@field = Array.new(2*@rows+1) { WALL*(2*@cols+1) }
end
def gen
conn = Array.new(@rows) { Array.new(@cols, false) }
cand = [[rand(@rows), rand(@cols), 0, 0]] # (0,0) is dummy
until cand.empty?
y, x, dy, dx = cand.slice!(rand(cand.size))
next if conn[y][x]
conn[y][x] = true
@field[2*y+1-dy][2*x+1-dx] = VAC
@field[2*y+1][2*x+1] = VAC
DIRS.each do |mv|
y_, x_ = y+mv[0], x+mv[1]
if (0...@rows) === y_ &&
(0...@cols) === x_ &&
!conn[y_][x_]
cand << [y_, x_, mv[0], mv[1]]
end
end
end
self
end
def p
puts @field
end
end
M, N = 10, 10
maze = Maze.new(M, N)
maze.gen.p
|





minke
#5323()
Rating0/0=0.00