LeetCode.51 N皇后
自己摞的屎山,高不高
def check(position, chessBoard):
y90 = 0
for x in range(n):
y_45 = x - position[0] + position[1]
y45 = position[0] + position[1] - x
if 0 <= y45 < n: chessBoard[y45][x] = 1
if 0 <= y_45 < n: chessBoard[y_45][x] = 1
chessBoard[y90][position[0]] = 1
y90 += 1
return chessBoard
def backtrack(res, result, y, chessBoard):
if y == n:
res.append(list(result))
return
line = ['.'] * n
for x in range(n):
if chessBoard[y][x]: continue
line[x] = "Q"
result.append("".join(line))
backtrack(res, result, y+1, check([x, y], copy.deepcopy(chessBoard)))
result.pop()
line[x] = "."
res = []
backtrack(res, [], 0, [[0] * n for _ in range(n)])
return res