1
好吧,所以我不得不为这个破解一个喜力。试图用文本文件解决最短路径问题。寻路条件def
我的研究带我去: link text link text 相当不错的信息,我的算法
基本上,我想在“S”为起点,开始了和“F”戒烟完成或回报无解的消息
两个样式自我测试或文本文件被认为
##########
#S #
###### #
#F #
##########
and non-solvable
##########
#S #
##########
#F #
##########
我的问题是我有特鲁为此写了一个路径查找def并将其返回到自定义打印def。下面是整个织物的代码。以及我的第一次尝试。空间可以自由成为一条道路。墙被定义为任何其他键,但空格键。同时也要警告我的while循环退出会返回一个错误,我现在正在处理这个错误。
puts "enter a file name (example maze1.mz) PRESS ENTER"
filename = gets.chomp.to_s
while filename != 'quit' do
def read_maze(filename)
local_maze = []
mz_file = File.open(filename,"r")
while ! mz_file.eof? do
line = mz_file.gets.chomp
local_maze << line.scan(/./)
end
mz_file.close
return local_maze
end
def solve_maze(row, column, blank_spot, blank_spot_not) #parse throug args
#from http://en.wikipedia.org/wiki/Maze
#from http://en.wikipedia.org/wiki/Pathfinding
return unless self[column][row] #condition for its full
return if self[column][row] != space #condition for printable spot
return if self[column][row] == blank_spot_not #can't use this spot
return if self[column][row]== full or f encountered
#maze is full can't solve or finished
self[column][row] = blank_spot_not
#direction up down left right
solve_maze(column+1, row, space, blank_spot_not) #right
solve_maze(column-1, row, space, blank_spot_not) #left
solve_maze(column, row+1, space, blank_spot_not) #up
solve_maze(column, row-1, space, blank_spot_not) #down
end
def print_maze(maze)
maze.each {|row|
puts row.join('')
}
end
maze = read_maze(filename)
print_maze(maze)
solve_maze(maze)
print_maze(maze)
puts "would you like to further drive yourself nuts with this maze solver"
filename = gets.chomp.to_s
maze = read_maze(filename)
print_maze(maze)
end
puts "you have quit"