Created
March 28, 2013 04:20
-
-
Save peas/5260576 to your computer and use it in GitHub Desktop.
8 rainhas meia boca
This file contains hidden or 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 Rainhas { | |
private static int[] rainhas = new int[8]; | |
public static void main(String[] args) { | |
tentaNaPosicao(0); | |
} | |
private static void tentaNaPosicao(int coluna) { | |
for(int linha = 0; linha < 8; linha++) { | |
if(valido(coluna, linha)) { | |
rainhas[coluna] = linha; | |
if(coluna == 7) System.out.println(Arrays.toString(rainhas)); | |
else tentaNaPosicao(coluna+1); | |
} | |
} | |
} | |
private static boolean valido(int coluna, int linha) { | |
for(int i = 0; i < coluna; i++) | |
if(coluna - i == Math.abs(rainhas[i] - linha) || rainhas[i] == linha) return false; | |
return true; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment