Ola pessoal esse era pra ser um código que resolve o sudoku, ele pega onde tem o numero 0 e verifica qual numero pode por la mas ele esta deixando alguns números 0 no meio da matriz sem preencher.
Alguem poderia me ajudar, Grato.
package sudoku;
public class Main {
public static int N = 9;
public static int VAZIO = 0;
public static void print_tabela(int[][] tabela) {
System.out.println(" \t1 2 3 4 5 6 7 8 9\n");
for(int linha = 0; linha < N; linha++) {
System.out.printf("%d\t", (linha+1));
for(int coluna = 0; coluna < N; coluna++) {
System.out.printf("%d ", tabela[linha][coluna]);
}
System.out.println();
}
}
public static boolean existe_linha(int[][] tabela, int linha, int num) {
for (int coluna = 0; coluna < N; coluna++) {
if(tabela[linha][coluna] == num) {
return true;
}
}
return false;
}
public static boolean existe_coluna(int[][] tabela, int coluna, int num) {
for(int linha = 0; linha < N; linha++) {
if(tabela[linha][coluna] == num) {
return true;
}
}
return false;
}
public static boolean existe_caixa(int[][] tabela, int comLinha, int comCol, int num) {
for (int linha = 0; linha < 3; linha++) {
for (int coluna = 0; coluna < 3; coluna++) {
if (tabela[linha + comLinha][coluna + comCol] == num) {
return true;
}
}
}
return false;
}
public static boolean numero_safe(int[][] tabela, int linha, int coluna, int num) {
return !existe_linha(tabela, linha, num)
&& !existe_coluna(tabela, coluna, num)
&& !existe_caixa(tabela, linha - (linha % 3), coluna - (coluna %3), num);
}
public static boolean achar_vazio(int[][] tabela) {
for (int linha = 0; linha < 9; linha++) {
for(int coluna = 0; coluna < 9; coluna++){
if(tabela[linha][coluna] == VAZIO) {
coloca_numero(tabela, linha, coluna);
}
}
}
return false;
}
public static boolean coloca_numero(int[][] tabela, int linha, int coluna) {
for (int num = 1; num <= 9; num++) {
if (numero_safe(tabela, linha, coluna, num)) {
tabela[linha][coluna] = num;
if (resolvido(tabela)) {
return true;
}
coloca_numero(tabela, linha, coluna);
}
}
return false;
}
public static boolean resolvido (int[][] tabela) {
if(!achar_vazio(tabela)) {
return true;
}
return false;
}
public static void main(String[] args) {
// TODO Auto-generated method stub
int tabela[][] = new int[][] {
{0,7,1, 4,5,0, 9,2,6},
{9,0,3, 7,2,6, 0,8,1},
{2,0,6, 8,0,1, 0,3,0},
{7,0,0, 3,0,0, 0,0,0},
{1,3,0, 5,0,7, 0,6,9},
{4,9,0, 1,6,2, 7,0,0},
{6,8,0, 9,3,5, 0,1,4},
{5,2,9, 6,1,4, 0,7,0},
{3,1,0, 2,7,8, 6,0,5}};
if(resolvido(tabela)) {
print_tabela(tabela);
} else {
System.out.println("Sem solução");
}
}
}