Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 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);
- }
- }
- 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;
- }
Add Comment
Please, Sign In to add comment