题解 | #Sudoku#学习大佬后自己敲一遍
Sudoku
http://www.nowcoder.com/practice/78a1a4ebe8a34c93aac006c44f6bf8a1
def isRightNum(all_list, x, y):
for i in range(9):
if all_list[x][i] == all_list[x][y] and i != y:
return False
if all_list[i][y] == all_list[x][y] and i != x:
return False
m, n = 3 * (x // 3), 3 * (y // 3)
for i in range(3):
for j in range(3):
if all_list[m+i][n+j] == all_list[x][y] and ((m+i) != x or (n+j) != y):
return False
return True
def changeNum(all_list):
for i in range(9):
for j in range(9):
if all_list[i][j] == 0:
for n in '123456789':
all_list[i][j] = int(n)
if isRightNum(all_list, i, j) and changeNum(all_list):
return True
all_list[i][j] = 0
else:
return False
return True
while True:
try:
all_list = []
for i in range(9):
all_list.append([int(x) for x in input().split()])
changeNum(all_list)
for j in range(9):
print(' '.join([str(x) for x in all_list[j]]))
except:
break
for i in range(9):
if all_list[x][i] == all_list[x][y] and i != y:
return False
if all_list[i][y] == all_list[x][y] and i != x:
return False
m, n = 3 * (x // 3), 3 * (y // 3)
for i in range(3):
for j in range(3):
if all_list[m+i][n+j] == all_list[x][y] and ((m+i) != x or (n+j) != y):
return False
return True
def changeNum(all_list):
for i in range(9):
for j in range(9):
if all_list[i][j] == 0:
for n in '123456789':
all_list[i][j] = int(n)
if isRightNum(all_list, i, j) and changeNum(all_list):
return True
all_list[i][j] = 0
else:
return False
return True
while True:
try:
all_list = []
for i in range(9):
all_list.append([int(x) for x in input().split()])
changeNum(all_list)
for j in range(9):
print(' '.join([str(x) for x in all_list[j]]))
except:
break