Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added NQueens in Java #1531

Merged
merged 4 commits into from
Dec 27, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Java/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ _add list here_
- [Suduko Solver](cp/SudukoSolver.java)
- [Ugly Number With Recursion](cp/UglyRecursion.java)
- [PDDI Using Recursion](cp/ArmStrongRecursion.java)
- [NQueens Problem](cp/NQueens.java)
- [Subarray of an array with given sum in O(n) time](cp/Subarray.java)


Expand Down
72 changes: 72 additions & 0 deletions Java/cp/NQueens.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
/**
N-Queens problem is a famous problem
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If possible, please add further information about the Algorithm

The paradigm used to solve the problem is backtracking
The problem is to find a way to place n queens on nXn board
such that no queen can kill the other
**/

import java.io.*;
import java.util.*;

public class NQueens {

public static void solve(boolean[][] board, boolean[] cols,
boolean[] ndiag, boolean[] rdiag, int row, String asf) {

if(row == board.length) {
System.out.println(asf+'.');
return;
}

for(int col=0; col<board.length; col++) {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you put apt comments on your Code for better explainability?

if(cols[col] == false && ndiag[row+col] == false &&
rdiag[row-col+board.length-1] == false) {
// place the queen
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you make the comments more understandable?

// let the column get occpied
// correspondingly ndiag and rdiag
// matrices are populated
cols[col] = true;
ndiag[row+col] = true;
rdiag[row-col+board.length-1] = true;
board[row][col] = true;
solve(board, cols, ndiag, rdiag, row+1, asf+row+'-'+col+", ");
// backtrack
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you make the comments more understandable?

// remove all the markings
// made in the above step
cols[col] = false;
ndiag[row+col] = false;
rdiag[row-col+board.length-1] = false;
board[row][col] = false;
}
}
}

public static void main(String[] args) throws Exception {
Scanner scn = new Scanner(System.in);
int n = scn.nextInt();
boolean[][] board = new boolean[n][n];

boolean[] cols = new boolean[n];
boolean[] ndiag = new boolean[2*n-1];
boolean[] rdiag = new boolean[2*n-1];

solve(board, cols, ndiag, rdiag, 0, "");
}

}

/**

Input :
4
Output :
0-1, 1-3, 2-0, 3-2, .
0-2, 1-0, 2-3, 3-1, .

Space Complexity : O(n^2)
Time Complexity : upperbounded by O(n^n)

**/