Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package sudoku;
- import java.util.Scanner;
- import java.util.ArrayList;
- public class Sudoku {
- static int s= 1;
- public static int[][] CargarMatriz(){
- Scanner sce = new Scanner(System.in);
- int[][] Matriz = new int[4][4];
- for (int i=0; i<Matriz.length; i++) {
- for (int j=0; j<Matriz.length;j++){
- System.out.println("Ingrese elemento"+" "+i+","+ j);
- int x= sce.nextInt();
- Matriz[i][j] = x;
- }
- }
- return Matriz;
- }
- public static boolean VerificarPos(int x, int y, int[][]m) {
- boolean valida;
- if (m[x][y] == 0) {
- valida = true;
- }else {
- valida = false;
- }
- return valida;
- }
- public static int ResolverFila(int x, int[][]m, int s) {
- ArrayList<Integer> valoresf = new ArrayList<Integer>();
- for (int j=0; j<m.length;j++) {
- if (m[x][j]!=0) {
- valoresf.add(m[x][j]);
- }
- }
- for (int k=1; k<5; k++) {
- if (valoresf.isEmpty()==false) {
- if(valoresf.contains(k)==false) {
- s = k;
- }
- }
- }return s;
- }
- public static int ResolverColumna(int y, int[][]m, int solfila) {
- ArrayList<Integer> valoresc = new ArrayList<Integer>();
- int sol = 0;
- if (solfila<5){
- for (int i=0; i<m.length;i++) {
- if (m[i][y]!=0) {
- valoresc.add(m[i][y]);
- }
- }
- if(valoresc.contains(solfila)==false) {
- sol = solfila;
- }else {
- solfila= solfila+1;
- ResolverColumna(y, m, solfila);
- }
- }else {
- solfila = 0;
- ResolverColumna(y, m, solfila);
- }return sol;
- }
- public static void Resolver(int[][]sudoku, int x, int y, int s ) {
- int solucion = 0;
- if (VerificarPos(x,y, sudoku)) {
- int solfila = ResolverFila(x, sudoku, s);
- solucion = ResolverColumna(y, sudoku, solfila);
- }
- if (solucion!=0) {System.out.println("La solucion en la posicion"+" "+x+","+y+"es:"+" "+solucion);
- }else {
- s= s+1;
- Resolver(sudoku, x, y, s);
- }
- }
- public static void main(String[] args) {
- // TODO Auto-generated method stub
- int[][] sudoku = CargarMatriz();
- Scanner sce = new Scanner(System.in);
- System.out.println("Ingrese posicion a resolver (fila, columna)");
- int x= sce.nextInt();
- int y= sce.nextInt();
- Resolver(sudoku, x, y, s);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement