Last active
August 29, 2015 14:07
-
-
Save huberflores/6e4e0894d95e16f1c25e to your computer and use it in GitHub Desktop.
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
package ee.ut.cs.mc.exercise; | |
public class NQueens { | |
static int[] x; | |
public NQueens(int N) { | |
x = new int[N]; | |
} | |
public boolean canPlaceQueen(int r, int c) { | |
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(); | |
} | |
/* | |
* This is the target to transform | |
*/ | |
public void placeNqueens(int r, int n) { | |
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 static void main(String args[]) { | |
long startTimeNano = System.nanoTime(); | |
NQueens Q = new NQueens(8); | |
Q.placeNqueens(0, x.length); | |
double taskTimeNano = (System.nanoTime() - startTimeNano) * 1.0e-6; | |
System.out.println(taskTimeNano); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment