@@ -65,6 +65,75 @@ public static void findPermutation(String str, String ans) { // TC -> O(n * n!)
6565 }
6666 }
6767
68+ public static void nQueens (char board [][], int row ) {
69+ if (row == board .length ) { // Base case
70+ System .out .println ("-------Chess Board-------" );
71+ printBoard (board );
72+ countWays ++;
73+ return ;
74+ }
75+ //column loop
76+ for (int j = 0 ; j < board .length ; j ++) {
77+ if (isSafeQueen (board , row , j )) { // safe hai ya nhi call isSafe
78+ board [row ][j ] = 'Q' ;
79+ nQueens (board , row + 1 ); // function call
80+ board [row ][j ] = 'X' ; // backtracking
81+ }
82+ }
83+ }
84+
85+ public static boolean isSafeQueen (char board [][], int row , int col ) {
86+ // vertical up
87+ for (int i = row - 1 ; i >= 0 ; i --) {
88+ if (board [i ][col ] == 'Q' ) {
89+ return false ;
90+ }
91+ }
92+ // diag left up
93+ for (int i = row - 1 , j = col - 1 ; i >= 0 && j >= 0 ; i --, j --) {
94+ if (board [i ][j ] == 'Q' ) {
95+ return false ;
96+ }
97+ }
98+ // diag right up
99+ for (int i = row - 1 , j = col + 1 ; i >= 0 && j < board .length ; i --, j ++) {
100+ if (board [i ][j ] == 'Q' ) {
101+ return false ;
102+ }
103+ }
104+ return true ;
105+ }
106+
107+ public static void printBoard (char board [][]) {
108+ for (int i = 0 ; i < board .length ; i ++) {
109+ for (int j = 0 ; j < board .length ; j ++) {
110+ System .out .print (board [i ][j ] + " " );
111+ }
112+ System .out .println ();
113+ }
114+ }
115+
116+ static int countWays = 0 ;
117+
118+ public static boolean nQueensSol1 (char board [][], int row ) {
119+ if (row == board .length ) { // Base case
120+ // printBoard(board);
121+ // countWays++;
122+ return true ;
123+ }
124+ //column loop
125+ for (int j = 0 ; j < board .length ; j ++) {
126+ if (isSafeQueen (board , row , j )) { // safe hai ya nhi call isSafe
127+ board [row ][j ] = 'Q' ;
128+ if (nQueensSol1 (board , row + 1 )) {
129+ return true ;
130+ }
131+ board [row ][j ] = 'X' ; // backtracking
132+ }
133+ }
134+ return false ;
135+ }
136+
68137 public static void main (String [] args ) {
69138 // Type of Backtracking --------------------------------
70139 // 1. Decision
@@ -90,5 +159,30 @@ public static void main(String[] args) {
90159 findPermutation (str2 , "" );
91160 System .out .println ();
92161
162+ // N Queens --------------------------------
163+ // Place N Queens on an N x N chess board such that no 2 queens can attack each other. ---
164+ int n = 4 ;
165+ char board [][] = new char [n ][n ];
166+ for (int i = 0 ; i < n ; i ++) {
167+ for (int j = 0 ; j < n ; j ++) {
168+ board [i ][j ] = 'X' ;
169+ }
170+ }
171+ nQueens (board , 0 );
172+ System .out .println ();
173+
174+ // N Queens count ways --------------------------------
175+ // Count total number of ways in which we can solve N Queens problems. ---
176+ System .out .println ("Total ways to solve n queens = " + countWays );
177+
178+ // N Queens print one solution --------------------------------
179+ // Check if problem can be solved & print only 1 solution to N Queens. ---
180+ if (nQueensSol1 (board , 0 )) {
181+ System .out .println ("Solution is possible" );
182+ printBoard (board );
183+ } else {
184+ System .out .println ("Solution is not possible" );
185+ }
186+
93187 }
94188}
0 commit comments