AcWing 843. n-皇后问题
原题链接
中等
作者:
皓首不倦
,
2020-08-09 01:17:03
,
所有人可见
,
阅读 456
def dfs(cur, max_row, cols, col_mask):
if cur == max_row:
for i in range(max_row):
col = cols[i]
print(col*'.' + 'Q' + (max_row - col - 1) * '.')
print()
return
for col in range(max_row):
if (1 << col) & col_mask == 0:
flag = True
for i in range(cur):
if abs(i-cur) == abs(col -cols[i]):
flag = False
break
if flag:
cols.append(col)
dfs(cur+1, max_row, cols, col_mask | (1 << col))
cols.pop(-1)
n = int(input())
dfs(0, n, [], 0)