[leetcode 37]sudoku solver
1 题目:
根据给出的数独,全部填出来
2 思路:
为了做出来,我自己人工做了一遍题目给的数独。思路是看要填的数字横、竖、子是否已经有1-9的数字,有就剔除一个,最后剩下一个的话,就填上。一遍一遍的循环,直到填完为止。
后来发现,这个思路只能解决部分数独。还有部分数独是需要回溯的,比如,这个位置只能填3或5,那么就需要先填上3,看看能否继续填下去,不能的话,再回过来填5。
想了半天,想不出来,把别人的backtracking看懂了,写出来了。。
3 代码:
自己的:
Hashtable<Integer, List<Character>> table;
int row;
int column;
int totalNum; public void solveSudoku(char[][] board) {
if (board.length == ) {
return;
} row = board.length;
column = board[].length;
totalNum = row * column;
table = new Hashtable<Integer, List<Character>>(totalNum);
for (int i = ; i < row; i++) {
for (int j = ; j < column; j++) {
if (board[i][j] == '.') {
List<Character> validNum = new ArrayList<Character>();
Character[] nums = {'','','','','','','','',''};
validNum.addAll(Arrays.asList(nums));
table.put(i*row+j, validNum);
}else { }
}
} fillBoard(board);
} private void fillBoard(char[][] board)
{
for (int i = ; i < row; i++) {
System.out.println();
for (int j = ; j < column; j++) {
int index = i*column + j;
if (table.containsKey(index)) {
// 判断剩余的数字
calRemaingNum(i,j,index,board);
} }
}
System.out.println("++++++++++++++++++++++++++++++++++++++");
if (isSuccess()) {
return;
}
fillBoard(board);
} private void calRemaingNum(int i, int j, int calIndex,char[][] board)
{
//row
for(int k=; k< column; k++){
if (board[i][k] != '.') {
table.get(calIndex).remove((Character)board[i][k]);
}
} //column
for (int k = ; k < row; k++) {
if (board[k][j] != '.') {
table.get(calIndex).remove((Character)board[k][j]);
}
} //square
int m = i / ;
int n = j / ;
for (int k = ; k < ; k++) {
for (int k2 = ; k2 < ; k2++) {
if (board[m*+k][n*+k2] != '.') {
table.get(calIndex).remove((Character)board[m*+k][n*+k2]);
}
}
}
System.out.println("i=" + i + " , j = " + j + ",index = " + calIndex + " ++ " + table.get(calIndex));
if (table.get(calIndex).size() == ) {
board[i][j] = table.get(calIndex).get();
table.remove(calIndex);
}
} private boolean isSuccess(){
return table.size() == ;
}
别人的
public void solveSudoku(char[][] board) {
if (board==null || board.length == ) {
return;
}
fillboard(board);
}
private boolean fillboard(char[][] board){
for (int i = ; i < board.length; i++) {
for (int j = ; j < board[].length; j++) {
if(board[i][j] == '.'){
for(char ch = ''; ch <= ''; ch++){
if (isValid(i, j, ch, board)) {
System.out.println("ok, i = " + i + ", j = " + j + " , value = " + ch);
board[i][j] = ch;
if (fillboard(board)) {
return true;
}else {
board[i][j] = '.';
}
}
}
return false;
}
}
}
return true;
}
private boolean isValid(int row, int column, char value, char[][] board){
// row
for (int i = ; i < board.length; i++) {
if (board[row][i] == value) {
return false;
}
}
// column
for (int i = ; i < board[].length; i++) {
if (board[i][column] == value) {
return false;
}
}
// square
int m = row / ;
int n = column / ;
for (int k = ; k < ; k++) {
for (int k2 = ; k2 < ; k2++) {
if (board[m*+k][n*+k2] == value) {
return false;
}
}
}
return true;
}
[leetcode 37]sudoku solver的更多相关文章
- leetcode 37. Sudoku Solver 36. Valid Sudoku 数独问题
三星机试也考了类似的题目,只不过是要针对给出的数独修改其中三个错误数字,总过10个测试用例只过了3个与世界500强无缘了 36. Valid Sudoku Determine if a Sudoku ...
- [LeetCode] 37. Sudoku Solver 求解数独
Write a program to solve a Sudoku puzzle by filling the empty cells. A sudoku solution must satisfy ...
- 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 解数独
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 : 解决数独问题,给出一个二维数组,将这个数独 ...
- leetcode 37 Sudoku Solver java
求数独,只要求做出一个答案就可以. 刚开始对题意理解错误,以为答案是唯一的, 所以做了很久并没有做出来,发现答案不唯一之后,使用回溯.(还是借鉴了一下别人) public class Solution ...
- [Leetcode][Python]37: Sudoku Solver
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 37: Sudoku Solverhttps://oj.leetcode.co ...
- 【LeetCode】37. Sudoku Solver
Sudoku Solver Write a program to solve a Sudoku puzzle by filling the empty cells. Empty cells are i ...
- 【leetcode】Sudoku Solver
Sudoku Solver Write a program to solve a Sudoku puzzle by filling the empty cells. Empty cells are i ...
随机推荐
- js的form基础知识点
在HTML 中,表单是由<form>元素来表示的,而在JavaScript 中,表单对应的则是HTMLForm-Element 类型.HTMLFormElement 继承了HTMLElem ...
- c语言插入排序
对于小规模输入,插入排序是一种非常快速的排序算法,且原理简单,结构紧凑. 插入排序的原理:从序列中第二个数A开始,将A,插入前面已经排好的序列中,形成一个新的排序好的序列,以此类推到最后一个元素. 参 ...
- [转] Oracle数据库备份与恢复 - 增量备份
转:http://blog.csdn.net/pan_tian/article/details/46780929 RMAN一个强大的功能是支持增量备份,增量备份中心思想就是减少备份的数据量,我们不 ...
- CSS布局(一)
本节内容: 没有布局 display属性 margin:auto; max-width 盒模型 没有布局 如果想把所有内容都塞进一栏里,那么不用设置任何布局也是可以的.然而,如果用户把浏览器窗口调整的 ...
- android Activity类中的finish()、onDestory()和System.exit(0) 三者的区别
android Activity类中的finish().onDestory()和System.exit(0) 三者的区别 Activity.finish() Call this when your a ...
- 一起买beta版PHP单元测试
一起买beta版PHP单元测试 测试目的 保证代码质量,对各个单元进行测试,可以有效地保证代码的可靠性,让模块在与别的模块整合时出现更少的错误. 单元描述 完成帖子接口 将"正在进行&q ...
- 3515 如何调试AT指令、查看拨号模块的打印
冯sir给了一个在设备运行的程序serial,运行起来后可以敲指令输入AT指令,具体步骤如下: 1.将serial文件从U盘拷贝到设备里,U盘在设备系统下的目录是/stm/udisk/0/,但是注意设 ...
- wpf 空白汉字占位符
<TextBlock xml:space="preserve" Text="主 编" /> 表示一个汉字占位符
- 数据类型安全验证都交给TryParse吧
C# 网站开发中 往往在编写后台代码中遇到从字符类型转换到其他类型.其实无需在单独验证字符串长度,是否为空等工作.直接用Tryparse转换,如果转换失败说明字符串的格式等有误.成功则再进一步验证转换 ...
- sys.dm_tran_locks,
sys.dm_tran_locks 返回有关当前活动的锁管理器资源的信息.向锁管理器发出的已授予锁或正等待授予锁的每个当前活动请求分别对应一行. 列名 数据类型 说明 resource_type nv ...