Advertisement
uopspop

Untitled

Sep 7th, 2019
196
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.71 KB | None | 0 0
  1. import java.util.Arrays;
  2.  
  3. public class Backtracking_sudoko {
  4.     static boolean isDebugMode = false;
  5.     static int emptyCell = -1;
  6.     static boolean isEndReached = false;
  7.  
  8.  
  9.     public static void main(String[] args)
  10.     {
  11.         // 3 x 3 sudoko
  12. //        int[][] multi = new int[][]{
  13. //                { 3, emptyCell, 6 },
  14. //                { 5, 2, emptyCell },
  15. //                { emptyCell, 8, 7 }
  16. //        };
  17.  
  18.         // 9 x 9 sudoko
  19.         int[][] multi = new int[][]{
  20.                 { 3, emptyCell, 6, 5, emptyCell, 8, 4, emptyCell, emptyCell },
  21.                 { 5, 2, emptyCell, emptyCell, emptyCell, emptyCell, emptyCell, emptyCell, emptyCell },
  22.                 { emptyCell, 8, 7, emptyCell, emptyCell, emptyCell, emptyCell, 3, 1 },
  23.  
  24.                 { emptyCell, emptyCell, 3, emptyCell, 1, emptyCell, emptyCell, 8, emptyCell},
  25.                 { 9, emptyCell, emptyCell, 8, 6, 3, emptyCell, emptyCell, 5},
  26.                 { emptyCell, 5, emptyCell, emptyCell, 9, emptyCell, 6, emptyCell, emptyCell},
  27.  
  28.                 { 1, 3, emptyCell, emptyCell, emptyCell, emptyCell, 2, 5, emptyCell},
  29.                 { emptyCell, emptyCell, emptyCell, emptyCell, emptyCell, emptyCell, emptyCell, 7, 4},
  30.                 { emptyCell, emptyCell, 5, 2, emptyCell, 6, 3, emptyCell, emptyCell},
  31.         };
  32.  
  33.         System.out.println("initial: ");
  34.         print2DArray(multi);
  35.  
  36.         int[][] multiFilled = backtracking(multi);
  37.  
  38.         System.out.println("result: ");
  39.         print2DArray(multiFilled);
  40.     }
  41.  
  42.     private static int[][] backtracking(int[][] ary) {
  43.         int[][] aryFilled = generateDeepCopy2dAry(ary);
  44.         goDown(aryFilled, 0, 0);
  45.         return aryFilled;
  46.     }
  47.  
  48.     private static void goDown(int[][] multiFilled, int row, int col){
  49.         if (isDebugMode) System.out.printf("[%d, %d]:%d %n", row, col, multiFilled[row][col]);
  50.         // go down to the next empty cell: left -> right , top -> down
  51.         if (multiFilled[row][col] != emptyCell) {
  52.             if ((col + 1)< multiFilled[row].length) {
  53.                 col++;
  54.                 goDown(multiFilled, row, col);
  55.             } else {
  56.                 col = 0;
  57.                 if (row + 1 < multiFilled.length) {
  58.                     row++;
  59.                     goDown(multiFilled, row, col);
  60.                 } else {
  61.                     // if no more cells to evaluate
  62.                     isEndReached = true; // if we can reach to this point, it means we found "a" solution
  63.                 }
  64.             }
  65.             return;
  66.         }
  67.  
  68.         // found empty cell
  69.         // try 0 ~ 9
  70.         for (int possibleNo = 0; possibleNo <= 9; possibleNo++) {
  71.             multiFilled[row][col] = possibleNo;
  72.             boolean check001 = checkHorizontal(multiFilled, row);
  73.             boolean check002 = checkVertical(multiFilled, col);
  74.             boolean check003 = checkSqure(multiFilled, row, col);
  75.  
  76.             if (!check001 || !check002 || !check003) {
  77.                 multiFilled[row][col] = emptyCell; // restore change
  78.                 // if none succeed, we don't need to explore down forward
  79.                 continue;
  80.             }else {
  81.                 // if all pass, go on to the next empty cell
  82.                 goDown(multiFilled, row, col);
  83.                 if (isEndReached) return;
  84.             }
  85.         }
  86.  
  87.     }
  88.  
  89.     private static boolean checkSqure(int[][] ary, int nowRow, int nowCol) {
  90.         int[] oneToNine = new int[10]; // the first element we won't use
  91.         int row = (nowRow / 3) * 3; // baseline this run
  92.         int col = (nowCol / 3) * 3; // baseline this run
  93.  
  94.         int row_limit = row + 3;
  95.         int col_limit = col + 3;
  96.  
  97.         for (int tmpRow = row; tmpRow < row_limit; tmpRow++) {
  98.             for (int tmpCol = col; tmpCol < col_limit; tmpCol++) {
  99.                 if (ary[tmpRow][tmpCol] == emptyCell) continue;
  100.                 oneToNine[ary[tmpRow][tmpCol]]++;
  101.                 if (oneToNine[ary[tmpRow][tmpCol]] == 2) {
  102.                     return false;
  103.                 }
  104.             }
  105.         }
  106.         return true;
  107.     }
  108.  
  109.     private static boolean checkHorizontal(int[][] ary, int row) {
  110.         int[] oneToNine = new int[10]; // the first element we won't use
  111.         for (int col = 0 ; col < ary[row].length ; col++){
  112.             if (ary[row][col] == emptyCell) continue;
  113.             oneToNine[ary[row][col]]++;
  114.             if (oneToNine[ary[row][col]] == 2) {
  115.                 return false;
  116.             }
  117.         }
  118.         return true;
  119.     }
  120.  
  121.     private static boolean checkVertical(int[][] ary, int col) {
  122.         int[] oneToNine = new int[10]; // the first element we won't use
  123.         for (int row = 0 ; row < ary.length ; row++){
  124.             if (ary[row][col] == emptyCell) continue;
  125.             oneToNine[ary[row][col]]++;
  126.             if (oneToNine[ary[row][col]] == 2) {
  127.                 return false;
  128.             }
  129.         }
  130.         return true;
  131.     }
  132.  
  133.  
  134.     ///// Util Only below /////
  135.  
  136.     public static int[][] generateDeepCopy2dAry(int[][] original) {
  137.         if (original == null) {
  138.             return null;
  139.         }
  140.  
  141.         final int[][] result = new int[original.length][];
  142.         for (int i = 0; i < original.length; i++) {
  143.             result[i] = Arrays.copyOf(original[i], original[i].length);
  144.             // For Java versions prior to Java 6 use the next:
  145.             // System.arraycopy(original[i], 0, result[i], 0, original[i].length);
  146.         }
  147.         return result;
  148.     }
  149.     private static void print2DArray(int[][] ary){
  150.         for (int i = 0; i < ary.length; i++) {
  151.             for (int j = 0; j < ary[i].length; j++) {
  152.                 System.out.printf("%3d" , ary[i][j]);
  153.             }
  154.             System.out.println();
  155.         }
  156.     }
  157. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement