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的更多相关文章

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

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

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

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

  3. Java [leetcode 37]Sudoku Solver

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

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

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

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

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

  6. leetcode 37 Sudoku Solver java

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

  7. [Leetcode][Python]37: Sudoku Solver

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 37: Sudoku Solverhttps://oj.leetcode.co ...

  8. 【LeetCode】37. Sudoku Solver

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

  9. 【leetcode】Sudoku Solver

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

随机推荐

  1. 09-JAVA中的异常处理

    1. 程序执行结果: 也就是它根本就没抛出异常,更别提捕获异常了.那么,为什么会这样呢? 原来, 如上面程序展示,程序运行到k=i/j;的时候,就会直接终止,根本就不会运行到监视的程序,更不会运行到捕 ...

  2. 【待整理】Linux故障排查

    ethtool -i eth0dmidecode -i eth3more /var/log/meclogmore /etc/issue

  3. 修改了chrome的官方的有道词典插件,添加了生词本的功能

    项目地址+导入教程 https://github.com/cclient/chrome-extensions-youdaowithwordnode

  4. HTTP Client工具类

    HTTP Client工具类: import org.apache.http.Header;import org.apache.http.HttpEntity;import org.apache.ht ...

  5. css实现并列效果

    <a href="#" class="mj-picList"> <div class="mj-picList-pic" s ...

  6. MFC学习笔记

    获取窗口句柄 FindWindow               根据窗口名获取 GetSafehWnd                取你程序所在窗口类的句柄 GetActiveWindow     ...

  7. String,StringBuffer

    String类代表不可变的字符序列. String s1 = "hello"; String s2 = "hello"; s1 == s2  ==> tr ...

  8. 20145316&20145229实验五:网络通信

    20145316&20145229实验五:网络通信 结对伙伴:20145316 博客链接:http://www.cnblogs.com/xxy745214935/p/6130897.html

  9. [11]APUE:(文件)记录锁

    [a] 概念 建议锁:在遵循相同记录锁规则的进程间生效,通常用于保证某个程序自身多个进程间的数据一致性 强制锁:意在保证所有进程间的数据一致性,但不一定有效:如不能应对先 unlink 后建立同名副本 ...

  10. 安卓奇葩问题之.so库加载不了

    真是哔了狗了. 今天突然遇到一个问题:之前用第三方的密码控件,给了一个.so库文件.然后我就放在了/jniLibs/armeabi目录下. 运行,一切都很OK. 然后重点来了.N天之后的今天,突然打包 ...