3 questions in three days (I wish I could fix my problems alone) and today is still about my Sudoku project. I'm working on building a Sudoku game in Java and am working with other people who have programmed various parts of the project and right now we are trying to join together the Sudoku solver class with the JFrame class which makes it all pretty (Or it will at one point) but right now I'm having an issue with the way my 2D arrays are working (more like aren't). Below is the code where I call the solver and feed it into a lot of methods to do things:
public class FenetreGrille extends JFrame implements ActionListener{
public static int [][] NOMBRES_DEBUT;
public static int [][] GRILLE_MODIF = new int[9][9];
public int [][] GRILLE_FINALE = new int[9][9];
public static final int TAILLE = 9;
public static int TAILLECASE = 60;
public static int COTEGRILLE ;
public FenetreGrille(int [][] t){
NOMBRES_DEBUT = t;
GRILLE_MODIF = NOMBRES_DEBUT;
GRILLE_FINALE = NOMBRES_DEBUT;
COTEGRILLE = TAILLE * TAILLECASE;
SudokuBackTrack sbt = new SudokuBackTrack(GRILLE_FINALE);
// Here is the problem ^^^^^
// More code below that shouldn't be important...
}
And here is the SudokuBackTrack class:
public class SudokuBackTrack{
public static int[][] grille;
public static int[][] grilleResolu;
public static int[][] grillePos;
public static boolean[][] existeSurLigne = new boolean[9][9];
public static boolean[][] existeSurColonne = new boolean[9][9];
public static boolean[][] existeSurBloc = new boolean[9][9];
public final static int taille = 9;
public static ArrayList<Case> valParCase;
public SudokuBackTrack(int[][] t) {
grille = t;
grilleResolu = grille;
valParCase = listeValPossibles(grilleResolu);
tableauxExistence(grilleResolu);
backtracking(0, grilleResolu);
}
public static ArrayList<Case> listeValPossibles(int[][] temp) {
ArrayList<Case> t = new ArrayList<Case>();
for (int i=0; i<9; i++){
for (int j=0; j<9; j++){
if(temp[i][j] == 0) {
int pos = i*taille+j;
t.add(valeursPossibles(pos, temp));
}
}
}
Collections.sort(t);
return t;
}
public static Case valeursPossibles(int pos, int[][] t) {
int i = pos/9;
int j = pos%9;
int valPossibles = 9;
for(int s=1; s<=9; s++) {
if(!absentSurLigne(s, i, t) || !absentSurColonne(s, j, t) || !absentDansBloc(s, i, j, t)) {
valPossibles--;
}
}
Case a = new Case(pos, valPossibles);
return a;
}
public static boolean absentSurLigne(int k, int i, int[][] t) {
for (int j=0; j < 9; j++) {
if (t[i][j] == k) {
return false;
}
}
return true;
}
public static boolean absentSurColonne(int k, int j, int[][] t) {
for (int i=0; i < 9; i++) {
if (t[i][j] == k) {
return false;
}
}
return true;
}
public static boolean absentDansBloc(int k, int i, int j, int[][] t) {
int _i = i-(i%3); // ou encore : _i = 3*(i/3);
int _j = j-(j%3); // ou encore : _j = 3*(j/3);
for (i=_i; i < _i+3; i++) {
for (j=_j; j < _j+3; j++) {
if (t[i][j] == k) {
return false;
}
}
}
return true;
}
public static void tableauxExistence(int[][] t) {
for (int i=0; i < 9; i++) {
for (int j=0; j < 9; j++) {
existeSurLigne[i][j] = existeSurColonne[i][j] = existeSurBloc[i][j] = false;
}
}
int k;
for (int i=0; i < 9; i++) {
for (int j=0; j < 9; j++) {
if ((k = t[i][j]) != 0) {
existeSurLigne[i][k-1] = existeSurColonne[j][k-1] = existeSurBloc[3*(i/3)+(j/3)][k-1] = true;
}
}
}
}
public static boolean backtracking(int index, int[][] t) {
if(index == valParCase.size()) {
return true;
}
int i = (valParCase.get(index).position)/9;
int j = (valParCase.get(index).position)%9;
for(int k = 0; k < 9; k++) {
if(!existeSurLigne[i][k] && !existeSurColonne[j][k] && !existeSurBloc[3*(i/3)+(j/3)][k]){
// Ajoute k aux valeurs enregistrées
existeSurLigne[i][k] = existeSurColonne[j][k] = existeSurBloc[3*(i/3)+(j/3)][k] = true;
if(backtracking(index+1, t)){
// Ecrit le choix valide dans la grille
t[i][j] = k+1;
return true;
}
// Supprime k des valeurs enregistrées
existeSurLigne[i][k] = existeSurColonne[j][k] = existeSurBloc[3*(i/3)+(j/3)][k] = false;
}
}
t[i][j] = 0;
return false;
}
As mentioned above, my problem is that when I create an instance of my SudokuBackTrack class using the 2D Array GRILLE_FINALE, all my 2D Arrays NOMBRES_DEBUT, GRILLE_MODIF and GRILLE_FINALE become solved Sudoku grids whereas all I want is for GRILLE_FINALE to become the solved version, not all 3. I've tried debugging the codes but I haven't found anything and since it's a mixture of codes from different people, I don't know how they each created their parts. I've modified the attribute types and tried all sorts of fancy things but non worked and I'm out of ideas and mainly time... Thanks in advance and sorry for the huge question and code.
You're placing the same instance of the 2D array into NOMBRES_DEBUT, GRILLE_MODIF, GRILLE_FINALE. I think what you're looking to do is to place a copy NOMBRES_DEBUT into GRILLE_MODIF and GRILLE_FINALE.
They way you have it set up now, whever you make a modification to any of the 3 arrays, you will end up modifying all 3 because all 3 point to the same array in memory.
Related
public class Matrix {
public static int a = 0;
public static int b = 0;
public double myArray[][];
public Matrix(double a[][]) {
this.myArray = a;
}
public Matrix(int b, Vector... vectors) {
double myArray[][] = new double[vectors.length][];
int row = vectors.length;
Matrix.a = row;
int column = vectors[0].getYourArray().length;
Matrix.b = column;
for (int i = 0; i < row; i++) {
myArray[i] = new double[row];
}
for (int i = 0; i < row; i++) {
for (int j = 0; j < column; j++) {
if (b == 0) {
double[] a = vectors[i].getYourArray();
myArray[i][j] = a[j];
} else {
myArray[j][i] = vectors[i].getYourArray()[j];
}
}
}
}
public class Vector {
double yourArray[];
public double[] getYourArray() {
return yourArray;
}
public void setYourArray(double[] yourArray) {
this.yourArray = yourArray;
}
public Vector(double... yourArray) {
this.yourArray = yourArray;
}
}
}
I create 2 vectors array and send them into vector class to make an array which includes these two vectors parameters and then send to matrix class to create matrices with dimension of vector arrays
The problem is that how can i determine the rows and columns of new matrix?
I cannot write a proper code guys please help me
As you see it in your code:
1-th dimension: vectors.length
2-th dimension: vectors[0].getYourArray().length
Normally you also have to ensure, that for all vectors[0].getYourArray() the length is the same.
So I'm creating many byte[] that I would like to be placed in a matrix, eg. 3x3, so 9 byte[] which I can then by using the methods below rotate them accordingly.
// ARRAY LIST
private static void transpose(ArrayList<byte[]> m) {
for (int i = 0; i < m.size(); i++) {
for (int j = i; j < m.get(0).length; j++) {
byte x = m.get(i)[j];
m.get(i)[j] = m.get(j)[i];
m.get(j)[i] = x;
}
}
}
public static void swapRows(ArrayList<byte[]> m) {
for (int i = 0, k = m.size() - 1; i < k; ++i, --k) {
byte[] x = m.get(i);
m.set(i, m.get(k));
m.set(k, x);
}
}
public static void rotateByNinetyToLeft(ArrayList<byte[]> m) {
transpose(m);
swapRows(m);
}
public static void rotateByNinetyToRight(ArrayList<byte[]> m) {
swapRows(m);
transpose(m);
}
When I call the inserts method I want to add to the array in the correct position. So from 0,0 then 0,1 then 1,1 .... 3,3. So I created the code to do that with..
public void inserts(byte[] s){
if(x ==y){
buffer.get(x)[y]= s;
System.out.println(y);
y++;
}
else{
buffer.get(x)[y]= s;
System.out.println(x);
x++;
}
counter++;
}
But It won't allow me to execute the insertion properly. Unsure as to what is the problem.
Kind of feel like i'm making a very blatant mistake, any help would be great
thank you
EDIT:
code for array of arrays:
private static void transposeb(byte[][] m) {
for (int i = 0; i < m.length; i++) {
for (int j = i; j < m[0].length; j++) {
byte x = m[i][j];
m[i][j] = m[j][i];
m[j][i] = x;
}
}
}
public static void swapRowsb(byte[][] m) {
for (int i = 0, k = m.length - 1; i < k; ++i, --k) {
byte[] x = m[i];
m[i] = m[k];
m[k] = x;
}
}
public static void rotateByNinetyToLeftb(byte[][] m) {
transposeb(m);
swapRowsb(m);
}
public static void rotateByNinetyToRightb(byte[][] m) {
swapRowsb(m);
transposeb(m);
}
and insertion
private byte[][] buffer;
private int x=0;
private int y=0;
public FixedBuffer(int BUFF_SIZE) {
this.BUFF_SIZE = BUFF_SIZE;
buffer = new byte[BUFF_SIZE][BUFF_SIZE];
}
public void inserts(byte[] s){
if(x ==y){
buffer.get(x)[y]= s;
System.out.println(y);
y++;
}
else{
buffer.get(x)[y]= s;
System.out.println(x);
x++;
}
counter++;
}
Say we have a vector of 3 x 3
I want to use inserts() to add all the byte[], there will be 9 in total. so 9 byte[], each time I add one the index's (x and y) change.
Structure:
byte[], byte[], byte[]
byte[], byte[], byte[]
byte[], byte[], byte[]
Some test code first.
ArrayList<byte[]> arr = new ArrayList<byte[]>(9);
initialize(arr);
printRows(arr);
arr = transpose(arr);
printRows(arr);
Initialize ArrayList with random test data
private static void initialize(ArrayList<byte[]> arr) {
for(byte i = 0 ; i < 9 ; i++) {
byte[] a = {i,i,i,i};
arr.add(a);
}
}
A way to find out if all the ops are working as required.
private static void printRows(ArrayList<byte[]> arr) {
int row = 0;
for(int i = 0 ; i < arr.size() ; i++) {
System.out.print(Arrays.toString(arr.get(i)) + " ");
row++;
if(row%3 == 0)
System.out.println();
}
System.out.println("\n\n");
}
The method to transpose. Took the hacky way of making a new ArrayList. Please change that. This is just to demonstrate the logic.
private static ArrayList<byte[]> transpose(ArrayList<byte[]> arr) {
ArrayList<byte[]> newArr = new ArrayList<byte[]>(9);
// fill with null
for(int i = 0 ; i < 9 ; i++)
newArr.add(null);
for(int row = 0; row < 3 ; row++) {
for(int col = 0 ; col < 3 ; col++) {
// diagonal
if(row == col) {
newArr.add(row + 3*col, arr.get(row + 3*col));
}
int second = row + 3*col; // elem at (row,col)
int first = 3*row + col; // elem at (col,row)
// swap
newArr.set(first, arr.get(second));
newArr.set(second, arr.get(first));
}
}
return newArr;
}
I am working on Conway's Game of Life for a school project. I am not looking for the code directly. I am looking to find out what is wrong with my code.
In Conway's Game of Life a cell goes from dead to alive if it has 3 alive neighbors. It stays alive if it has two or three alive neighbors. If none of those are true it is dead.
My LifeView class has a method that displays the cell simulation and afterwards displays how many alive cells are around the given point.
This is the output I am getting:
How many rows is your simulation?
5
How many columns is your simulation?
5
How many generations is your simulation?
3
xxxxx
xxxxx
xx0xx
xx0xx
xx0xx
00000
01110
02120
03230
02120
xxxxx
xxxxx
xxxxx
xxxxx
xxxxx
00000
00000
00000
00000
00000
xxxxx
xxxxx
xxxxx
xxxxx
xxxxx
00000
00000
00000
00000
00000
This is wrong because the second generation is supposed to be a horizontal line of live cells crossing the center of the first generation alive cells. Instead of crossing that center, all cells are turned dead. I am stumped as to why it doesn't work.
Main class:
package gameOfLife;
import java.util.Scanner;
public class Main {
/**
* #param args the command line arguments
*/
public static void main(String[] args)
{
Scanner numReader = new Scanner(System.in);
System.out.println("How many rows is your simulation?");
int rows = numReader.nextInt();
System.out.println("How many columns is your simulation?");
int columns = numReader.nextInt();
System.out.println("How many generations is your simulation?");
int generations = numReader.nextInt();
LifeModel model = new LifeModel(rows,columns);
LifeView life = new LifeView(model);
for(int i=0; i<generations; i++)
{
life.displayLife();
model.nextGeneration();
}
}
LifeView class:
package gameOfLife;
import java.util.Scanner;
public class LifeView {
private LifeModel model;
public LifeView(LifeModel model)
{
this.model = model;
}
public void displayLife()
{
for(int i=0; i < model.getWorld().length; i++)
{
for(int j=0; j < model.getWorld()[0].length; j++)
{
if(model.getWorld()[i][j])
{
System.out.print("0");
}
else
{
System.out.print("x");
}
}
System.out.println("");
}
System.out.println("");
for(int i=0; i < model.getWorld().length; i++)
{
for(int j=0; j < model.getWorld()[0].length; j++)
{
System.out.print(model.numLivingNeighbors(i,j));
}
System.out.println("");
}
System.out.println("");
System.out.println("");
}
}
LifeModel class:
package gameOfLife;
public class LifeModel
{
private boolean[][] world;
private int numRows;
private int numCols;
private boolean[][] tempWorld;
public LifeModel(int rows, int cols)
{
this.numRows=rows;
this.numCols=cols;
world = new boolean[rows][cols];
initWorld();
tempWorld = world;
}
private void initWorld()
{
boolean done = false;
while(!done)
{
int i = (int) (Math.random()*numRows);
int j = (int) (Math.random()*numCols);
if(j>0 && i>0 && i<numRows-1 && j<numCols-1)
{
/*
world[i-1][j-1] = true;
world[i-1][j] = true;
world[i-1][j+1] = true;
world[i][j+1] = true;
world[i+1][j] = true;
*/
world[i][j]=true;
world[i+1][j]=true;
world[i-1][j]=true;
done = true;
}
}
}
public void nextGeneration()
{
//tempWorld = new boolean[numRows+2][numCols+2];
int rows = world.length;
int columns = world[0].length;
for(int i=0; i < rows; i++)
{
for(int j = 0; j < columns; j++)
{
toggleCell(i,j);
}
}
world = tempWorld;
}
public void toggleCell(int r, int c)
{
int count = numLivingNeighbors(r,c);
if(!world[r][c] && count==3)
{
tempWorld[r][c] = true;
}
else if(world[r][c] && (count>=2 && count<=3))
{
tempWorld[r][c] = true;
}
else
{
tempWorld[r][c] = false;
}
}
public int numLivingNeighbors(int r, int c)
{
int count = 0;
boolean newCells[][] = world;
for(int i = -1; i<=1; i++)
{
for(int j = -1; j<=1; j++)
{
if(i!=0 || j!=0)
{
int row = r + i;
int column = c + j;
if(row>=0 && row < newCells.length && column>=0 && column<newCells[0].length && newCells[row][column])
{
count++;
}
}
}
}
return count;
}
public void userChange()
{
}
public boolean[][] getWorld()
{
return world;
}
}
Any help is GREATLY appreciated!
You just have a couple small issues with your LifeModel class.
In your constructor you set the tempWorld to reference the same array as the actual game world. This will cause any modifications to tempWorld to also affect the gameWorld.
public LifeModel(int rows, int cols)
{
this.numRows=rows;
this.numCols=cols;
world = new boolean[rows][cols];
initWorld();
//tempWorld = world; // You can remove this line.
}
Then in next generation you have the line "//tempWorld = new boolean[numRows+2][numCols+2];" commented out. You really do need to create a new temp array here so you aren't changing the game board as you read it. However, I'm not sure what the +2 is supposed to be, so I removed it. You should have:
public void nextGeneration()
{
tempWorld = new boolean[numRows][numCols]; // Keep it the same size
int rows = world.length;
int columns = world[0].length;
for(int i=0; i < rows; i++)
{
for(int j = 0; j < columns; j++)
{
toggleCell(i,j);
}
}
world = tempWorld;
}
After I made those changes it worked perfectly for me. I've included the full LifeModel class below that I used on my machine.
package gameOfLife;
public class LifeModel
{
private boolean[][] world;
private int numRows;
private int numCols;
private boolean[][] tempWorld;
public LifeModel(int rows, int cols)
{
this.numRows=rows;
this.numCols=cols;
world = new boolean[rows][cols];
initWorld();
}
private void initWorld()
{
boolean done = false;
while(!done)
{
int i = (int) (Math.random()*numRows);
int j = (int) (Math.random()*numCols);
if(j>0 && i>0 && i<numRows-1 && j<numCols-1)
{
/*
world[i-1][j-1] = true;
world[i-1][j] = true;
world[i-1][j+1] = true;
world[i][j+1] = true;
world[i+1][j] = true;
*/
world[i][j]=true;
world[i+1][j]=true;
world[i-1][j]=true;
done = true;
}
}
}
public void nextGeneration()
{
tempWorld = new boolean[numRows][numCols];
int rows = world.length;
int columns = world[0].length;
for(int i=0; i < rows; i++)
{
for(int j = 0; j < columns; j++)
{
toggleCell(i,j);
}
}
world = tempWorld;
}
public void toggleCell(int r, int c)
{
int count = numLivingNeighbors(r,c);
if(!world[r][c] && count==3)
{
tempWorld[r][c] = true;
}
else if(world[r][c] && (count>=2 && count<=3))
{
tempWorld[r][c] = true;
}
else
{
tempWorld[r][c] = false;
}
}
public int numLivingNeighbors(int r, int c)
{
int count = 0;
boolean newCells[][] = world;
for(int i = -1; i<=1; i++)
{
for(int j = -1; j<=1; j++)
{
if(i!=0 || j!=0)
{
int row = r + i;
int column = c + j;
if(row>=0 && row < newCells.length && column>=0 && column<newCells[0].length && newCells[row][column])
{
count++;
}
}
}
}
return count;
}
public void userChange()
{
}
public boolean[][] getWorld()
{
return world;
}
}
Check that numLivingNeighbors is returning the proper value for each cell in the world.
Also check the step for staying alive
Hey You have done a simple mistake in your code
public LifeModel(int rows, int cols)
{
this.numRows=rows;
this.numCols=cols;
world = new boolean[rows][cols];
initWorld();
tempWorld = world;
}
this is LifeModel constructor.
In this constructor you need to initialize tempworld also. You should not assign your world to tempworld.
After modification this block of code will become like this....
public LifeModel(int rows, int cols)
{
this.numRows=rows;
this.numCols=cols;
world = new boolean[rows][cols];
tempWorld = new boolean[rows][cols];
initWorld();
}
After this your output will be correct.
I can't see to get a double boolean array to pass through to the another activity. I use putExtra and when I retrieve it and cast it to boolean[][], it states that it can not cast and crashes. Boolean[] works however.
How would I go about passing a boolean[][] between activities?
If you absolutely need a boolean[][] (and can't do this with just a flat boolean[] array passed to Parcel.writeBooleanArray()), then the formal way to do this is to wrap it in a Parcelable class and do the marshalling/unmarshalling there.
I'll sketch out the code, though this is not tested so there are certainly to be some issues.
public class BooleanArrayArray implements Parcelable {
private final boolean[][] mArray;
public BooleanArrayArray(boolean[][] array) {
mArray = array;
}
private BooleanArrayArray(Parcelable in) {
boolean[][] array;
final int N = in.readInt();
array = new boolean[][N];
for (int i=0; i<N; i++) {
array[i] = in.createBooleanArray();
}
mArray = array;
}
#Override
public int describeContents() {
return 0;
}
#Override
public void writeToParcel(Parcel out, int flags) {
final int N = mArray.length;
out.writeInt(N);
for (int i=0; i<N; i++) {
out.writeBooleanArray(mArray[i]);
}
}
public static final Parcelable.Creator<BooleanArrayArray> CREATOR
= new Parcelable.Creator<BooleanArrayArray>() {
public BooleanArrayArraycreateFromParcel(Parcel in) {
return new BooleanArrayArray(in);
}
public BooleanArrayArray[] newArray(int size) {
return new BooleanArrayArray[size];
}
};
}
If you really require a 2-dimensional array, you can easily convert a 2-dimensional array into a single dimensional array for passing between Activities like so:
public boolean[] toOneDimension(boolean[][] input){
boolean[] output = new boolean[input.length * input[0].length];
for(int i = 0; i < input.length; i++){
for(int j = 0; j < input[i].length; j++){
output[i*j] = input[i][j];
}
}
return output;
}
which you can then build back into a 2-dimensional array like so:
public boolean[][] toTwoDimensions(int dimensions, boolean[] input){
boolean[][] output = new boolean[input.length / dimensions][dimensions];
for(int i = 0; i < input.length; i++){
output[i/dimensions][i % dimensions] = input[i];
}
return output;
}
then use like so:
public static void main(String[] args){
int size = 10;
Random rand = new Random();
Tester tester = new Tester(); //example code holder
boolean[][] value = new boolean[size+1][size];
for(int i = 0; i < size+1; i++){
for(int j = 0; j < size; j++){
value[i][j] = rand.nextBoolean();
}
}
boolean [][] output = tester.toTwoDimensions(size, tester.toOneDimension(value));
for(int i = 0; i < size+1; i++){
for(int j = 0; j < size; j++){
assert value[i][j] == output[i][j];
}
}
}
The only requirement is that you need to know the dimension of your array before you flattened it.
This is old, but helped me a lot, so I wanted to share my findings.
I used Parcelabler to make my Object Class Parcelable(Because everything I read, was written in martian to me), then I used #John Ericksen answer to implement it in my Object Class and some methods to make my life easier flattenMultiDimensionalArray and restoreMultiDimensionalArray and the final result.
For a 2 Dimensional Array
MultiDimensionalArray .java
public class MultiDimensionalArray implements Parcelable {
private int[][] multiDimensionalArray;
//Any other Variables, Objects
public MultiDimensionalArray() {
}
public MultiDimensionalArray(int[][] multiDimensionalArray) {
this.multiDimensionalArray = multiDimensionalArray;
//Any other Variables, Objects
}
public int[][] getMultiDimensionalArray() {
return multiDimensionalArray;
}
public void setMultiDimensionalArray(int[][] multiDimensionalArray) {
this.multiDimensionalArray = multiDimensionalArray;
}
protected MultiDimensionalArray(Parcel in) {
int rows = in.readInt();
int columns = in.readInt();
int[] transitionalArray = in.createIntArray();
multiDimensionalArray = restoreMultiDimensionalArray(transitionalArray, rows, columns);
//Any other Variables, Objects
}
#Override
public int describeContents() {
return 0;
}
#Override
public void writeToParcel(Parcel dest, int flags) {
int rows = multiDimensionalArray.length;
int columns = multiDimensionalArray[rows - 1].length;
int[] transitionalArray = flattenMultiDimensionalArray(multiDimensionalArray);
dest.writeInt(rows);
dest.writeInt(columns);
dest.writeIntArray(transitionalArray);
//Any other Variables, Objects
}
public static final Creator<MultiDimensionalArray> CREATOR = new Creator<MultiDimensionalArray>() {
#Override
public MultiDimensionalArray createFromParcel(Parcel in) {
return new MultiDimensionalArray(in);
}
#Override
public MultiDimensionalArray[] newArray(int size) {
return new MultiDimensionalArray[size];
}
};
private int[] flattenMultiDimensionalArray(int[][] sourceCard) {
int k = 0;
int[] targetCard = new int[sourceCard.length * sourceCard[sourceCard.length - 1].length];
for (int[] aSourceCard : sourceCard) {
for (int anASourceCard : aSourceCard) {
targetCard[k] = anASourceCard;
k++;
}
}
return targetCard;
}
private int[][] restoreMultiDimensionalArray(int[] sourceCard, int rows, int columns) {
int k = 0;
int[][] multiDimensionalArray = new int[rows][columns];
for (int i = 0; i < multiDimensionalArray.length; i++) {
for (int j = 0; j < multiDimensionalArray[multiDimensionalArray.length - 1].length; j++) {
multiDimensionalArray[i][j] = sourceCard[k];
k++;
}
}
return multiDimensionalArray;
}
}
I'm trying to solve the problem of positioning N queens on NxN board without row, column and diagonal conflicts. I use an algorithm with minimizing the conflicts. Firstly, on each column randomly a queen is positioned. After that, of all conflict queens randomly one is chosen and for her column are calculated the conflicts of each possible position. Then, the queen moves to the best position with min number of conflicts. It works, but it runs extremely slow. My goal is to make it run fast for 10000 queens. Would you, please, suggest me some improvements or maybe notice some mistakes in my logic?
Here is my code:
public class Queen {
int column;
int row;
int d1;
int d2;
public Queen(int column, int row, int d1, int d2) {
super();
this.column = column;
this.row = row;
this.d1 = d1;
this.d2 = d2;
}
#Override
public String toString() {
return "Queen [column=" + column + ", row=" + row + ", d1=" + d1
+ ", d2=" + d2 + "]";
}
#Override
public boolean equals(Object obj) {
return ((Queen)obj).column == this.column && ((Queen)obj).row == this.row;
}
}
And:
import java.util.HashSet;
import java.util.Random;
public class SolveQueens {
public static boolean printBoard = false;
public static int N = 100;
public static int maxSteps = 2000000;
public static int[] queens = new int[N];
public static Random random = new Random();
public static HashSet<Queen> q = new HashSet<Queen>();
public static HashSet rowConfl[] = new HashSet[N];
public static HashSet d1Confl[] = new HashSet[2*N - 1];
public static HashSet d2Confl[] = new HashSet[2*N - 1];
public static void init () {
int r;
rowConfl = new HashSet[N];
d1Confl = new HashSet[2*N - 1];
d2Confl = new HashSet[2*N - 1];
for (int i = 0; i < N; i++) {
r = random.nextInt(N);
queens[i] = r;
Queen k = new Queen(i, r, i + r, N - 1 + i - r);
q.add(k);
if (rowConfl[k.row] == null) {
rowConfl[k.row] = new HashSet<Queen>();
}
if (d1Confl[k.d1] == null) {
d1Confl[k.d1] = new HashSet<Queen>();
}
if (d2Confl[k.d2] == null) {
d2Confl[k.d2] = new HashSet<Queen>();
}
((HashSet<Queen>)rowConfl[k.row]).add(k);
((HashSet<Queen>)d1Confl[k.d1]).add(k);
((HashSet<Queen>)d2Confl[k.d2]).add(k);
}
}
public static void print () {
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
System.out.print(queens[i] == j ? "♕ " : "◻◻◻ ");
}
System.out.println();
}
System.out.println();
}
public static boolean checkItLinear() {
Queen r = choseConflictQueen();
if (r == null) {
return true;
}
Queen newQ = findNewBestPosition(r);
q.remove(r);
q.add(newQ);
rowConfl[r.row].remove(r);
d1Confl[r.d1].remove(r);
d2Confl[r.d2].remove(r);
if (rowConfl[newQ.row] == null) {
rowConfl[newQ.row] = new HashSet<Queen>();
}
if (d1Confl[newQ.d1] == null) {
d1Confl[newQ.d1] = new HashSet<Queen>();
}
if (d2Confl[newQ.d2] == null) {
d2Confl[newQ.d2] = new HashSet<Queen>();
}
((HashSet<Queen>)rowConfl[newQ.row]).add(newQ);
((HashSet<Queen>)d1Confl[newQ.d1]).add(newQ);
((HashSet<Queen>)d2Confl[newQ.d2]).add(newQ);
queens[r.column] = newQ.row;
return false;
}
public static Queen choseConflictQueen () {
HashSet<Queen> conflictSet = new HashSet<Queen>();
boolean hasConflicts = false;
for (int i = 0; i < 2*N - 1; i++) {
if (i < N && rowConfl[i] != null) {
hasConflicts = hasConflicts || rowConfl[i].size() > 1;
conflictSet.addAll(rowConfl[i]);
}
if (d1Confl[i] != null) {
hasConflicts = hasConflicts || d1Confl[i].size() > 1;
conflictSet.addAll(d1Confl[i]);
}
if (d2Confl[i] != null) {
hasConflicts = hasConflicts || d2Confl[i].size() > 1;
conflictSet.addAll(d2Confl[i]);
}
}
if (hasConflicts) {
int c = random.nextInt(conflictSet.size());
return (Queen) conflictSet.toArray()[c];
}
return null;
}
public static Queen findNewBestPosition(Queen old) {
int[] row = new int[N];
int min = Integer.MAX_VALUE;
int minInd = old.row;
for (int i = 0; i < N; i++) {
if (rowConfl[i] != null) {
row[i] = rowConfl[i].size();
}
if (d1Confl[old.column + i] != null) {
row[i] += d1Confl[old.column + i].size();
}
if (d2Confl[N - 1 + old.column - i] != null) {
row[i] += d2Confl[N - 1 + old.column - i].size();
}
if (i == old.row) {
row[i] = row[i] - 3;
}
if (row[i] <= min && i != minInd) {
min = row[i];
minInd = i;
}
}
return new Queen(old.column, minInd, old.column + minInd, N - 1 + old.column - minInd);
}
public static void main(String[] args) {
long startTime = System.currentTimeMillis();
init();
int steps = 0;
while(!checkItLinear()) {
if (++steps > maxSteps) {
init();
steps = 0;
}
}
long endTime = System.currentTimeMillis();
System.out.println("Done for " + (endTime - startTime) + "ms\n");
if(printBoard){
print();
}
}
}
Edit:
Here is my a-little-bit-optimized solution with removing some unused objects and putting the queens on diagonal positions when initializing.
import java.util.Random;
import java.util.Vector;
public class SolveQueens {
public static boolean PRINT_BOARD = true;
public static int N = 10;
public static int MAX_STEPS = 5000;
public static int[] queens = new int[N];
public static Random random = new Random();
public static int[] rowConfl = new int[N];
public static int[] d1Confl = new int[2*N - 1];
public static int[] d2Confl = new int[2*N - 1];
public static Vector<Integer> conflicts = new Vector<Integer>();
public static void init () {
random = new Random();
for (int i = 0; i < N; i++) {
queens[i] = i;
}
}
public static int getD1Pos (int col, int row) {
return col + row;
}
public static int getD2Pos (int col, int row) {
return N - 1 + col - row;
}
public static void print () {
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
System.out.print(queens[i] == j ? "Q " : "* ");
}
System.out.println();
}
System.out.println();
}
public static boolean hasConflicts() {
generateConflicts();
if (conflicts.isEmpty()) {
return false;
}
int r = random.nextInt(conflicts.size());
int conflQueenCol = conflicts.get(r);
int currentRow = queens[conflQueenCol];
int bestRow = currentRow;
int minConfl = getConflicts(conflQueenCol, queens[conflQueenCol]) - 3;
int tempConflCount;
for (int i = 0; i < N ; i++) {
tempConflCount = getConflicts(conflQueenCol, i);
if (i != currentRow && tempConflCount <= minConfl) {
minConfl = tempConflCount;
bestRow = i;
}
}
queens[conflQueenCol] = bestRow;
return true;
}
public static void generateConflicts () {
conflicts = new Vector<Integer>();
rowConfl = new int[N];
d1Confl = new int[2*N - 1];
d2Confl = new int[2*N - 1];
for (int i = 0; i < N; i++) {
int r = queens[i];
rowConfl[r]++;
d1Confl[getD1Pos(i, r)]++;
d2Confl[getD2Pos(i, r)]++;
}
for (int i = 0; i < N; i++) {
int conflictsCount = getConflicts(i, queens[i]) - 3;
if (conflictsCount > 0) {
conflicts.add(i);
}
}
}
public static int getConflicts(int col, int row) {
return rowConfl[row] + d1Confl[getD1Pos(col, row)] + d2Confl[getD2Pos(col, row)];
}
public static void main(String[] args) {
long startTime = System.currentTimeMillis();
init();
int steps = 0;
while(hasConflicts()) {
if (++steps > MAX_STEPS) {
init();
steps = 0;
}
}
long endTime = System.currentTimeMillis();
System.out.println("Done for " + (endTime - startTime) + "ms\n");
if(PRINT_BOARD){
print();
}
}
}
Comments would have been helpful :)
Rather than recreating your conflict set and your "worst conflict" queen everything, could you create it once, and then just update the changed rows/columns?
EDIT 0:
I tried playing around with your code a bit. Since the code is randomized, it's hard to find out if a change is good or not, since you might start with a good initial state or a crappy one. I tried making 10 runs with 10 queens, and got wildly different answers, but results are below.
I psuedo-profiled to see which statements were being executed the most, and it turns out the inner loop statements in chooseConflictQueen are executed the most. I tried inserting a break to pull the first conflict queen if found, but it didn't seem to help much.
Grouping only runs that took more than a second:
I realize I only have 10 runs, which is not really enough to be statistically valid, but hey.
So adding breaks didn't seem to help. I think a constructive solution will likely be faster, but randomness will again make it harder to check.
Your approach is good : Local search algorithm with minimum-conflicts constraint. I would suggest try improving your initial state. Instead of randomly placing all queens, 1 per column, try to place them so that you minimize the number of conflicts. An example would be to try placing you next queen based on the position of the previous one ... or maybe position of previous two ... Then you local search will have less problematic columns to deal with.
If you randomly select, you could be selecting the same state as a previous state. Theoretically, you might never find a solution even if there is one.
I think you woud be better to iterate normally through the states.
Also, are you sure boards other than 8x8 are solvable?
By inspection, 2x2 is not, 3x3 is not, 4x4 is not.