题解 | #N皇后问题#
N皇后问题
http://www.nowcoder.com/practice/c76408782512486d91eea181107293b6
def queen(result, board, n, row=0): if row >= n: result.append(board[::]) 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 queen(result, board, n, row + 1) while True: try: n, res = int(input().strip()), [] queen(res, [0 for _ in range(n)], n) print(len(res)) except: break