1
+ /* C/C++ program to solve n Queen Problem using
2
+ backtracking */
3
+ #include <stdio.h>
4
+ #include <stdbool.h>
5
+ int n ;
6
+ /* A utility function to print solution */
7
+ void printSolution (int board [n ][n ])
8
+ {
9
+ static int k = 1 ;
10
+ printf ("%d-\n" ,k ++ );
11
+ for (int i = 0 ; i < n ; i ++ )
12
+ {
13
+ for (int j = 0 ; j < n ; j ++ )
14
+ printf (" %d " , board [i ][j ]);
15
+ printf ("\n" );
16
+ }
17
+ printf ("\n" );
18
+ }
19
+
20
+ /* A utility function to check if a queen can
21
+ be placed on board[row][col]. note that this
22
+ function is called when "col" queens are
23
+ already placed in columns from 0 to col -1.
24
+ So we need to check only left side for
25
+ attacking queens */
26
+ bool isSafe (int board [n ][n ], int row , int col )
27
+ {
28
+ int i , j ;
29
+
30
+ /* Check this row on left side */
31
+ for (i = 0 ; i < col ; i ++ )
32
+ if (board [row ][i ])
33
+ return false;
34
+
35
+ /* Check upper diagonal on left side */
36
+ for (i = row , j = col ; i >=0 && j >=0 ; i -- , j -- )
37
+ if (board [i ][j ])
38
+ return false;
39
+
40
+ /* Check lower diagonal on left side */
41
+ for (i = row , j = col ; j >=0 && i < n ; i ++ , j -- )
42
+ if (board [i ][j ])
43
+ return false;
44
+
45
+ return true;
46
+ }
47
+
48
+ /* A recursive utility function to solve n
49
+ Queen problem */
50
+ bool solvenQUtil (int board [n ][n ], int col )
51
+ {
52
+ /* base case: If all queens are placed
53
+ then return true */
54
+ if (col == n )
55
+ {
56
+ printSolution (board );
57
+ return true;
58
+ }
59
+
60
+ /* Consider this column and try placing
61
+ this queen in all rows one by one */
62
+ for (int i = 0 ; i < n ; i ++ )
63
+ {
64
+ /* Check if queen can be placed on
65
+ board[i][col] */
66
+ if ( isSafe (board , i , col ) )
67
+ {
68
+ /* Place this queen in board[i][col] */
69
+ board [i ][col ] = 1 ;
70
+
71
+ /* recur to place rest of the queens */
72
+ solvenQUtil (board , col + 1 ) ;
73
+
74
+ // below commented statement is replaced
75
+ // by above one
76
+ /* if ( solvenQUtil(board, col + 1) )
77
+ return true;*/
78
+
79
+ /* If placing queen in board[i][col]
80
+ doesn't lead to a solution, then
81
+ remove queen from board[i][col] */
82
+ board [i ][col ] = 0 ; // BACKTRACK
83
+ }
84
+ }
85
+
86
+ /* If queen can not be place in any row in
87
+ this column col then return false */
88
+ return false;
89
+ }
90
+
91
+ /* This function solves the n Queen problem using
92
+ Backtracking. It mainly uses solvenQUtil() to
93
+ solve the problem. It returns false if queens
94
+ canot be placed, otherwise return true and
95
+ prints placement of queens in the form of 1s.
96
+ Please note that there may be more than one
97
+ solutions, this function prints one of the
98
+ feasible solutions.*/
99
+ void solvenQ ()
100
+ {
101
+ int board [n ][n ];
102
+ for (int i = 0 ; i < n ; i ++ )
103
+ for (int j = 0 ; j < n ; ++ j )
104
+ {
105
+ board [i ] [j ] = 0 ;
106
+ }
107
+
108
+ if (solvenQUtil (board , 0 ))
109
+ {
110
+ printf ("Solution does not exist\n" );
111
+ return ;
112
+ }
113
+
114
+ return ;
115
+ }
116
+
117
+ // driver program to test above function
118
+ int main ()
119
+ {
120
+ printf ("Enter the size of the board, ie value of n:\n" );
121
+ scanf ("%d" ,& n );
122
+ solvenQ ();
123
+ return 0 ;
124
+ }
0 commit comments