error with arrayList in java

0

I'm doing an implementation of the n-queens problem in java, the idea is to have an arrayList that saves arrays and in turn each fix is a different solution, but when I check the arrayList the arrays are full of cigars 7.

the code is this:

   public static void queens(int[] tablero, int x,ArrayList soluciones) {
    int n = tablero.length; 
    if (x >= n) // verifica que todas las reinas esten puestas
        if (esValido(tablero)){ // metodo que mira si las reinas estan bien puestas
            soluciones.add(tablero); // se añade el tablero al arreglo que guarda las soluciones
            return;
        }
        else {
            return;
        } 
    for (int i = 0; i < n; ++i) { // for para ir poniendo las reinas  
        tablero[x] = i; // coloca la reina en la fila i columna x 
        queens(tablero, x + 1,soluciones); 
    }
}

the method is valid is this:

   public static boolean esValido(int[] tablero) {
    int n = tablero.length;
    for (int i = 0; i < n; ++i) {
        if (tablero[i] < 0 || tablero[i] >= n) 
            return false;
        for (int j = 0; j < n; ++j)
            if (i != j) {
                if (tablero[i] == tablero[j])
                    return false;
                if (i - j == Math.abs(tablero[i] - tablero[j]))
                    return false;
            }
    }
    return true;
}
    
asked by Eduard Damiam 25.02.2018 в 04:21
source

1 answer

0

I recommend that you check your "if" in each of the cycles, that is why some are not fulfilled and you pass the same value. Greetings

    
answered by 26.02.2018 в 02:46