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. 细说;(function ($, undefined){ })(jQuery); 的使用

    1. 对于function前面的分号(;)的使用:使用分号的目的是为了防止多个文件压缩合并时,以为其他文件最后一行语句没加分号,而引起合并后的语法错误. 2. (function ($, undefi ...

  2. Linux命令(ntp)

    NTP时间同步 下载ntp软件包 root@rgw01:~# apt-get install ntp 调整ntp server时间 root@rgw01:~# date Mon Dec 1 17:02 ...

  3. Web页面报错: Eval()、XPath() 和 Bind() 这类数据绑定方法只能在上下文中使用

    可以使用string.formt来避免出错. 如: <%# Convert.ToInt32(DataBinder.Eval(Container.DataItem, "Status&qu ...

  4. c# datagridview 中DataSource的使用总结

    由于当前项目的窗体更新使用的是订阅事件的方式.其中有个datagridview 动态显示统计数据的列表框.本来想用textbox显示,但不规则,看起来也不美观,改由dgv显示. 我没打算用改dgv表的 ...

  5. [python]自动化将markdown文件转成html文件

    *:first-child { margin-top: 0 !important; } body>*:last-child { margin-bottom: 0 !important; } /* ...

  6. python第三方库学习(2):requests

    Make a Request r = requests.get('https://github.com/timeline.json') Passing Parameters In URLspayloa ...

  7. android图片的scaleType属性

    scaleType 保持图片原有大小 scaleType="fitXY"填满盒子 scaleType="fitStart"保持纵横比缩放放在左上角 scaleT ...

  8. css之滚动条

    overflow:auto; overflow-x:auto; overflow-y:auto;

  9. 【CMD】日常总结

    命令脚本可以提升工作效率,之前用过也写过一些脚本,但时间一长就忘记了.写篇随笔记录一下,随用随记哈. 调用程序 //切换到某个路径下 cd D:\Glodon\GDW\GDW\Release\Bin ...

  10. Jquery打造的类似新浪微博@提醒功能

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...