1
+ '''
2
+ Aim: To place N queens in a N*N Chessboard such that no two queens
3
+ attack each other. A queen is said to be attacked by another queen
4
+ if they share same diagonal(right/left), Row or Column.
5
+ Intution: Since there could be only one queen in each row, we can assume the
6
+ N*N chessboard to be a 1d array which each index denotes one of the
7
+ row and the row value denotes the column. Now in each row, we will
8
+ put a queen and check whether it is possible or not. If possible, then
9
+ we recursively check for the next row. If its not possible to place
10
+ a queen in any of the column is a particular row, then we backtrack
11
+ and try next Column.
12
+
13
+ '''
14
+
15
+ # Main function argument =size of the board
16
+ def n_queens (board_size ):
17
+
18
+ # Occupied Diagonals and Columns
19
+ # For right and left Diagonal respectively
20
+ diagonal1 = {}
21
+ diagonal2 = {}
22
+ Col = {}
23
+
24
+ ans = place_queen (0 , [], board_size , diagonal1 , diagonal2 , Col )
25
+
26
+ return ans
27
+
28
+ # Recursive Function to check and place the queens
29
+ def place_queen (row , a , n , diagonal1 , diagonal2 , Col ):
30
+
31
+ # If the answer is found, row will be equal to the size of the board i.e. n
32
+ if (row == n ):
33
+ return a
34
+ R = row + 1
35
+
36
+ for C in range (1 , n + 1 ):
37
+ # Check that particular Column is free to place a queen or not
38
+ if ((C not in Col ) and ((R + C ) not in diagonal1 ) and ((R - C ) not in diagonal2 )):
39
+
40
+ # Add the Column and their respective Diagonals to the dictionary
41
+ # to mark they are Occupied
42
+ Col [C ] = 0
43
+ diagonal1 [R + C ] = 0
44
+ diagonal2 [R - C ] = 0
45
+ chk = place_queen (
46
+ row + 1 , a + [(row , C - 1 )], n , diagonal1 , diagonal2 , Col )
47
+
48
+ # If the answer is found, Stop the recursion
49
+ if chk :
50
+ return chk
51
+
52
+ # Deleaating the Column and Diagonals to vacant that place
53
+ del diagonal1 [R + C ]
54
+ del Col [C ]
55
+ del diagonal2 [R - C ]
56
+
57
+ return False
58
+
59
+
60
+ # -------------------------------Driver Code-------------------------------
61
+
62
+ if __name__ == "__main__" :
63
+ n = int (input ("Enter the Board Size: " ))
64
+ answer = n_queens (n )
65
+
66
+ if not answer :
67
+ print ("Queens cannot be placed in the given Chessboard" )
68
+
69
+ else :
70
+ print ("Queens are Placed in the chessboard" )
71
+ print ("Position :" , * answer )
72
+
73
+ '''
74
+ Sample Working:
75
+
76
+ Enter the Board Size: 3
77
+ Queens cannot be placed in the given Chessboard
78
+
79
+ Enter the Board Size: 4
80
+ Queens are Placed in the chessboard
81
+ Position : (0, 1) (1, 3) (2, 0) (3, 2)
82
+
83
+ 0 1 2 3
84
+ +-------+-------+-------+-------+
85
+ | | | | |
86
+ 0 | | X | | |
87
+ | | | | |
88
+ +---------------+-------+-------+
89
+ | | | | |
90
+ 1 | | | | X |
91
+ | | | | |
92
+ +-------+-------+-------+-------+
93
+ | | | | |
94
+ 2 | X | | | |
95
+ | | | | |
96
+ +-------+-------+-------+-------+
97
+ | | | | |
98
+ 3 | | | X | |
99
+ | | | | |
100
+ +-------+-------+-------+-------+
101
+
102
+ Enter the Board Size: 8
103
+ Queens are Placed in the chessboard
104
+ Position : (0, 0) (1, 4) (2, 7) (3, 5) (4, 2) (5, 6) (6, 1) (7, 3)
105
+
106
+ COMPLEXITY:
107
+
108
+ Time Complexity: O(2^N)
109
+ Space complexity: O(N)
110
+
111
+ '''
0 commit comments