-
Notifications
You must be signed in to change notification settings - Fork 1
/
QUEEN_PERMU_BOX_CHOOSE.PY
74 lines (52 loc) · 1.36 KB
/
QUEEN_PERMU_BOX_CHOOSE.PY
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# Queens Permutations - 2d As 2d - Queen Chooses
# 1. You are given a number n, representing the size of a n * n chess board.
# 2. You are required to calculate and print the permutations in which n queens can be placed on the
# n * n chess-board.
# Note -> Use the code snippet and follow the algorithm discussed in question video. The judge can't
# force you but the intention is to teach a concept. Play in spirit of the question.
# Sample Input
# 2
# Sample Output
# q1 q2
# - -
# q1 -
# q2 -
# q1 -
# - q2
# q2 q1
# - -
# - q1
# q2 -
# - q1
# - q2
# q2 -
# q1 -
# - q2
# q1 -
# - -
# q1 q2
# q2 -
# - q1
# - q2
# - q1
# - -
# q2 q1
def get_solution(chess,box,q_count):
if q_count==box:
if q_count == box:
for i in chess:
for j in i:
print(j+"\t",end="")
print()
print()
return
for i in range(len(chess)):
for j in range(len(chess[0])):
if chess[i][j]=="-":
chess[i][j] = "q" + str(q_count+1)
get_solution(chess,box,q_count+1)
chess[i][j] = "-"
if __name__ == '__main__':
box = 2
chess = [["-" for i in range(box)]for i in range(box)]
get_solution(chess,box,0)