Ruby 骑士';s阵痛:递归解决方案

Ruby 骑士';s阵痛:递归解决方案,ruby,recursion,binary-tree,binary-search-tree,knights-tour,Ruby,Recursion,Binary Tree,Binary Search Tree,Knights Tour,问题是创建一个数据结构,类似于二进制搜索树,它可以列出骑士(在国际象棋中)在8x8棋盘上可能做出的所有动作。我提出了一个带有当前位置的单节点类,一个父节点和8个可能的子节点,代表了一个骑士可以做出的8个可能的动作 class KnightNode attr_accessor :location, :child_1, :child_2, :child_4, :child_5, :child_7, :child_8, :child_10, :child_11 def initialize(l

问题是创建一个数据结构,类似于二进制搜索树,它可以列出骑士(在国际象棋中)在8x8棋盘上可能做出的所有动作。我提出了一个带有当前位置的单节点类,一个父节点和8个可能的子节点,代表了一个骑士可以做出的8个可能的动作

class KnightNode
  attr_accessor :location, :child_1, :child_2, :child_4, :child_5, :child_7, :child_8, :child_10, :child_11
  def initialize(location = nil)
    @location = location
    @parent = nil
    #8 possible children, label them as if they were hands on a clock
    @child_1 = nil
    @child_2 = nil
    @child_4 = nil
    @child_5 = nil
    @child_7 = nil
    @child_8 = nil
    @child_10 = nil
    @child_11 = nil
  end
end

def generate_tree(location)
  root = KnightNode.new(location)
  move1 = [root.location[0] + 1,root.location[1] + 2]
  move2 = [root.location[0] + 2,root.location[1] + 1]
  move4 = [root.location[0] + 2,root.location[1] - 1]
  move5 = [root.location[0] + 1,root.location[1] - 2]
  move7 = [root.location[0] - 1,root.location[1] - 2]
  move8 = [root.location[0] - 2,root.location[1] - 1]
  move10 = [root.location[0] - 2,root.location[1] - 1]
  move11 = [root.location[0] - 1,root.location[1] + 2]
  move1[0] > 7 && move1[1] > 7 ? root.child_1 = nil : root.child_1 = generate_tree(move1)
  move2[0] > 7 && move2[1] > 7 ? root.child_2 = nil : root.child_2 = generate_tree(move2)
  move4[0] > 7 && move4[1] < 0 ? root.child_4 = nil : root.child_4 = generate_tree(move4)
  move5[0] > 7 && move5[1] < 7 ? root.child_5 = nil : root.child_5 = generate_tree(move5)
  move7[0] < 0 && move7[1] < 7 ? root.child_7 = nil : root.child_7 = generate_tree(move7)
  move8[0] < 0 && move8[1] < 0 ? root.child_8 = nil : root.child_8 = generate_tree(move8)
  move10[0] < 0 && move10[1] < 0 ? root.child_10 = nil : root.child_10 = generate_tree(move10)
  move11[0] < 0 && move11[1] > 7 ? root.child_11 = nil : root.child_11 = generate_tree(move11)
  return root
end

generate_tree([3,3])
class KnightNode
属性访问器:位置,:子对象1,:子对象2,:子对象4,:子对象5,:子对象7,:子对象8,:子对象10,:子对象11
def初始化(位置=零)
@位置=位置
@父项=零
#8个可能的孩子,给他们贴上标签,就好像他们是时钟上的指针一样
@child_1=零
@child_2=零
@child_4=零
@child_5=零
@child_7=零
@child_8=零
@child_10=零
@child_11=零
结束
结束
def生成树(位置)
root=KnightNode.new(位置)
move1=[root.location[0]+1,root.location[1]+2]
move2=[root.location[0]+2,root.location[1]+1]
move4=[root.location[0]+2,root.location[1]-1]
move5=[root.location[0]+1,root.location[1]-2]
move7=[root.location[0]-1,root.location[1]-2]
move8=[root.location[0]-2,root.location[1]-1]
move10=[root.location[0]-2,root.location[1]-1]
move11=[root.location[0]-1,root.location[1]+2]
move1[0]>7&&move1[1]>7?root.child\u 1=nil:root.child\u 1=generate\u树(move1)
move2[0]>7&&move2[1]>7?root.child_2=nil:root.child_2=generate_树(move2)
move4[0]>7&&move4[1]<0?root.child_4=nil:root.child_4=generate_树(move4)
move5[0]>7&&move5[1]<7?root.child_5=nil:root.child_5=generate_树(move5)
move7[0]<0&&move7[1]<7?root.child_7=nil:root.child_7=generate_树(move7)
move8[0]<0&&move8[1]<0?root.child_8=nil:root.child_8=generate_树(move8)
move10[0]<0&&move10[1]<0?root.child_10=nil:root.child_10=generate_树(move10)
move11[0]<0&&move11[1]>7?root.child_11=nil:root.child_11=generate_树(move11)
返回根
结束
生成_树([3,3])

当我运行这段代码时,我遇到了SystemStackError。我的猜测是,我的递归正在经历一个无限循环,但是我看不出问题所在。提前谢谢

我看不出你有什么办法终止搜索。如果它离开棋盘,你可以修剪它,但是你需要人为地将它限制在64(棋盘上的方块数)和/或跟踪它已经访问过的地方,不要让它再次访问该方块