题解 | #n-皇后#
n-皇后
http://www.nowcoder.com/practice/c77ac599d0764433a5946610a7626768
class Solution: def solveNQueens(self, n): res = [] self.queen(res, [0 for _ in range(n)], n) return res def queen(self, result, board, n, row=0): if row >= n: plan = [] for row, col in enumerate(board[::]): plan.append("." * col + "Q" + "." * (n - 1 - col)) result.append(plan) else: for col in range(n): for i in range(row): x, y = i - row, board[i] - col if y == 0 or x == y or x == -y: break else: board[row] = col self.queen(result, board, n, row + 1)