Last active
October 31, 2016 23:57
-
-
Save ajaysjournal/1dfe103ac4486459f029cb2be95db43f to your computer and use it in GitHub Desktop.
NQueens
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* To change this template, choose Tools | Templates | |
* and open the template in the editor. | |
*/ | |
package csp; | |
/** | |
* | |
* @author ACHCHUTHAN | |
*/ | |
public class Queens { | |
int[] x; | |
public Queens(int N) { | |
x = new int[N]; | |
} | |
public boolean canPlaceQueen(int r, int c) { | |
/** | |
* Returns TRUE if a queen can be placed in row r and column c. | |
* Otherwise it returns FALSE. x[] is a global array whose first (r-1) | |
* values have been set. | |
*/ | |
for (int i = 0; i < r; i++) { | |
if (x[i] == c || (i - r) == (x[i] - c) ||(i - r) == (c - x[i])) | |
{ | |
return false; | |
} | |
} | |
return true; | |
} | |
public void printQueens(int[] x) { | |
int N = x.length; | |
for (int i = 0; i < N; i++) { | |
for (int j = 0; j < N; j++) { | |
if (x[i] == j) { | |
System.out.print("Q "); | |
} else { | |
System.out.print("* "); | |
} | |
} | |
System.out.println(); | |
} | |
System.out.println(); | |
} | |
public void placeNqueens(int r, int n) { | |
/** | |
* Using backtracking this method prints all possible placements of n | |
* queens on an n x n chessboard so that they are non-attacking. | |
*/ | |
for (int c = 0; c < n; c++) { | |
if (canPlaceQueen(r, c)) { | |
x[r] = c; | |
if (r == n - 1) { | |
printQueens(x); | |
} else { | |
placeNqueens(r + 1, n); | |
} | |
} | |
} | |
} | |
public void callplaceNqueens() { | |
placeNqueens(0, x.length); | |
} | |
public static void main(String args[]) { | |
Queens Q = new Queens(8); | |
Q.callplaceNqueens(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Reference - http://www.java.achchuthan.org/2012/02/n-queens-problem-in-java.html