题解 | #Sudoku#
Sudoku
http://www.nowcoder.com/practice/78a1a4ebe8a34c93aac006c44f6bf8a1
def check(n,x,y,M):
block = [M[x//3*3][y//3*3],M[x//3*3][y//3*3+1],M[x//3*3][y//3*3+2],
M[x//3*3+1][y//3*3],M[x//3*3+1][y//3*3+1],M[x//3*3+1][y//3*3+2],
M[x//3*3+2][y//3*3],M[x//3*3+2][y//3*3+1],M[x//3*3+2][y//3*3+2]]
if n in M[x]:
return False
elif n in [M[_][y] for _ in range(9)]:
return False
elif n in block:
return False
else:
return True
def solu(M):
global maze
for i in range(9):
for j in range(9):
if M[i][j] == 0:
for k in range(1,10):
if check(k,i,j,M):
M[i][j] = k
if solu(M.copy()):
maze = M
return True
M[i][j] = 0
return False
return True
while True:
try:
maze = []
for i in range(9):
maze.append(list(map(int,input().split())))
result = solu(maze.copy())
for _ in range(9):
print(*maze[_])
except:
break