Advertisement
Guest User

MDPYPC-TP03-Ejercicio 7

a guest
Apr 6th, 2020
233
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.50 KB | None | 0 0
  1. package e7;
  2. public class sudoku {
  3.  
  4.     public static void main(String[] args) {
  5.         int matriz [] [] = new int [4] [4];
  6.         int fila, columna;
  7.         cargarmatriz(matriz);
  8.         System.out.println("El sudoku para completar es el siguiente:");
  9.         System.out.println();
  10.         mostrarmatriz(matriz);
  11.         solucion(matriz);
  12.         System.out.println();
  13.         System.out.println("La solución para las posiciones (1,2) y (3,4) es:");
  14.         System.out.println();
  15.         mostrarmatriz(matriz);
  16.     }
  17.    
  18.    
  19.     public static void cargarmatriz (int [] [] matriz) {
  20.     matriz [0] [0] = 3;
  21.     matriz [0] [2] = 1;
  22.     matriz [0] [3] = 2;
  23.     matriz [1] [1] = 1;
  24.     matriz [1] [3] = 3;
  25.     matriz [3] [0] = 1;
  26.     matriz [3] [2] = 3;
  27.     matriz [3] [3] = 4;  
  28.     }
  29.    
  30.     public static void mostrarmatriz (int [] [] matriz) {
  31.     for (int i=0; i<matriz.length; i++) {
  32.      for (int j=0; j<matriz[0].length; j++) {
  33.      System.out.print(matriz [i] [j] + " " );
  34.      }
  35.     System.out.println();
  36.     }
  37.     }
  38.    
  39.     public static boolean estaenfila (int [] [] matriz, int fila, int numero) {
  40.     for (int i=0; i<matriz.length; i++) {
  41.         if (matriz [fila] [i] == numero) {
  42.          return true;
  43.         }
  44.        
  45.     }
  46.     return false;
  47.     }
  48.    
  49.     public static boolean estaencolumna (int [] [] matriz, int columna, int numero) {
  50.         for (int j=0; j<matriz[0].length; j++) {
  51.             if (matriz [j] [columna] == numero) {
  52.                  return true;
  53.                 }
  54.         }
  55.     return false;
  56.         }
  57.        
  58.     public static boolean estaencuadrado (int [] [] matriz, int fila, int columna, int numero) {
  59.         int f = fila - fila%2;
  60.         int c = columna - columna%2;
  61.         for (int i = f; i<f+2; i++) {
  62.          for (int j = c; j < c+2; j++) {
  63.              if (matriz [i] [j] == numero) {
  64.                  return true;
  65.              }
  66.          }
  67.         }
  68.      return false; 
  69.     }
  70.    
  71.     public static boolean noesta (int [] [] matriz, int fila, int columna, int numero) {
  72.     return !estaenfila (matriz, fila, numero) && !estaencolumna (matriz, columna, numero) && !estaencuadrado(matriz, fila, columna, numero);
  73.     }
  74.    
  75.     public static void solucion (int [] [] matriz){
  76.           for (int numero = 1; numero<=matriz.length; numero++) {
  77.            if (matriz [0] [1] == 0) {
  78.            if (noesta (matriz, 0, 1, numero)) {
  79.                matriz [0] [1] = numero;
  80.            }
  81.           }
  82.            if (matriz [2] [3] == 0) {
  83.                if (noesta (matriz, 2, 3, numero)) {
  84.                    matriz [2] [3] = numero;
  85.                }
  86.               }
  87.           }    
  88.       }
  89.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement