"""
Ver: 1.0
Date: 2020年8月16日08:59:02
Auther: CCandle
"""
####### definition of arrays ######
N = 8
map = []
for i in range(0, N+1):
line = []
for j in range(0, N+1):
line.append(False)
map.append(line)
# create a N*N map filles with False
# to store the queens' positions
row = []
x_row = []
y_row = []
for i in range(0, N+1):
row.append(False)
for i in range(0, N*2 + 1):
x_row.append(False)
y_row.append(False)
# to mark which row/x_row/y_row is occupied
###### definitions of functions ########
def dfs(deep):
global row
global x_row
global y_row
if(deep >= N):
print_map()
return
for i in range(0, N+1):
if row[i] == False and \
x_row[i+deep] == False \
and y_row[i-deep+N] == False :
row[i] = True
x_row[i] = True
y_row[i] = True
map[deep][i] = True
dfs(deep+1)
row[i] = False
x_row[i] = False
y_row[i] = False
map[deep][i] = False
return
def print_map():
for i in range(0, N+1):
for j in range(0, N+1):
if map[i][j]:
print(' ', end='*')
else:
print(' ', end=' ')
print()
########## call the function ###########
dfs(0)
|