

Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!


import java.util.Arrays; public class Main { final int N = 8 ; void printSolution(char board[][]) { for (int i = 0 ; i < N; i++) { for (int j = 0 ; j < N; j++) System.out.print(" " + board[i][j]
for (i = row, j = col; i >= 0 && j >= 0 ; i--, j--) if (board[i][j] == 'Q') return false; for (i = row, j = col; j >= 0 && i < N; i++, j--) if (board[i][j] == 'Q') return false; return true; } boolean SolveNQUt(char board[][], int col) { if (col >= N) return true; for (int i = 0 ; i < N; i++) { if (isSafe(board, i, col)) { board[i][col] = 'Q'; if (SolveNQUt(board, col + 1 ) == true) return true; board[i][col] = ''; } } return false; } boolean solveNQ() { char board[][] = new char[N][N]; for (int i = 0 ; i < N; i++) { Arrays.fill(board[i], ''); } if (SolveNQUt(board, 0 ) == false) { System.out.print("Solution does not exist"); return false; } printSolution(board); return true; } public static void main(String args[])