Created
August 24, 2012 21:08
-
-
Save tcard/3455637 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
public class Reinas { | |
public static int reinas(int n) { | |
int cont = 0; | |
for (int i=0; i<n; i++) { | |
List<Integer> lista = new ArrayList<Integer>(); | |
lista.add(i); | |
cont += reinas(n, lista); | |
} | |
return int; | |
} | |
public static int reinas (int n, List<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)) { | |
lista.add(i); | |
cont += reinas(n, lista) | |
} | |
} | |
return cont; | |
} | |
public static boolean noCome(int i, List<Integer> lista) { | |
int j = lista.size(); | |
for (int k=0; k<lista.size(); k++) { | |
if (j-1 == k-lista.get(k)) return false; | |
if (i+j == k+lista.get(k)) 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