- import java.io.*;
- class Sudoku
- {
- int SQN = 3;
- int N = 9;
- int celda[][] = new int [N][N];
- boolean sector[][][] = new boolean [SQN][SQN][N+1];
- boolean RESUELTO;
- int celda_actual;
- public Sudoku ()
- {
- /* Inicializamos el sudoku con todo a cero */
- int i, j, k;
- celda_actual = 0;
- RESUELTO = false;
- for (i=0; i<9; i++)
- for (j=0; j<9; j++)
- celda [i][j] = 0;
- for (i=0; i<3; i++)
- for(j=0; j<3; j++)
- for(k=1; k<=9; k++)
- sector[i][j][k] = false;
- }
- public int getCelda(int i, int j)
- {
- return celda[i][j];
- }
- public void setCelda(int i, int j, int value)
- {
- celda[i][j] = value;
- }
- public boolean isResuelto()
- {
- return RESUELTO;
- }
- public void reset()
- {
- int i, j, k;
- celda_actual = 0;
- RESUELTO = false;
- for (i=0; i<9; i++)
- for (j=0; j<9; j++)
- celda [i][j] = 0;
- for (i=0; i<3; i++)
- for(j=0; j<3; j++)
- for(k=1; k<=9; k++)
- sector[i][j][k] = false;
- }
- public void muestra ()
- {
- int i, j;
- int h;
- System.out.printf("\n\n");
- for (i=0;i<N;i++)
- {
- if ((i%SQN == 0)&&(i!=0))
- {
- System.out.printf("\n -");
- System.out.printf("--------|------------|----------");
- System.out.printf("\n");
- }
- else
- System.out.printf("\n");
- for (j=0;j<N;j++)
- if ((j%SQN==0)&&(j!=0))
- {
- System.out.printf(" |%3d ",celda[i][j]);
- }
- else
- {
- System.out.printf("%3d", celda[i][j]);
- }
- }
- System.out.printf("\n\n");
- }
- public void resuelve ()
- {
- /* Comprueba que es resoluble, ajusta los sectores y lo resuelve */
- if (checkIntegridad())
- {
- resuelve (0);
- }
- else
- {
- RESUELTO = false;
- }
- }
- public boolean checkIntegridad ()
- {
- int i, j, k;
- boolean VALIDO = true;
- for(i=0; i<N; i++)
- {
- for(j=0; j<N; j++)
- {
- if (celda[i][j]!=0)
- {
- /* Veamos que no hay un número igual en toda la columna o fila*/
- for(k=0; k<N; k++)
- {
- if( ((k!=j)&&(celda[i][k]==celda[i][j])) || ((k!=i)&&(celda[k][j]==celda[i][j])) )
- {
- VALIDO = false; System.out.printf("\nMalo 1 en (%d,%d) k = %d", i,j,k);
- }
- }
- if(sector[i/SQN][j/SQN][celda[i][j]] == true) // ya estaba en el sector de antes
- {
- VALIDO = false; System.out.printf("\nMalo 2 en (%d, %d) ", i,j);
- }
- sector[i/SQN][j/SQN][celda[i][j]] = true;
- }
- }
- }
- return VALIDO;
- }
- public void resuelve (int celda_actual)
- {
- int i, j, n;
- boolean LEGAL;
- boolean VACIA = false;
- boolean ULTIMA = true;
- i = celda_actual/N;
- j = celda_actual%N;
- if (celda_actual < (N*N)-1)
- ULTIMA=false;
- if (celda[i][j] == 0)
- VACIA = true;
- n=0;
- if (VACIA)
- {
- do
- {
- n++;
- RESUELTO = false;
- LEGAL = true;
- /* ¿Es legal n? */
- for(int k=0; k<N; k++)
- if(celda[i][k]==n || celda[k][j]==n)
- LEGAL = false;
- if (sector[i/SQN][j/SQN][n]==true)
- LEGAL = false;
- if (LEGAL)
- {
- /* Escribimos el número en la casilla */
- celda[i][j] = n;
- sector[i/SQN][j/SQN][n] = true;
- /* Si era la última, hemos acabado */
- if (ULTIMA)
- RESUELTO = true;
- else // No es la última
- {
- /* Si no era la última, hay que seguir */
- resuelve(celda_actual+1);
- if (!RESUELTO) /* Si no ha tenido éxito, hay que borrar esta casilla y probar otra cosa */
- {
- celda[i][j] = 0;
- sector[i/SQN][j/SQN][n] = false;
- }
- }
- }
- }while((RESUELTO==false)&&(n<N));
- }
- else // No está vacía
- {
- if(ULTIMA)
- RESUELTO = true;
- else
- {
- resuelve(celda_actual+1);
- }
- }
- }
- }