楼主: Lisrelchen
1059 3

【独家发布】[Case Study]Solving Sudoku using Java [推广有奖]

  • 0关注
  • 62粉丝

VIP

已卖:4194份资源

院士

67%

还不是VIP/贵宾

-

TA的文库  其他...

Bayesian NewOccidental

Spatial Data Analysis

东西方数据挖掘

威望
0
论坛币
50288 个
通用积分
83.6306
学术水平
253 点
热心指数
300 点
信用等级
208 点
经验
41518 点
帖子
3256
精华
14
在线时间
766 小时
注册时间
2006-5-4
最后登录
2022-11-6

楼主
Lisrelchen 发表于 2016-8-31 21:01:52 |AI写论文

+2 论坛币
k人 参与回答

经管之家送您一份

应届毕业生专属福利!

求职就业群
赵安豆老师微信:zhaoandou666

经管之家联合CDA

送您一个全额奖学金名额~ !

感谢您参与论坛问题回答

经管之家送您两个论坛币!

+2 论坛币
  1. /**
  2. * The <code>Sudoku</code> class povides a static <code>main</code>
  3. * method allowing it to be called from the command line to print the
  4. * solution to a specified Sudoku problem.  
  5. *
  6. * <p>The following is an example of a Sudoku problem:
  7. *
  8. * <pre>
  9. * -----------------------
  10. * |   8   | 4   2 |   6   |
  11. * |   3 4 |       | 9 1   |
  12. * | 9 6   |       |   8 4 |
  13. *  -----------------------
  14. * |       | 2 1 6 |       |
  15. * |       |       |       |
  16. * |       | 3 5 7 |       |
  17. *  -----------------------
  18. * | 8 4   |       |   7 5 |
  19. * |   2 6 |       | 1 3   |
  20. * |   9   | 7   1 |   4   |
  21. *  -----------------------
  22. * </pre>
  23. *
  24. * The goal is to fill in the missing numbers so that
  25. * every row, column and box contains each of the numbers
  26. * <code>1-9</code>.  Here is the solution to the
  27. * problem above:
  28. *
  29. * <pre>
  30. *  -----------------------
  31. * | 1 8 7 | 4 9 2 | 5 6 3 |
  32. * | 5 3 4 | 6 7 8 | 9 1 2 |
  33. * | 9 6 2 | 1 3 5 | 7 8 4 |
  34. *  -----------------------
  35. * | 4 5 8 | 2 1 6 | 3 9 7 |
  36. * | 2 7 3 | 8 4 9 | 6 5 1 |
  37. * | 6 1 9 | 3 5 7 | 4 2 8 |
  38. *  -----------------------
  39. * | 8 4 1 | 9 6 3 | 2 7 5 |
  40. * | 7 2 6 | 5 8 4 | 1 3 9 |
  41. * | 3 9 5 | 7 2 1 | 8 4 6 |
  42. *  -----------------------
  43. * </pre>
  44. *
  45. * Note that the first row <code>187492563</code> contains
  46. * each number exactly once, as does the first column
  47. * <code>159426873</code>, the upper-left box
  48. * <code>187534962</code>, and every other row, column
  49. * and box.
  50. *
  51. * <p>The {@link #main(String[])} method encodes a problem as an array
  52. * of strings, with one string encoding each constraint in the problem
  53. * in row-column-value format.  Here is the problem again with
  54. * the indices indicated:
  55. *
  56. * <pre>
  57. *     0 1 2   3 4 5   6 7 8
  58. *    -----------------------
  59. * 0 |   8   | 4   2 |   6   |
  60. * 1 |   3 4 |       | 9 1   |
  61. * 2 | 9 6   |       |   8 4 |
  62. *    -----------------------
  63. * 3 |       | 2 1 6 |       |
  64. * 4 |       |       |       |
  65. * 5 |       | 3 5 7 |       |
  66. *   -----------------------
  67. * 6 | 8 4   |       |   7 5 |
  68. * 7 |   2 6 |       | 1 3   |
  69. * 8 |   9   | 7   1 |   4   |
  70. *    -----------------------
  71. * </pre>
  72. *
  73. * The <code>8</code> in the upper left box of the puzzle is encoded
  74. * as <code>018</code> (<code>0</code> for the row, <code>1</code> for
  75. * the column, and <code>8</code> for the value).  The <code>4</code>
  76. * in the lower right box is encoded as <code>874</code>.  
  77. *
  78. * <p>The full command-line invocation for the above puzzle is:
  79. *
  80. * <pre>
  81. * % java -cp . Sudoku 018 034 052 076 \
  82. *                     113 124 169 171 \
  83. *                     209 216 278 284 \
  84. *                     332 341 356     \
  85. *                     533 545 557     \
  86. *                     608 614 677 685 \
  87. *                     712 726 761 773 \
  88. *                     819 837 851 874 \
  89. * </pre>
  90. *
  91. * <p>See <a href="http://en.wikipedia.org/wiki/Sudoku">Wikipedia:
  92. * Sudoku</a> for more information on Sudoku.
  93. *
  94. * <p>The algorithm employed is similar to the standard backtracking
  95. * <a href="http://en.wikipedia.org/wiki/Eight_queens_puzzle">eight
  96. * queens algorithm</a>.
  97. *
  98. * @version 1.0
  99. * @author <a href="http://www.colloquial.com/carp">Bob Carpenter</a>
  100. */
  101. public class Sudoku {

  102.     /**
  103.      * Print the specified Sudoku problem and its solution.  The
  104.      * problem is encoded as specified in the class documentation
  105.      * above.
  106.      *
  107.      * @param args The command-line arguments encoding the problem.
  108.      */
  109.     public static void main(String[] args) {
  110.         int[][] matrix = parseProblem(args);
  111.         writeMatrix(matrix);
  112.         if (solve(0,0,matrix))    // solves in place
  113.             writeMatrix(matrix);
  114.         else
  115.             System.out.println("NONE");
  116.     }

  117.     static boolean solve(int i, int j, int[][] cells) {
  118.         if (i == 9) {
  119.             i = 0;
  120.             if (++j == 9)
  121.                 return true;
  122.         }
  123.         if (cells[i][j] != 0)  // skip filled cells
  124.             return solve(i+1,j,cells);
  125.        
  126.         for (int val = 1; val <= 9; ++val) {
  127.             if (legal(i,j,val,cells)) {  
  128.                 cells[i][j] = val;      
  129.                 if (solve(i+1,j,cells))  
  130.                     return true;
  131.             }
  132.         }
  133.         cells[i][j] = 0; // reset on backtrack
  134.         return false;
  135.     }

  136.     static boolean legal(int i, int j, int val, int[][] cells) {
  137.         for (int k = 0; k < 9; ++k)  // row
  138.             if (val == cells[k][j])
  139.                 return false;

  140.         for (int k = 0; k < 9; ++k) // col
  141.             if (val == cells[i][k])
  142.                 return false;

  143.         int boxRowOffset = (i / 3)*3;
  144.         int boxColOffset = (j / 3)*3;
  145.         for (int k = 0; k < 3; ++k) // box
  146.             for (int m = 0; m < 3; ++m)
  147.                 if (val == cells[boxRowOffset+k][boxColOffset+m])
  148.                     return false;

  149.         return true; // no violations, so it's legal
  150.     }

  151.     static int[][] parseProblem(String[] args) {
  152.         int[][] problem = new int[9][9]; // default 0 vals
  153.         for (int n = 0; n < args.length; ++n) {
  154.             int i = Integer.parseInt(args[n].substring(0,1));   
  155.             int j = Integer.parseInt(args[n].substring(1,2));   
  156.             int val = Integer.parseInt(args[n].substring(2,3));
  157.             problem[i][j] = val;
  158.         }
  159.         return problem;
  160.     }

  161.     static void writeMatrix(int[][] solution) {
  162.         for (int i = 0; i < 9; ++i) {
  163.             if (i % 3 == 0)
  164.                 System.out.println(" -----------------------");
  165.             for (int j = 0; j < 9; ++j) {
  166.                 if (j % 3 == 0) System.out.print("| ");
  167.                 System.out.print(solution[i][j] == 0
  168.                                  ? " "
  169.                                  : Integer.toString(solution[i][j]));
  170.                
  171.                 System.out.print(' ');
  172.             }
  173.             System.out.println("|");
  174.         }
  175.         System.out.println(" -----------------------");
  176.     }

  177. }
