Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package e7;
- public class sudoku {
- public static void main(String[] args) {
- int matriz [] [] = new int [4] [4];
- int fila, columna;
- cargarmatriz(matriz);
- System.out.println("El sudoku para completar es el siguiente:");
- System.out.println();
- mostrarmatriz(matriz);
- solucion(matriz);
- System.out.println();
- System.out.println("La solución para las posiciones (1,2) y (3,4) es:");
- System.out.println();
- mostrarmatriz(matriz);
- }
- public static void cargarmatriz (int [] [] matriz) {
- matriz [0] [0] = 3;
- matriz [0] [2] = 1;
- matriz [0] [3] = 2;
- matriz [1] [1] = 1;
- matriz [1] [3] = 3;
- matriz [3] [0] = 1;
- matriz [3] [2] = 3;
- matriz [3] [3] = 4;
- }
- public static void mostrarmatriz (int [] [] matriz) {
- for (int i=0; i<matriz.length; i++) {
- for (int j=0; j<matriz[0].length; j++) {
- System.out.print(matriz [i] [j] + " " );
- }
- System.out.println();
- }
- }
- public static boolean estaenfila (int [] [] matriz, int fila, int numero) {
- for (int i=0; i<matriz.length; i++) {
- if (matriz [fila] [i] == numero) {
- return true;
- }
- }
- return false;
- }
- public static boolean estaencolumna (int [] [] matriz, int columna, int numero) {
- for (int j=0; j<matriz[0].length; j++) {
- if (matriz [j] [columna] == numero) {
- return true;
- }
- }
- return false;
- }
- public static boolean estaencuadrado (int [] [] matriz, int fila, int columna, int numero) {
- int f = fila - fila%2;
- int c = columna - columna%2;
- for (int i = f; i<f+2; i++) {
- for (int j = c; j < c+2; j++) {
- if (matriz [i] [j] == numero) {
- return true;
- }
- }
- }
- return false;
- }
- public static boolean noesta (int [] [] matriz, int fila, int columna, int numero) {
- return !estaenfila (matriz, fila, numero) && !estaencolumna (matriz, columna, numero) && !estaencuadrado(matriz, fila, columna, numero);
- }
- public static void solucion (int [] [] matriz){
- for (int numero = 1; numero<=matriz.length; numero++) {
- if (matriz [0] [1] == 0) {
- if (noesta (matriz, 0, 1, numero)) {
- matriz [0] [1] = numero;
- }
- }
- if (matriz [2] [3] == 0) {
- if (noesta (matriz, 2, 3, numero)) {
- matriz [2] [3] = numero;
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement