Advertisement
Guest User

RecursionMatrix

a guest
Oct 20th, 2022
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 8.75 KB | Source Code | 0 0
  1. package sudoku;
  2.  
  3. import java.util.stream.IntStream;
  4.  
  5.  
  6.  
  7. public class Matrix {
  8.    
  9.     public static void main(String[] args) {
  10.         Matrix matrix = new Matrix();
  11.  
  12.         int[][] M1 = new int[][] {{0, 0, 0, 0},     // Empty Pseudoco
  13.                                   {0, 0, 0, 0},
  14.                                   {0, 0, 0, 0},
  15.                                   {0, 0, 0, 0}};
  16.        
  17.         int[][] M2 = new int[][] {{1, 2, 0, 0},     // Unsolvable Pseudoco
  18.                                   {4, 3, 0, 0},
  19.                                   {0, 0, 0, 0},
  20.                                   {0, 1, 0, 1}};
  21.        
  22.         int[][] M3 = new int[][] {{1, 2, 3, 4},     // Mising (i,j) (4,4) to be solved
  23.                                   {3, 4, 1, 2},
  24.                                   {2, 1, 4, 3},
  25.                                   {4, 3, 2, 0}};
  26.  
  27.         int[][] M4 = new int[][] {{0, 3, 0, 0},
  28.                                   {2, 0, 0, 0},
  29.                                   {3, 0, 1, 0},
  30.                                   {0, 0, 0, 0}};
  31.  
  32.         int[][] M5 = new int[][] {{3, 0, 6, 5, 0, 8, 4, 0, 0},
  33.                                   {5, 2, 0, 0, 0, 0, 0, 0, 0},
  34.                                   {0, 8, 7, 0, 0, 0, 0, 3, 1},
  35.                                   {0, 0, 3, 0, 1, 0, 0, 8, 0},
  36.                                   {9, 0, 0, 8, 6, 3, 0, 0, 5},
  37.                                   {0, 5, 0, 0, 9, 0, 6, 0, 0},
  38.                                   {1, 3, 0, 0, 0, 0, 2, 5, 0},
  39.                                   {0, 0, 0, 0, 0, 0, 0, 7, 4},
  40.                                   {0, 0, 5, 2, 0, 6, 3, 0, 0}};
  41.  
  42.         int[][] M6 = new int[][]   {{8, 0, 0, 0, 0, 0, 0, 0, 0},
  43.                                     {0, 0, 3, 6, 0, 0, 0, 0, 0},
  44.                                     {0, 7, 0, 0, 9, 0, 2, 0, 0},
  45.                                     {0, 5, 0, 0, 0, 7, 0, 0, 0},
  46.                                     {0, 0, 0, 0, 4, 5, 7, 0, 0},
  47.                                     {0, 0, 0, 1, 0, 0, 0, 3, 0},
  48.                                     {0, 0, 1, 0, 0, 0, 0, 6, 8},
  49.                                     {0, 0, 8, 5, 0, 0, 0, 1, 0},
  50.                                     {0, 9, 0, 0, 0, 0, 4, 0, 0}};
  51.  
  52.         System.out.println(matrix.sudokuCount(M1, 1, 1));
  53.     }
  54.    
  55.     private final int n = 2;
  56.     private volatile long count = 0;
  57.  
  58.     public boolean BackTrackingSudokuSolver(int[][] M, int i, int j) {
  59.         if (!isSudokuValid(M))
  60.             return false;
  61.         if (outsideGrid(M, i, j))
  62.             return true;
  63.         for (int k = 1; k <= M.length; k++) {
  64.             if (canPlace(M, i, j, k)) {
  65.                 boolean isClue;
  66.                 isClue = (M[i-1][j-1] != 0) ? true : false;
  67.                 M[i-1][j-1] = (isClue) ? M[i-1][j-1] : k;
  68.                 //System.out.println("* * * *");
  69.                 //printM(M);
  70.                 int iPrime = (j < M.length) ? i : i + 1;
  71.                 int jPrime = (j < M.length) ? j + 1 : 1;
  72.                 if (BackTrackingSudokuSolver(M, iPrime, jPrime))
  73.                     return true;
  74.                 M[i-1][j-1] = (isClue) ? k : 0;
  75.                 }
  76.             }
  77.         return false;
  78.     }
  79.  
  80.      public long BackTrackingSudokuSolverCount(int[][] M, int i, int j) {
  81.         if (!isSudokuValid(M))
  82.             return count;
  83.         if (outsideGrid(M, i, j)){
  84.             count++;
  85.             return count;
  86.         }
  87.  
  88.         int[][] array = new int[M.length][];
  89.         for (int r = 0; r < M.length; r++) {
  90.             array[r] = M[r].clone();
  91.         }
  92.  
  93.         for (int c = 1; c <= array.length; c++) {
  94.             for (int r = 1; r <= array.length; r++) {
  95.                 for (int k = 1; k <= M.length; k++) {
  96.                     for (int a = 0; a < M.length; a++) {
  97.                         array[a] = M[a].clone();
  98.                     }
  99.                     if (canPlace(array, c, r, k)) {
  100.                         boolean isClue;
  101.                         isClue = (array[c-1][r-1] != 0) ? true : false;
  102.                         array[c-1][r-1] = (isClue) ? array[c-1][r-1] : k;
  103.                         //System.out.println("* * * *");
  104.                         //printM(M);
  105.                         int iPrime = (r < M.length) ? c : c + 1;
  106.                         int jPrime = (r < M.length) ? r + 1 : 1;
  107.                         count++;
  108.                         BackTrackingSudokuSolver(array, iPrime, jPrime);
  109.                         if (BackTrackingSudokuSolver(M, iPrime, jPrime))
  110.                             count++;
  111.                             //return true;
  112.                             //array[c-1][r-1] = (isClue) ? k : 0;
  113.                         }
  114.                     }
  115.             }
  116.         }
  117.         return count;
  118.     }
  119.  
  120.     public long sudokuCount(int[][] M, int i, int j) {
  121.         int[][] array = new int[M.length][];
  122.         for (int r = 0; r < M.length; r++) {
  123.             array[r] = M[r].clone();
  124.         }
  125.        
  126.         for (int c = 1; c <= array.length; c++) {
  127.             for (int r = 1; r <= array.length; r++) {
  128.                 for (int k = 1; k <= array.length; k++) {
  129.                     for (int a = 0; a < M.length; a++) {
  130.                         array[a] = M[a].clone();
  131.                     }
  132.                     if (canPlace(array, c, r, k) && array[c-1][r-1] == 0){
  133.                         boolean isClue;
  134.                         isClue = (array[c-1][r-1] != 0) ? true : false;
  135.                         array[c-1][r-1] = (isClue) ? array[c-1][r-1] : k;
  136.                         if (BackTrackingSudokuSolver(array, i, j) && isSudokuSolved(array))
  137.                             count++;
  138.                     }
  139.                 }
  140.             }
  141.         }
  142.         return count;
  143.     }
  144.  
  145.     public boolean canPlace(int[][] M, int i, int j, int k) {
  146.         if (M[i-1][j-1] != 0 && M[i-1][j-1] != k)
  147.             return false;
  148.         int[][] array = new int[M.length][];
  149.         for (int r = 0; r < M.length; r++) {
  150.             array[r] = M[r].clone();
  151.         }
  152.         array[i-1][j-1] = k;
  153.         return
  154.         rowConstraint(array, i-1) &&
  155.         columnConstraint(array, j-1) &&
  156.         subgridContraint(array, i-1, j-1);
  157.     }
  158.  
  159.     public boolean rowConstraint(int[][] M, int i) {
  160.         boolean[] constraint = new boolean[n*n];
  161.         return IntStream.range(0, n*n)
  162.                 .allMatch(c -> checkConstraint(M, i, constraint, c));
  163.     }
  164.  
  165.     public boolean columnConstraint(int[][] M, int j) {
  166.         boolean[] constraint = new boolean[n*n];
  167.         return IntStream.range(0, n*n)
  168.                 .allMatch(r -> checkConstraint(M, r, constraint, j));
  169.     }
  170.  
  171.     public boolean subgridContraint(int[][] M, int i, int j) {
  172.         boolean[] constraint = new boolean[n*n];
  173.         int rowStart = (i / n) * n;
  174.         int rowEnd   = rowStart + n;
  175.        
  176.         int columnStart = (j / n) * n;
  177.         int columnEnd   = (columnStart + n);
  178.  
  179.         for (int row = rowStart; row < rowEnd; row++) {
  180.             for (int column = columnStart; column < columnEnd; column++) {
  181.                 if (!checkConstraint(M, row, constraint, column))
  182.                     return false;
  183.             }
  184.         }
  185.         return true;
  186.     }
  187.  
  188.     public boolean checkConstraint(int[][] M, int i, boolean[] constraint, int j) {
  189.         if (M[i][j] != 0) {
  190.             if (!constraint[M[i][j] - 1]) {
  191.                 constraint[M[i][j] - 1] = true;
  192.             } else {
  193.                 return false;
  194.             }
  195.         }
  196.         return true;
  197.     }
  198.  
  199.     private boolean outsideGrid(int[][] M, int i, int j) {
  200.         try {
  201.             if (M[i-1][j-1] != 0);
  202.             // boolean inBounds = (index >= 0) && (index < array.length);
  203.         } catch (IndexOutOfBoundsException e) {
  204.             return true;
  205.         }
  206.         return false;
  207.     }
  208.  
  209.     private void printM(int[][] M) {
  210.         for (int row = 0; row < n*n; row++) {
  211.             for (int column = 0; column < n*n; column++) {
  212.                 System.out.print(M[row][column] + " ");
  213.             }
  214.             System.out.println();
  215.         }
  216.     }
  217.  
  218.     public boolean isSudokuSolved(int[][] M) {
  219.         for (int i = 0; i < M.length; i++) {
  220.             for (int j = 0; j < M.length; j++) {
  221.                 if (!isSolved(M, i, j) || M[i][j] == 0)
  222.                     return false;
  223.             }
  224.         }
  225.         return true;
  226.     }
  227.  
  228.     public boolean isSudokuValid(int[][] M) {
  229.         for (int i = 0; i < M.length; i++) {
  230.             for (int j = 0; j < M.length; j++) {
  231.                 if (!isSolved(M, i, j))
  232.                     return false;
  233.             }
  234.         }
  235.         return true;
  236.     }
  237.  
  238.     public boolean isSolved(int[][] M, int i, int j) {
  239.         return
  240.         rowConstraint(M, i) &&
  241.         columnConstraint(M, j) &&
  242.         subgridContraint(M, i, j);
  243.     }
  244.    
  245. }
  246.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement