题解 | 简单迭代一下#Sudoku#
Sudoku
https://www.nowcoder.com/practice/78a1a4ebe8a34c93aac006c44f6bf8a1
def check(l):
ref = set(map(str,[1,2,3,4,5,6,7,8,9]))
# return sorted(list(ref - set(map(str,l))))
return ref - set(map(str,l))
def block(M, a, b):
m,n = a//3, b//3
temp = M[3*m:3*m+3]
temp = [x[3*n:3*n+3] for x in temp]
return temp[0] + temp[1] + temp[2]
mm = []
while True:
try:
mm.append(list(map(int, input().split())))
except:
break
def fillout(m):
for i in range(9):
for j in range(9):
if m[i][j] == 0:
temp = list(check(m[i]) & check([x[j] for x in m]) & check(block(m,i,j)))
if temp != []:
sq = [x[:] for x in m]
for k in temp:
sq[i][j] = int(k)
ans = fillout(sq)
if ans:
return ans
else:
continue
if ans == False:
return False
else:
return sq
else:
return False
elif i == j == 8:
return m
# m[i][j] = list(check(m[i]) & check([x[j] for x in m]) & check(block(m,i,j)))[0]
mmm = fillout(mm)
for line in mmm:
print(' '.join(list(map(str,line))))
查看23道真题和解析
顺丰集团工作强度 335人发布