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实现如下:

static public void solveSudoku(char[][] board) {
int count = 0;
for (int i = 0; i < board.length; i++)
for (int j = 0; j < board[0].length; j++)
if (board[i][j] == '.')
count++;
dfs(board, count);
} public static int dfs(char[][] board, int count) {
if (count == 0)
return 0;
for (int i = 0; i < board.length; i++) {
for (int j = 0; j < board[0].length; j++) {
if (board[i][j] == '.') {
for (int k = 1; k <= 10; k++) {
if (k == 10)
return count;
board[i][j] = (char) ('0' + k);
if (!isValid(board, i, j))
board[i][j] = '.';
else {
count--;
count = dfs(board, count);
if (count == 0)
return count;
count++;
board[i][j] = '.';
}
}
}
}
}
return count;
} static public boolean isValid(char[][] board, int row, int col) {
HashMap<Character, Integer> hashmap = new HashMap<Character, Integer>();
for (int j = 0; j < board[0].length; j++) {
if (board[row][j] != '.') {
if (hashmap.containsKey(board[row][j]))
return false;
hashmap.put(board[row][j], 1);
}
} hashmap = new HashMap<Character, Integer>();
for (int i = 0; i < board.length; i++) {
if (board[i][col] != '.') {
if (hashmap.containsKey(board[i][col]))
return false;
hashmap.put(board[i][col], 1);
}
} hashmap = new HashMap<Character, Integer>();
int rowTemp = (row / 3) * 3;
int colTemp = (col / 3) * 3; for (int k = 0; k < 9; k++) {
if (board[rowTemp + k / 3][colTemp + k % 3] != '.') {
if (hashmap
.containsKey(board[rowTemp + k / 3][colTemp + k % 3]))
return false;
hashmap.put(board[rowTemp + k / 3][colTemp + k % 3], 1);
}
}
return true;
}

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. SP*

    1.PS1——默认提示符 root@tcx2250-14:/etc# echo $PS1\u@\h:\w\$ \u是用户名 \h是主机名 \w是当前目录的完整路径.请注意当你在主目录下的时候,如上面所 ...

  2. 37.Activity之间的转换以及数据的传递(Intent)学习

      Intent简介:                                                                                在一个Androi ...

  3. js阻止表单重复提交

    //校验表单的数据 function newFatherModuleVerify() { var moduelName = $('#fatherModule_moduelName').val(); a ...

  4. Ext comboBox的remote和local的区别

    remote模式下不能使用模糊查询的功能 而local模式下可以实现模糊查询的功能 如果非要实现模糊查询的功能,最好就是提前把数据查询出来,缓存到本地,然后再用local模式 且,改个属性,改成可编辑 ...

  5. newinstance和new有什么区别

    用newInstance与用new是区别的,区别在于创建对象的方式不一样,前者是使用类加载机制,那么为什么会有两种创建对象方式?这个就要从可伸缩.可扩展,可重用等软件思想上解释了.Java中工厂模式经 ...

  6. Ubuntu系统启动过程详解

    作者:杨硕,华清远见嵌入式学院讲师. 一. Ubuntu的启动流程 ubuntu的启动流程和我们熟知的RedHat的启动方式有所区别. RedHat的启动过程如下图: 这是我们熟知的linux启动流程 ...

  7. Selenium2+python自动化13-Alert

    不是所有的弹出框都叫alert,在使用alert方法前,先要识别出它到底是不是alert.先认清楚alert长什么样子,下次碰到了,就可以用对应方法解决.alert\confirm\prompt弹出框 ...

  8. C#实现自动升级(附源码)

    http://blog.csdn.net/zhuweisky/article/details/50439386 OAUS

  9. WPF 屏蔽Alt+F4强制退出

    if (e.KeyStates == Keyboard.GetKeyStates(Key.F4) && Keyboard.Modifiers == ModifierKeys.Alt) ...

  10. 在表单(input)中id和name的区别

    但是name在以下用途是不能替代的: 1. 表单(form)的控件名,提交的数据都用控件的name而不是id来控制.因为有许多name会同时对应多个控件,比如checkbox和radio,而id必须是 ...