最近在用 recursive backtracking 写数独 (sudoku) solver,但调试的时候就出现stack level too deep (SystemStackError) 的问题,不知如何解决!
命令:ruby sudoku.rb < puzzle.txt
puzzle.txt 内容:
030090070
004800500
000001006
000000002
005406800
100000000
700200000
008003400
090070060
到 37 行是都会有这问题 以下是完整代码:
UNASSIGNED = 0
N = 9
problem = ''
while line = gets                               # read the txt file (puzzle)
  problem += line.chomp
end
problem = problem.split('').map(&:to_i).each_slice(9).to_a  # convert the puzzle into an 2-D integer array
grid = problem.dup
def FindUnassignedLocation(grid, row, col)
   0.upto(N-1) do |x|
    0.upto(N-1) do |y|
      if grid[x][y] == UNASSIGNED then return true end
    end
  end
  return false
end
def UsedInRow(grid, row, num)
  0.upto(N-1) do |col|
    if grid[row][col] == num then return true end
  end
  return false
end
def UsedInCol(grid, col, num)
  9.times do |row|
    if grid[row][col] == num then return true end
  end
  return false
end
def UsedInBox(grid, boxStartRow, boxStartCol, num)
  0.upto(2) do |row|
    0.upto(2) do |col|
      if grid[row+boxStartRow][col+boxStartCol] == num then
        return true
      end
    end
  end
  return false
end
def isSafe(grid, row, col, num)
  return !UsedInRow(grid, row, num) && !UsedInCol(grid, col, num) && !UsedInBox(grid, row - row%3, col - col%3, num)
end
def SolveSudoku(grid)
  row, col = 0, 0
  if !FindUnassignedLocation(grid, row, col) then
    return true
  end
  1.upto(9) { |num|                 # trying from 1 to 9
    if (isSafe(grid, row, col, num)) then
      grid[row][col] = num
      if SolveSudoku(grid) then             # stop when solved
        return true
      end
      grid[row][col] = UNASSIGNED       # undo and try again
    end
  }
    return false                    # this triggers backtracking
end
def printGrid(grid)
  grid.each {|s| p s}
end
if SolveSudoku(grid) == true then
  printGrid(grid)
else
  puts("No solution!")
end