复制代码

二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

关键词:Case study Solving sudoku study Using following specified solution command example

本帖被以下文库推荐

沙发
Lisrelchen 发表于 2016-8-31 21:03:02
  1. Compiling and Running Sudoku.java
  2. The following session provides an example of how to compile and run the code (assuming you have the Java developer's kit installed).

  3. Bob Carpenter@ry /cygdrive/c/mycvs/carp
  4. $ cd sudoku/

  5. Bob Carpenter@ry /cygdrive/c/mycvs/carp/sudoku
  6. $ ls -l Sudoku.java
  7. -rwxr-xr-x 1 Bob Carpenter None 5285 Apr 27 23:59 Sudoku.java

  8. Bob Carpenter@ry /cygdrive/c/mycvs/carp/sudoku
  9. $ javac Sudoku.java

  10. Bob Carpenter@ry /cygdrive/c/mycvs/carp/sudoku
  11. $ ls -l Sudoku.class
  12. -rwxr-xr-x 1 Bob Carpenter None 1600 Apr 28 00:01 Sudoku.class

  13. Bob Carpenter@ry /cygdrive/c/mycvs/carp/sudoku
  14. $ java -cp . Sudoku  014 023 037 069 088 \
  15. 125 143 211 263 306 342 357 404 427 461 483 \
  16. 535 544 589 622 673 745 764 805 824 851 862 876
  17. -----------------------
  18. |   4 3 | 7     | 9   8 |
  19. |     5 |   3   |       |
  20. |   1   |       | 3     |
  21. -----------------------
  22. | 6     |   2 7 |       |
  23. | 4   7 |       | 1   3 |
  24. |       | 5 4   |     9 |
  25. -----------------------
  26. |     2 |       |   3   |
  27. |       |   5   | 4     |
  28. | 5   4 |     1 | 2 6   |
  29. -----------------------
  30. -----------------------
  31. | 2 4 3 | 7 1 6 | 9 5 8 |
  32. | 9 8 5 | 2 3 4 | 7 1 6 |
  33. | 7 1 6 | 8 9 5 | 3 4 2 |
  34. -----------------------
  35. | 6 3 9 | 1 2 7 | 5 8 4 |
  36. | 4 5 7 | 9 6 8 | 1 2 3 |
  37. | 8 2 1 | 5 4 3 | 6 7 9 |
  38. -----------------------
  39. | 1 6 2 | 4 7 9 | 8 3 5 |
  40. | 3 7 8 | 6 5 2 | 4 9 1 |
  41. | 5 9 4 | 3 8 1 | 2 6 7 |
  42. -----------------------

  43. Bob Carpenter@ry /cygdrive/c/mycvs/carp/sudoku
  44. $
复制代码

藤椅
Lisrelchen 发表于 2016-8-31 21:10:45
  1. import java.util.Scanner;

  2. public class Solver {
  3.      
  4.     private static int[][] board = new int[9][9];
  5.      
  6.     public static void main(String[] args) {
  7.      
  8.     Scanner keyboard = new Scanner(System.in);
  9.      
  10.     System.out.println("Welcome to the sudoku solver!");
  11.     System.out.println("Type inital values separated by spaces, \nwith zeros for blank spots.");
  12.      
  13.     for(int y = 0; y < 9; y++) {
  14.         System.out.print("Enter row values: ");
  15.         for(int x = 0; x < 9; x++) {
  16.         board[x][y] = keyboard.nextInt();
  17.         }
  18.         keyboard.nextLine();
  19.     }
  20.      
  21.     System.out.println("Initial state: ");
  22.     printBoard();

  23.     solve(0, 0);

  24.     System.out.println("Solved state: ");
  25.     printBoard();
  26.     }

  27.     public static void solve(int x, int y) {
  28.     // find next open cell
  29.     int[] nextOpenCell = findOpenCell(x, y);
  30.     if(nextOpenCell == null) return;
  31.     int xPos = nextOpenCell[0];
  32.     int yPos = nextOpenCell[1];
  33.      
  34.     System.out.println("xPos: " + xPos + " yPos: " + yPos); // check...

  35.     // attempt to fill
  36.     for(int currentNumber = fillCell(1, xPos, yPos); currentNumber != -1;
  37.         currentNumber = fillCell(currentNumber+1, xPos, yPos)) {

  38.         if(xPos >= 8)
  39.         solve(0, yPos+1);
  40.         else
  41.         solve(xPos+1, yPos);
  42.         if(finished()) return;
  43.     }
  44.     }

  45.     public static int fillCell(int val, int x, int y) {
  46.         if(val > 9) return -1;
  47.     if(checkRow(val, x) && checkColumn(val, y) && checkBox(val, x, y)) {
  48.         board[x][y] = val;
  49.         return val;
  50.     }
  51.     return fillCell(val+1, x, y);
  52.     }

  53.     public static boolean checkRow(int val, int x) {
  54.     for(int i = 0; i < 9; i++) {
  55.         if(board[x][i] == val) return false;
  56.     }
  57.     return true;
  58.     }

  59.     public static boolean checkColumn(int val, int y) {
  60.     for(int i = 0; i < 9; i++) {
  61.         if(board[i][y] == val) return false;
  62.     }
  63.     return true;
  64.     }

  65.     public static boolean checkBox(int val, int x, int y) {

  66.     // find 3x3's upper right x/ys
  67.     int i = (x / 3) * 3;
  68.     int j = (y / 3) * 3;

  69.     // check each cell in the 3x3
  70.     for(int row = i; row < i+3; row++) {
  71.         for(int column = j; column < j+3; column++) {
  72.         if(board[row][column] == val) return false;
  73.         }
  74.     }
  75.     return true;
  76.     }

  77.     public static void printBoard() {
  78.     for(int y = 0; y < 9; y++) {
  79.         if(y%3 == 0) System.out.println("");
  80.         for(int x = 0; x < 9; x++) {
  81.         if(x%3 == 0) System.out.print("  ");
  82.         if(board[x][y] == 0)
  83.             System.out.print("_ ");
  84.         else
  85.             System.out.print(board[x][y] + " ");
  86.         }
  87.         System.out.println("");
  88.     }
  89.     }

  90.     public static int[] findOpenCell(int x, int y) {
  91.     for(int column = y; column < 9; column++) {
  92.         for(int row = x; row < 9; row++) {
  93.         if(board[row][column] == 0) {
  94.             return new int[] {row, column};
  95.         }
  96.         }
  97.     }
  98.     return null;
  99.     }

  100.     public static boolean finished() {
  101.     for(int row = 0; row < 9; row++) {
  102.         for(int column = 0; column < 9; column++) {
  103.         if(board[row][column] == 0)
  104.             return false;
  105.         }
  106.     }
  107.     return true;
  108.     }
  109. }
复制代码


https://coderanch.com/t/601344/java/java/Sudoku-Solving-Java-Program

板凳
西门高 发表于 2016-8-31 21:15:08
谢谢分享

您需要登录后才可以回帖 登录 | 我要注册

本版微信群
jg-xs1
拉您进交流群
GMT+8, 2025-12-29 09:52