Guest User

Untitled

a guest
Feb 24th, 2018
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.06 KB | None | 0 0
  1. public static void queens(int[] tablero, int x,ArrayList soluciones) {
  2. int n = tablero.length;
  3. if (x >= n) // verifica que todas las reinas esten puestas
  4. if (esValido(tablero)){ // metodo que mira si las reinas estan bien puestas
  5. soluciones.add(tablero); // se añade el tablero al arreglo que guarda las soluciones
  6. return;
  7. }
  8. else {
  9. return;
  10. }
  11. for (int i = 0; i < n; ++i) { // for para ir poniendo las reinas
  12. tablero[x] = i; // coloca la reina en la fila i columna x
  13. queens(tablero, x + 1,soluciones);
  14. }
  15. }
  16.  
  17. public static boolean esValido(int[] tablero) {
  18. int n = tablero.length;
  19. for (int i = 0; i < n; ++i) {
  20. if (tablero[i] < 0 || tablero[i] >= n)
  21. return false;
  22. for (int j = 0; j < n; ++j)
  23. if (i != j) {
  24. if (tablero[i] == tablero[j])
  25. return false;
  26. if (i - j == Math.abs(tablero[i] - tablero[j]))
  27. return false;
  28. }
  29. }
  30. return true;
  31. }
Add Comment
Please, Sign In to add comment