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

Empty cells are indicated by the character '.'.

You may assume that there will be only one unique solution.

解题思路:

经典的NP问题,采用Dancing Links可以优化算法,参考链接:https://www.ocf.berkeley.edu/~jchu/publicportal/sudoku/sudoku.paper.html

本题方法多多,优化算法也是多多,本例仅给出最简单的DFS暴力枚举算法。

JAVA实现如下:

  1. static public void solveSudoku(char[][] board) {
  2. int count = 0;
  3. for (int i = 0; i < board.length; i++)
  4. for (int j = 0; j < board[0].length; j++)
  5. if (board[i][j] == '.')
  6. count++;
  7. dfs(board, count);
  8. }
  9.  
  10. public static int dfs(char[][] board, int count) {
  11. if (count == 0)
  12. return 0;
  13. for (int i = 0; i < board.length; i++) {
  14. for (int j = 0; j < board[0].length; j++) {
  15. if (board[i][j] == '.') {
  16. for (int k = 1; k <= 10; k++) {
  17. if (k == 10)
  18. return count;
  19. board[i][j] = (char) ('0' + k);
  20. if (!isValid(board, i, j))
  21. board[i][j] = '.';
  22. else {
  23. count--;
  24. count = dfs(board, count);
  25. if (count == 0)
  26. return count;
  27. count++;
  28. board[i][j] = '.';
  29. }
  30. }
  31. }
  32. }
  33. }
  34. return count;
  35. }
  36.  
  37. static public boolean isValid(char[][] board, int row, int col) {
  38. HashMap<Character, Integer> hashmap = new HashMap<Character, Integer>();
  39. for (int j = 0; j < board[0].length; j++) {
  40. if (board[row][j] != '.') {
  41. if (hashmap.containsKey(board[row][j]))
  42. return false;
  43. hashmap.put(board[row][j], 1);
  44. }
  45. }
  46.  
  47. hashmap = new HashMap<Character, Integer>();
  48. for (int i = 0; i < board.length; i++) {
  49. if (board[i][col] != '.') {
  50. if (hashmap.containsKey(board[i][col]))
  51. return false;
  52. hashmap.put(board[i][col], 1);
  53. }
  54. }
  55.  
  56. hashmap = new HashMap<Character, Integer>();
  57. int rowTemp = (row / 3) * 3;
  58. int colTemp = (col / 3) * 3;
  59.  
  60. for (int k = 0; k < 9; k++) {
  61. if (board[rowTemp + k / 3][colTemp + k % 3] != '.') {
  62. if (hashmap
  63. .containsKey(board[rowTemp + k / 3][colTemp + k % 3]))
  64. return false;
  65. hashmap.put(board[rowTemp + k / 3][colTemp + k % 3], 1);
  66. }
  67. }
  68. return true;
  69. }

Java for LeetCode 037 Sudoku Solver的更多相关文章

  1. LeetCode 037 Sudoku Solver

    题目要求:Sudoku Solver Write a program to solve a Sudoku puzzle by filling the empty cells. Empty cells ...

  2. 【leetcode】Sudoku Solver

    Sudoku Solver Write a program to solve a Sudoku puzzle by filling the empty cells. Empty cells are i ...

  3. leetcode 37. Sudoku Solver 36. Valid Sudoku 数独问题

    三星机试也考了类似的题目,只不过是要针对给出的数独修改其中三个错误数字,总过10个测试用例只过了3个与世界500强无缘了 36. Valid Sudoku Determine if a Sudoku ...

  4. Java [leetcode 37]Sudoku Solver

    题目描述: Write a program to solve a Sudoku puzzle by filling the empty cells. Empty cells are indicated ...

  5. leetcode 37 Sudoku Solver java

    求数独,只要求做出一个答案就可以. 刚开始对题意理解错误,以为答案是唯一的, 所以做了很久并没有做出来,发现答案不唯一之后,使用回溯.(还是借鉴了一下别人) public class Solution ...

  6. [LeetCode] 37. Sudoku Solver 求解数独

    Write a program to solve a Sudoku puzzle by filling the empty cells. A sudoku solution must satisfy  ...

  7. [leetcode]37. Sudoku Solver 解数独

    Write a program to solve a Sudoku puzzle by filling the empty cells. A sudoku solution must satisfy  ...

  8. LeetCode 37 Sudoku Solver(求解数独)

    题目链接: https://leetcode.com/problems/sudoku-solver/?tab=Description   Problem : 解决数独问题,给出一个二维数组,将这个数独 ...

  9. 037 Sudoku Solver 解数独

    写一个程序通过填充空格来解决数独.空格用 '.' 表示. 详见:https://leetcode.com/problems/sudoku-solver/description/ class Solut ...

随机推荐

  1. ztree设置节点checked

    1.根据id获取树的某个节点: var zTree = $.fn.zTree.getZTreeObj("mytree"); var node = zTree.getNodeByPa ...

  2. BZOJ-1975 魔法猪学院 K短路 (A*+SPFA)

    1975: [Sdoi2010]魔法猪学院 Time Limit: 10 Sec Memory Limit: 64 MB Submit: 1323 Solved: 433 [Submit][Statu ...

  3. 学习笔记--博弈组合-SG函数

    fye学姐的测试唯一的水题.... SG函数是一种游戏图每个节点的评估函数 具体定义为: mex(minimal excludant)是定义在整数集合上的操作.它的自变量是任意整数集合,函数值是不属于 ...

  4. 洛谷P2320 [HNOI2006]鬼谷子的钱袋

    https://www.luogu.org/problem/show?pid=2320#sub 题目描述全是图 数学思维,分治思想 假设总数为n 从n/2+1到n的数都可以用1~n的数+n/2表示出来 ...

  5. thinkphp ajax添加及删除

    写在前面的话:应客户需求需要给后台增加自助添加电影名称和链接的功能,添加后在微信前台能自动读取显示.开发步骤:1.由于是给后台添加一个增加电影及电影链接的功能,所以控制器在Admin下.在路径 App ...

  6. 静态内部类和Lazy<T>优雅实现单例

    public class Singleton { private Singleton() { } public static Singleton GetInstance() { return Conc ...

  7. 我和NLP的故事(转载)

    正值ACL录用结果发布,国内的老师和同学们又是一次大丰收,在这里再次恭喜所有论文被录用的老师和同学们!我人品爆发,也收获了自己硕士阶段的第二篇ACL论文.本来只是想单纯分享下自己中论文的喜悦,但没成想 ...

  8. iOS快速单例宏

    // 单例 #define DECLARE_SHARED_INSTANCE(className) \ + (className *)sharedInstance; #define IMPLEMENT_ ...

  9. MyEclipse代码提示快捷键和常用设置

    我使用的是MyEclipse 6.0版本,代码助手(content assist)的快捷键由 Alt + / 改成了 Ctrl + Space,恰好我的输入法快捷键也是 Ctrl + Space .造 ...

  10. 行为Behavior的使用

    原文地址:http://www.it610.com/article/4918541.htm 行为就是继承yii\base\behavior,可以绑定到任意yii\base\compent实例上,然后这 ...