Created
August 24, 2012 22:26
-
-
Save tcard/3456435 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
import java.util.*; | |
public class Reinas { | |
public static int reinas(int n) { | |
int cont = 0; | |
for (int i=0; i<n; i++) { | |
ArrayList<Integer> lista = new ArrayList<Integer>(); | |
lista.add(i); | |
cont += reinas(n, (ArrayList<Integer>)lista.clone()); | |
} | |
return cont; | |
} | |
public static int reinas (int n, ArrayList<Integer> lista) { | |
if (lista.size() == n) { | |
return 1; | |
} | |
int cont = 0; | |
for (int i=0; i<n; i++) { | |
if (!lista.contains(i) && noCome(i, lista)) { | |
ArrayList<Integer> lista2 = (ArrayList<Integer>)lista.clone(); | |
lista2.add(i); | |
cont += reinas(n, lista2); | |
} | |
} | |
return cont; | |
} | |
public static boolean noCome(int i, ArrayList<Integer> lista) { | |
int j = lista.size() - 1; | |
int desp = 0; | |
for (int k=j; k >= 0; k--) { | |
desp++; | |
if (lista.get(k) == (i-desp) || lista.get(k) == (i+desp)) | |
return false; | |
} | |
return true; | |
} | |
public static void main(String[] args) { | |
System.out.println(reinas(5)); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment