好的,所以我不得不为这个打破喜力啤酒。尝试用文本文件解决最短路径问题。
我的研究带我去: link text link text 我的算法非常好的信息
基本上,我试图从'S'开始,然后退出'F'表示完成或返回无解决方案消息
正在考虑的两种自我测试或文本文件
##########
#S #
###### #
#F #
##########
and non-solvable
##########
#S #
##########
#F #
##########
我的问题是我在为此编写路径查找def并将其返回到自定义打印def时遇到问题。下面是整个darn事物的代码。以及我的第一次尝试。空间可以自由地成为一条道路。墙被定义为任何其他键,但空格键。还要注意我的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"
答案 0 :(得分:1)
以下是有关您的代码的一些提示:
solve_maze
有四个参数,但您只在初始调用中传递一个(solve_maze(maze)
)self
来引用当前对象,但您甚至没有当前对象,因为您是以程序样式而不是对象来编写的 - 导向的。 [注意:我知道在Ruby中,你总是有一个当前对象,在这种情况下是匿名的顶级对象。] 以下是关于您的工作流程的更多提示:
示例:
solvable = [
[false, false, false, false, false, false, false, false, false, false],
[false, true, true, true, true, true, true, true, true, false],
[false, false, false, false, false, false, true, true, true, false],
[false, true, true, true, true, true, true, true, true, false],
[false, false, false, false, false, false, false, false, false, false]
]
unsolvable = [
[false, false, false, false, false, false, false, false, false, false],
[false, true, true, true, true, true, true, true, true, false],
[false, false, false, false, false, false, false, false, false, false],
[false, true, true, true, true, true, true, true, true, false],
[false, false, false, false, false, false, false, false, false, false]
]
start = [1, 1]
finish = [3, 1]
这样,您只需使用if
来检查路径是否空闲,因为空图块为true
,墙为false
这是一个巧妙的技巧:在Ruby中,所有不是false
或nil
的东西都是真的。这意味着,您可以将所需内容(当然除false
或nil
除外)放在那里而不是true
,if
技巧仍然有用。例如,您可以直接在迷宫中编码算法所需的距离信息。只需在其中添加一些真正的大号而不是true
,并在结束时添加0
:
infin = 1.0/0.0
solvable = [
[false, false, false, false, false, false, false, false, false, false],
[false, infin, infin, infin, infin, infin, infin, infin, infin, false],
[false, false, false, false, false, false, infin, infin, infin, false],
[false, 0 , infin, infin, infin, infin, infin, infin, infin, false],
[false, false, false, false, false, false, false, false, false, false]
]
unsolvable = [
[false, false, false, false, false, false, false, false, false, false],
[false, infin, infin, infin, infin, infin, infin, infin, infin, false],
[false, false, false, false, false, false, false, false, false, false],
[false, 0 , infin, infin, infin, infin, infin, infin, infin, false],
[false, false, false, false, false, false, false, false, false, false]
]
start = [1, 1]
finish = [3, 1]
然而,从简单开始:
maze = [[0]]
start = [0, 0]
finish = [0, 0]
maze = [[0, infin]]
start = [0, 1]
finish = [0, 0]
maze = [
[false, false, false],
[false, infin, false],
[false, 0 , false],
[false, false, false]
]
start = [1, 1]
finish = [2, 1]
maze = [
[false, false, false],
[false, infin, false],
[false, infin, false],
[false, 0 , false],
[false, false, false]
]
start = [1, 1]
finish = [3, 1]