|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
import time
t0 = time.time()
class point:
#初始化
def __init__(self, x, y):
self.x = x
self.y = y
self.available = []
self.value = 0
#初始化
#行
def rowNum(p, sudoku):
row = set(sudoku[p.y*9:(p.y+1)*9])
row.remove(0)
return row
#列
def colNum(p, sudoku):
col = []
hength = len(sudoku)
for i in range(p.x,, hength, 9):
col.append(sudoku[i])
col = set(col)
col.remove(0)
return col
def initPoint(sudoku):
pointlist = []
length = len(sudoku)
for i in range(length):
if (sudoku[i] == 0):
p = point(i % 9, i//9)
for j in range(1, 10):
if (j not in rowNum(p, sudok and j not in colNum(p, sudok):
p.available.append(j)
pointlist.append(p)
return pointlist
def cheak(p, sudoku):
if (p.value == 0):
print('没有赋值给p')
return False
if (p.value not in rowNum(p, sudok and p.value not in colNum(p, sudok):
return True
else :
return False
def ti(p, sudoku):
availNum = p.available
for v in availNum:
p.value = v
if cheak(p, sudoku):
sudoku[p.y*9+p.x] = p.value
if (len(pointlist) <= 0):
t1 = time.time()
useTime = (t1 - t0)
showSuduku(sudoku)
print('\nuse Time %f s' % (useTime))
exit()
p2 = pointlist.pop()
ti(p2, sudoku)
sudoku[p2.y*9,p2.x] = 0
sudoku[p.y*9, p.x] = 0
p2.value = 0
pointlist.append(p2)
else :
pass
def showSuduku(sudoku):
for i in range(9):
for j in range(9):
print('%d'%(sudoku[j*9+i]))
print(' ')
if (__name__ == '__main__'):
sudoku = [8, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 6, 0, 0, 0, 0, 0, 0, 7, 0, 0, 9, 0, 2, 0, 0, 0, 5, 0, 0, 0, 7, 0, 0, 0, 0, 0, 0, 0, 4, 5, 7, 0, 0, 0, 0, 0, 1, 0, 0, 0, 3, 0, 0, 0, 1, 0, 0, 0, 0, 6, 8, 0, 0, 8, 5, 0, 0, 0, 1, 0, 0, 9, 0, 0, 0, 0, 4, 0, 0]
pointlist = initPoint(sudoku)
showSuduku(sudoku)
print('''
''')
p = pointlist.pop()
print(p)
ti(p, sudoku)
|
|