验证数量为“NxN”的数独【难度:4级】:
答案1:
import math
class Sudoku(object):
def __init__(self, board):
self.board = board
def is_valid(self):
if not isinstance(self.board, list):
return False
n = len(self.board)
rootN = int(round(math.sqrt(n)))
if rootN * rootN != n:
return False
isValidRow = lambda r : (isinstance(r, list) and
len(r) == n and
all(map(lambda x : type(x) == int, r)))
if not all(map(isValidRow, self.board)):
return False
oneToN = set(range(1, n + 1))
isOneToN = lambda l : set(l) == oneToN
tranpose = [[self.board[j][i] for i in range(n)] for j in range(n)]
squares = [[self.board[p+x][q+y] for x in range(rootN)
for y in range(rootN)]
for p in range(0, n, rootN)
for q in range(0, n, rootN)]
return (all(map(isOneToN, self.board)) and
all(map(isOneToN, tranpose)) and
all(map(isOneToN, squares)))
答案2:
import numpy as np
class Sudoku(object):
def __init__(self, theArray):
self.grid = np.array(theArray)
self.listgrid = theArray
self.N = len(theArray)
self.M = len(theArray[0])
def has_valid_size(self):
if isinstance(self.listgrid[0][0], bool): return False
if self.grid.shape == (1,1):
return True
for i in self.listgrid:
if len(i) != self.N: return False
return True
def is_valid(self):
if not self.has_valid_size():
return False
seqs2check = [self.getRowsIterator(), self.getColsIterator(), self.getSquaresIterator()]
for it in seqs2check:
for seq in it:
if self.checkSeq(seq) == False:
return False
return True
def getRowsIterator(self):
for i in range(self.N):
yield self.grid[i,:]
def getColsIterator(self):
for i in range(self.N):
yield self.grid[:,i]
def getSquaresIterator(self):
squareSize = int(np.sqrt(self.N))
for i in range(squareSize):
for j in range(squareSize):
ix1 = i*squareSize
ix2 = j*squareSize
yield self.grid[ix1:ix1+squareSize, ix2:ix2+squareSize].flatten()
def checkSeq(self, seq):
return sorted(seq) == range(1,self.N+1)
答案3:
import math
class Sudoku(object):
def __init__(self, board):
self.board = board
def is_valid(self):
if not isinstance(self.board, list):
return False
n = len(self.board)
rootN = int(round(math.sqrt(n)))
if rootN * rootN != n:
return False
isValidRow = lambda r : (isinstance(r, list) and
len(r) == n and
all(map(lambda x : type(x) == int, r)))
if not all(map(isValidRow, self.board)):
return False
oneToN = set(range(1, n + 1))
isOneToN = lambda l : set(l) == oneToN
tranpose = [[self.board[i][j] for i in range(n)] for j in range(n)]
squares = [[self.board[p+x][q+y] for x in range(rootN)
for y in range(rootN)]
for p in range(0, n, rootN)
for q in range(0, n, rootN)]
return (all(map(isOneToN, self.board)) and
all(map(isOneToN, tranpose)) and
all(map(isOneToN, squares)))
答案4:
class Sudoku(object):
def __init__(self, board):
self.board = board
def is_valid(self):
n = int(len(self.board)**0.5)
if not all(len(row) == n*n and all(not isinstance(cell, bool) for cell in row) for row in self.board):
return False
r, rr = xrange(n), xrange(0, n*n, n)
blocks = [[self.board[x+a][y+b] for a in r for b in r] for x in rr for y in rr]
return not filter(lambda x: set(x) != set(range(1, n*n + 1)), self.board + zip(*self.board) + blocks)
答案5:
class Sudoku(object):
def __init__(self, grid):
self.grid = grid
self.dim = len(grid)
self.boxsize = int(self.dim ** 0.5)
self.nums = set(range(1, self.dim + 1))
def row_valid(self, row):
return self.nums == set(self.grid[row])
def col_valid(self, col):
return self.nums == set(self.grid[row][col] for row in range(self.dim))
def box_valid(self, box):
box_row, box_col = divmod(box, self.boxsize)
box_row, box_col = box_row * self.boxsize, box_col * self.boxsize
return self.nums == set(self.grid[row][col]
for row in range(box_row, box_row + self.boxsize)
for col in range(box_col, box_col + self.boxsize))
def is_valid(self):
if any(len(row) != self.dim for row in self.grid):
return False
if any(any(not isinstance(i, int) or i is True or i is False
for i in row)
for row in self.grid):
return False
return (all(self.row_valid(row) for row in range(self.dim)) and
all(self.col_valid(col) for col in range(self.dim)) and
all(self.box_valid(box) for box in range(self.dim)))
答案6:
import math
class Sudoku(object):
def __init__(self, board):
self.board = board
self.size = len(board)
@staticmethod
def reduce_logic(seq):
return reduce(lambda x, y: x and y, seq)
def is_square(self):
return self.reduce_logic(map(lambda x: len(x) == self.size, self.board))
def check_number(self):
return self.reduce_logic(map(lambda x: 0 < x <= self.size and type(x) == type(1),
reduce(lambda x, y: x + y, self.board)))
def check_seq(self, seq):
return self.reduce_logic(map(lambda x: len(set(x)) == self.size, seq))
def check_rows(self):
return self.check_seq(self.board)
def check_cols(self):
cols = [map(lambda x: x[i], self.board) for i in range(self.size)]
return self.check_seq(cols)
def check_little_squares(self):
sq = int(math.sqrt(self.size))
squares = [reduce(lambda x, y: x + y, map(lambda x: x[i:(i + sq)], self.board[j:(j + sq)]))
for j in range(0, self.size, sq)
for i in range(0, self.size, sq)]
return self.check_seq(squares)
def is_valid(self):
if (self.is_square() and
self.check_number() and
self.check_rows() and
self.check_cols() and
self.check_little_squares()):
return True
else:
return False
答案7:
class Sudoku(object):
def __init__(self, data):
self.data = data
def determineSize(self, data):
valid = True
row = len(data[0])
collumn = len(data)
if row == 4: block = 2
elif row < 4: block = 1
else:
for i in range(1, int(row/2)):
if row % i == 0:
block = i
for i in data:
for j in i:
if str(j).isdigit() == False: valid = False
if row == 1 and data[0][0] != 1: valid = False
return row, collumn, block, valid
def is_valid(self):
rowlen, collumnlen, blocklen, valid = self.determineSize(self.data)
board = self.data
if valid == False: return False
for row in board:
for i in range(1,rowlen):
if not(i in row):
return False
for collum in range(collumnlen-1):
temp = []
for row in board:
temp.append(row[collum])
for i in range(1,rowlen):
if not(i in temp):
return False
for a in range(blocklen):
for b in range(blocklen):
blockList = []
for c in range(blocklen):
for d in range(blocklen):
blockList.append(board[c+blocklen*a][d+blocklen*b])
for i in range(1,rowlen):
if not(i in blockList):
return False
return True
答案8:
import math
class Sudoku(object):
count = 0
t = False
data = None
col = []
tem = []
tem1 = []
mat = []
def __init__(self, data):
self.data = data
self.count = 0
self.t = False
self.col = []
self.tem = []
self.tem1 = []
self.mat = []
for i in range(len(data)):
for y in range(len(data[i])):
try:
if data[i][y] <= 0 or data[i][y] > len(data[i]) or not str(data[i][y]).isdigit():
self.t = True
break
else:
self.tem.append(data[y][i])
except:
self.t = True
break
if self.t:
break
self.col.append(self.tem)
self.tem = []
if math.sqrt(len(self.data)) % 1 == 0 and len(data) > 1 and not self.t:
x = int(math.sqrt(len(self.data)))
for i in range(0, len(data), x):
for j in range(len(data)):
self.tem1.append(data[j][i:i + x])
for y in range(0, len(self.tem1), x):
self.mat.append(sum(self.tem1[y:y+x], []))
def is_valid(self):
if math.sqrt(len(self.data)) % 1 == 0 and not self.t and len(self.data) > 1:
for i in range(len(self.data)):
if len(set(self.col[i])) != len(self.data[i]) or len(set(self.mat[i])) != len(self.data[i]):
return False
else:
return True
elif len(self.data) == 1 and not self.t:
return True
return False
答案9:
import numpy as np
class Sudoku(object):
def __init__(self, data):
self.data=data
def well_formed(self):
if not isinstance(self.data, list):
return False
N = len(self.data)
if not np.sqrt(N).is_integer():
return False
for l in self.data:
if not isinstance(self.data, list):
return False
if len(l) != N:
return False
if not all( e in range(1,N+1) for e in l):
return False
if any( isinstance(e, bool) for e in l):
return False
return True
def is_valid(self):
if not self.well_formed():
return False
sudo = np.array(self.data)
N = np.shape(sudo)[0]
REF = set(range(1,N+1))
for l in sudo:
if set(l) != REF:
return False
for l in sudo.T:
if set(l) != REF:
return False
n = int(np.sqrt(N))
for i in range(0,N,n):
for j in range(0,N,n):
square = sudo[i:i+n,j:j+n]
if set( square.reshape(-1) ) != REF:
return False
return True
答案10:
import math
class Sudoku(object):
def __init__(self, data):
self.data = data
def is_valid(self):
n, m = len(self.data), int(math.sqrt(len(self.data)))
if type(self.data[0][0]) == bool: return False
for i in self.data:
if set(i) != set(range(1,n+1)):
return False
for i in zip(*self.data):
if set(i) != set(range(1,n+1)):
return False
ls = [[list(zip(*i))[j:j+m] for j in range(0,n,m)] for i in [self.data[i:i+m] for i in range(0,n,m)] ]
for a in [i for sub in ls for i in sub]:
if set([i for sub in a for i in sub]) != set(range(1,n+1)):
return False
return True