Java for LeetCode 037 Sudoku Solver
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的更多相关文章
- LeetCode 037 Sudoku Solver
题目要求:Sudoku Solver Write a program to solve a Sudoku puzzle by filling the empty cells. Empty cells ...
- 【leetcode】Sudoku Solver
Sudoku Solver Write a program to solve a Sudoku puzzle by filling the empty cells. Empty cells are i ...
- leetcode 37. Sudoku Solver 36. Valid Sudoku 数独问题
三星机试也考了类似的题目,只不过是要针对给出的数独修改其中三个错误数字,总过10个测试用例只过了3个与世界500强无缘了 36. Valid Sudoku Determine if a Sudoku ...
- Java [leetcode 37]Sudoku Solver
题目描述: Write a program to solve a Sudoku puzzle by filling the empty cells. Empty cells are indicated ...
- leetcode 37 Sudoku Solver java
求数独,只要求做出一个答案就可以. 刚开始对题意理解错误,以为答案是唯一的, 所以做了很久并没有做出来,发现答案不唯一之后,使用回溯.(还是借鉴了一下别人) public class Solution ...
- [LeetCode] 37. Sudoku Solver 求解数独
Write a program to solve a Sudoku puzzle by filling the empty cells. A sudoku solution must satisfy ...
- [leetcode]37. Sudoku Solver 解数独
Write a program to solve a Sudoku puzzle by filling the empty cells. A sudoku solution must satisfy ...
- LeetCode 37 Sudoku Solver(求解数独)
题目链接: https://leetcode.com/problems/sudoku-solver/?tab=Description Problem : 解决数独问题,给出一个二维数组,将这个数独 ...
- 037 Sudoku Solver 解数独
写一个程序通过填充空格来解决数独.空格用 '.' 表示. 详见:https://leetcode.com/problems/sudoku-solver/description/ class Solut ...
随机推荐
- ztree设置节点checked
1.根据id获取树的某个节点: var zTree = $.fn.zTree.getZTreeObj("mytree"); var node = zTree.getNodeByPa ...
- BZOJ-1975 魔法猪学院 K短路 (A*+SPFA)
1975: [Sdoi2010]魔法猪学院 Time Limit: 10 Sec Memory Limit: 64 MB Submit: 1323 Solved: 433 [Submit][Statu ...
- 学习笔记--博弈组合-SG函数
fye学姐的测试唯一的水题.... SG函数是一种游戏图每个节点的评估函数 具体定义为: mex(minimal excludant)是定义在整数集合上的操作.它的自变量是任意整数集合,函数值是不属于 ...
- 洛谷P2320 [HNOI2006]鬼谷子的钱袋
https://www.luogu.org/problem/show?pid=2320#sub 题目描述全是图 数学思维,分治思想 假设总数为n 从n/2+1到n的数都可以用1~n的数+n/2表示出来 ...
- thinkphp ajax添加及删除
写在前面的话:应客户需求需要给后台增加自助添加电影名称和链接的功能,添加后在微信前台能自动读取显示.开发步骤:1.由于是给后台添加一个增加电影及电影链接的功能,所以控制器在Admin下.在路径 App ...
- 静态内部类和Lazy<T>优雅实现单例
public class Singleton { private Singleton() { } public static Singleton GetInstance() { return Conc ...
- 我和NLP的故事(转载)
正值ACL录用结果发布,国内的老师和同学们又是一次大丰收,在这里再次恭喜所有论文被录用的老师和同学们!我人品爆发,也收获了自己硕士阶段的第二篇ACL论文.本来只是想单纯分享下自己中论文的喜悦,但没成想 ...
- iOS快速单例宏
// 单例 #define DECLARE_SHARED_INSTANCE(className) \ + (className *)sharedInstance; #define IMPLEMENT_ ...
- MyEclipse代码提示快捷键和常用设置
我使用的是MyEclipse 6.0版本,代码助手(content assist)的快捷键由 Alt + / 改成了 Ctrl + Space,恰好我的输入法快捷键也是 Ctrl + Space .造 ...
- 行为Behavior的使用
原文地址:http://www.it610.com/article/4918541.htm 行为就是继承yii\base\behavior,可以绑定到任意yii\base\compent实例上,然后这 ...