Sudoku Solver

分手后的思念是犯贱 2023-06-09 13:28 117阅读 0赞

Sudoku Solver

文章目录

  • Sudoku Solver
    • 题目
    • 分析
    • 代码如下

题目

Write a program to solve a Sudoku puzzle by filling the empty cells.

A sudoku solution must satisfy all of the following rules:

  1. Each of the digits 1-9 must occur exactly once in each row.
  2. Each of the digits 1-9 must occur exactly once in each column.
  3. Each of the the digits 1-9 must occur exactly once in each of the 9 3x3 sub-boxes of the grid.

Empty cells are indicated by the character '.'.

Note:

  1. The given board contain only digits 1-9 and the character '.'.
  2. You may assume that the given Sudoku puzzle will have a single unique solution.
  3. The given board size is always 9x9.

分析

这道题就是给你一个数独(会空出一些格子),然后要求你根据数独的规则(不了解数独的自行网上查找),推断出这个空格子应该填什么。

题目将会给出一个char类型的9x9的二维数组,该数组包含数字1 - 9和字符.(表示空格)。然后要求你根据数独的规则填充这个给出的二维数组。

数独的规则:每一行,每一类,每个九宫格都不能有相同的数字。

这道题直接使用DFS求解即可。
首先,寻找出那些没有数字的位置并把它们记录下来。
然后使用DFS,对每个没有数字地位置从1到9进行尝试。直到能够把所有地空格填满而且没有冲突为止。

代码如下

  1. class Solution {
  2. public void solveSudoku(char[][] board) {
  3. int[] empty = new int[81];
  4. int empty_count = 0;
  5. // 寻找出没有数字地位置
  6. for (int i = 0; i < 9; i++) {
  7. for (int j = 0; j < 9; j++) {
  8. if (board[i][j] == '.') {
  9. empty[empty_count] = i * 9 + j;
  10. empty_count++;
  11. }
  12. }
  13. }
  14. // DFS求解
  15. boolean flag = false;
  16. for (int i = 0; i < 9; i++) {
  17. flag = DFS((char)('1' + i), board, empty, empty_count, 0);
  18. if (flag) {
  19. return;
  20. }
  21. }
  22. }
  23. private boolean DFS(char target, char[][] board, int[] empty, int empty_count, int empty_index) {
  24. int row = empty[empty_index] / 9;
  25. int col = empty[empty_index] % 9;
  26. if (checkTarget(target, board, row, col)) { // 该数字满足要求
  27. board[row][col] = target;
  28. // empty_index++;
  29. if (empty_index + 1== empty_count) { // 如果所有空都被填满
  30. return true;
  31. } else {
  32. boolean flag = false;
  33. for (int i = 0; i < 9; i++) {
  34. // 填下一个空格,尝试['1', ..., '9']
  35. flag = DFS((char)(i + '1'), board, empty, empty_count, empty_index + 1);
  36. if (flag) {
  37. return flag;
  38. }
  39. }
  40. // 下一个空格没有符合要求的,回退
  41. board[row][col] = '.';
  42. return flag;
  43. }
  44. } else {
  45. return false;
  46. }
  47. }
  48. private boolean checkTarget(char target, char[][] board, int row, int col) {
  49. return checkRow(target, row, board) &&
  50. checkCol(target, col, board) && checkCell(target, row / 3, col / 3, board);
  51. }
  52. private static boolean checkRow(char target, int row, char[][] board) {
  53. for (int i = 0; i < 9; i++) {
  54. if (board[row][i] == target) {
  55. return false;
  56. }
  57. }
  58. return true;
  59. }
  60. private static boolean checkCol(char target, int col, char[][] board) {
  61. for (int i = 0; i < 9; i++) {
  62. if (board[i][col] == target) {
  63. return false;
  64. }
  65. }
  66. return true;
  67. }
  68. private static boolean checkCell(char target, int x, int y, char[][] board) {
  69. int row = x * 3;
  70. int col = y * 3;
  71. for (int i = row; i < row + 3; i++) {
  72. for (int j = col; j < col + 3; j++) {
  73. if (board[i][j] == target) {
  74. return false;
  75. }
  76. }
  77. }
  78. return true;
  79. }
  80. }

发表评论

表情:
评论列表 (有 0 条评论,117人围观)

还没有评论,来说两句吧...

相关阅读