我正在尝试为我的Sudoku Solver编写一个求解器,但是当我运行我的数独求解器时,我收到以下错误。我不明白问题是什么。我看其他问题,但也无法解决问题。 以下是我的代码和问题:
import numpy as np
class SolveSudoku(object):
def __init__(self,grid):
self.grid = grid
def _check_row(self,digit,row):
return digit not in self.grid[row,:]
def _check_column(self,digit,column):
return digit not in self.grid[:,column]
def _check_box(self,digit,row,col):
rc = (row/3)*3
cc = (col/3)*3
return digit not in self.grid[rc:rc+3,cc:cc+3]
def _check_safe(self,digit,row,col):
return self._check_box(digit,row,col) \
and self._check_row(digit,row) \
and self._check_column(digit,col)
def _find_empty(self,stop):
if 0 in self.grid:
stop[0] = np.where(self.grid==0)[0][0]
stop[1] = np.where(self.grid==0)[1][0]
return True
else:
return False
def _solve(self):
stop = [0,0]
if not self._find_empty(stop):
return True
for digit in np.arange(1,10):
row = stop[0]
col = stop[1]
if self._check_safe(digit,row,col):
self.grid[row,col] = digit
if self._solve():
return True
self.grid[row,col]=0
return False
def solve(self):
if self._solve():
return self.grid
raise("Sudoku can not be solved")
File "solve.py", line 117, in <module>
grid = sudoku.solve()
File "C:\Users\Erke\.spyder-py3\SudokuSolver-master\sudoku.py", line 47, in solve
if self._solve():
File "C:\Users\Erke\.spyder-py3\SudokuSolver-master\sudoku.py", line 39, in _solve
if self._check_safe(digit,row,col):
File "C:\Users\Erke\.spyder-py3\SudokuSolver-master\sudoku.py", line 20, in _check_safe
return self._check_box(digit,row,col) \
File "C:\Users\Erke\.spyder-py3\SudokuSolver-master\sudoku.py", line 17, in _check_box
return digit not in self.grid[rc:rc+3,cc:cc+3]
TypeError: slice indices must be integers or None or have an __index__ method