Tag: graph algorithm

跟踪路径,骑士旅行

到目前为止,我的最短路径方法将在到达目标位置时停止,打印出它沿途所做的一切。 我想知道如何实现父位置,以便我可以打印路径和目标。 这是一项任务。 class Knight attr_accessor :x, :y, :prev_position, :moves def initialize(position) @x = position[0] @y = position[1] @prev_position = nil @moves = [ [-1,-2], [-2,-1], [-2,+1], [-1,+2], [+1,-2], [+2,-1], [+2,+1], [+1,+2]] end def possible move_list = Array.new @moves.each do |moves| x = @x + moves[0] y = @y + moves[1] if x.between?(0,7) if y.between?(0,7) […]