From dcc53ee9e096a661de1418aac3881d381b81ac9a Mon Sep 17 00:00:00 2001 From: Abiha Fatima Date: Sun, 17 Oct 2021 22:34:36 +0400 Subject: [PATCH] chore(Java): add N Queen Problem (#570) --- algorithms/Java/README.md | 3 + algorithms/Java/backtracking/nqueen.java | 88 ++++++++++++++++++++++++ 2 files changed, 91 insertions(+) create mode 100644 algorithms/Java/backtracking/nqueen.java diff --git a/algorithms/Java/README.md b/algorithms/Java/README.md index b2b02385..412c9fa4 100644 --- a/algorithms/Java/README.md +++ b/algorithms/Java/README.md @@ -75,3 +75,6 @@ - [Left View of a Tree](trees/left-view.java) - [Right View of a Tree](trees/right-view.java) - [Zig-Zag Traversal of a Tree](trees/zig-zag-traversal.java) + +## Backtracking +- [N Queen Problem](backtracking/nqueen.java) diff --git a/algorithms/Java/backtracking/nqueen.java b/algorithms/Java/backtracking/nqueen.java new file mode 100644 index 00000000..7b638241 --- /dev/null +++ b/algorithms/Java/backtracking/nqueen.java @@ -0,0 +1,88 @@ +//The N Queen is the problem of placing N chess queens on an N×N chessboard so that no two queens attack each other. + +// Algorithm Type: Backtracking +// Time Complexity: O(n!) + +import java.util.*; +import java.util.Scanner; + +class nqueen +{ + //Array to store left diagonal elements to check if queen can be placed in left diagonal + static int []LeftDiagonal = new int[50]; + + //Array to store right diagonal elements to check if queen can be places in right diagonal + static int []RightDiagonal = new int[50]; + + //Array to store row-wise elements to check if queen can be placed in row + static int []cl = new int[50]; + + //Function to return true and print if feasible solution is obtained else return false + static boolean solveNQueen() + { + Scanner sc = new Scanner(System.in); + System.out.print("Enter the value of N for NxN chess board:\t"); + int n = sc.nextInt(); + int[][] chessBoard = new int[n][n]; + for(int i=0;i= N) //If all queens are placed, then return true + return true; + + for (int i = 0; i < N; i++) //placing queens in all rows of that particular column + { + //Check while placing a queen is not attacked by left and right diagonal elements + if ((LeftDiagonal[i - column + N - 1] != 1 && + RightDiagonal[i + column] != 1) && cl[i] != 1) + { //If the above condition is true then place the queen + chessBoard[i][column] = 1; + LeftDiagonal[i - column + N - 1] = + RightDiagonal[i + column] = cl[i] = 1; + + if (NQueen(chessBoard, column + 1,N)) + return true; + + //If placing this queen in chessBoard doesn't lead to a correct & safe position then remove queen from chessBoard + //going back through BACKTRACKING + chessBoard[i][column] = 0; + LeftDiagonal[i - column + N - 1] = + RightDiagonal[i + column] = cl[i] = 0; + } + } + return false; //If queen not placed in any row of this column then return false + } + + //Function to print chess board + static void printChessBoard(int chessBoard[][],int N) + { + System.out.printf("\n%d queens can be placed in the following order:\n",N); + for (int i = 0; i < N; i++) + { + for (int j = 0; j < N; j++) { + System.out.printf(" %d ", chessBoard[i][j]); + } + System.out.printf("\n"); + } + } + public static void main(String[] args) + { + nqueen obj = new nqueen(); + obj.solveNQueen(); + } +}