Advertisement
Luca_G6

SudokuPrueba1

Apr 10th, 2020
261
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.29 KB | None | 0 0
  1. package sudoku;
  2. import java.util.Scanner;
  3. import java.util.ArrayList;
  4.  
  5. public class Sudoku {
  6.     static int s= 1;
  7.     public static int[][] CargarMatriz(){
  8.         Scanner sce = new Scanner(System.in);
  9.         int[][] Matriz = new int[4][4];
  10.         for (int i=0; i<Matriz.length; i++) {
  11.             for (int j=0; j<Matriz.length;j++){
  12.             System.out.println("Ingrese elemento"+" "+i+","+ j);
  13.              int  x= sce.nextInt();
  14.              Matriz[i][j] = x;
  15.             }
  16.         }
  17.         return Matriz;
  18.     }
  19.    
  20.     public static boolean VerificarPos(int x, int y, int[][]m) {   
  21.         boolean valida;
  22.             if (m[x][y] == 0) {
  23.                 valida = true;
  24.             }else {
  25.                 valida = false;
  26.             }
  27.             return valida;
  28.             }
  29.    
  30.     public static int ResolverFila(int x, int[][]m, int s) {
  31.         ArrayList<Integer> valoresf = new ArrayList<Integer>();
  32.         for (int j=0; j<m.length;j++) {
  33.             if (m[x][j]!=0) {    
  34.                 valoresf.add(m[x][j]);
  35.             }
  36.         }
  37.         for (int k=1; k<5; k++) {
  38.             if (valoresf.isEmpty()==false) {
  39.             if(valoresf.contains(k)==false) {
  40.                   s = k;
  41.             }
  42.             }
  43.         }return s;
  44.     }
  45.        
  46.    
  47.     public static int ResolverColumna(int y, int[][]m, int solfila) {
  48.         ArrayList<Integer> valoresc = new ArrayList<Integer>();
  49.         int sol = 0;
  50.         if (solfila<5){
  51.          for (int i=0; i<m.length;i++) {
  52.             if (m[i][y]!=0) {
  53.                 valoresc.add(m[i][y]);
  54.             }
  55.          }
  56.          if(valoresc.contains(solfila)==false) {
  57.             sol = solfila;
  58.          }else {
  59.          solfila= solfila+1;
  60.          ResolverColumna(y, m, solfila);
  61.          }
  62.        
  63.         }else {
  64.         solfila = 0;
  65.         ResolverColumna(y, m, solfila);
  66.         }return sol;
  67.     }
  68.    
  69.       public static void Resolver(int[][]sudoku, int x, int y, int s ) {
  70.           int solucion = 0;
  71.       if (VerificarPos(x,y, sudoku)) {
  72.          int solfila = ResolverFila(x, sudoku, s);
  73.          solucion = ResolverColumna(y, sudoku, solfila);
  74.          }
  75.          if (solucion!=0) {System.out.println("La solucion en la posicion"+" "+x+","+y+"es:"+" "+solucion);
  76.      }else {
  77.          s= s+1;
  78.          Resolver(sudoku, x, y, s);
  79.          
  80.      }
  81.      }
  82.  
  83.     public static void main(String[] args) {
  84.         // TODO Auto-generated method stub
  85.       int[][] sudoku = CargarMatriz();
  86.       Scanner sce = new Scanner(System.in);
  87.       System.out.println("Ingrese posicion a resolver (fila, columna)");
  88.       int x= sce.nextInt();
  89.       int y= sce.nextInt();
  90.       Resolver(sudoku, x, y, s);
  91.    }
  92. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement