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. python学习之——安装Beautifulsoup、requests、lxml

    安装Beautiful soup: 1.下载安装包,解压到python的安装目录: 2.cmd 进入安装包解压后的存放位置: 3.使用命令:python  setup.py build   , pyt ...

  2. mount: unknown filesystem type 'ntfs'

    mount: unknown filesystem type 'ntfs' 问题描述 # mount –t ntfs /dev/sdc1 /mnt/usb2 mount: unknown filesy ...

  3. About memories in ASIC FPGA

    1. Write first | Read First | No Change区别在于:en & wr的时候,dout是什么,三种case对应于: dout = din; dout = mem ...

  4. 传统B2B中小型企业如何做好全网营销

    优网特独创全网营销服务理念,全网营销即以企业网站推广为核心,通过SEO.SEM.BBS营销.博客营销.微营销.即时通讯营销.网络口碑营销.视频营销.邮件营销.SNS营销等网络营销手段,全面提升企业网站 ...

  5. 学习总结relative和absolute

    之前对于absolute和relative不了解,现在整理 先设置relative再设置absolute 因为父不设置relative 那么子会向上寻找祖先元素,看是否设置relative.如果有则相 ...

  6. iOS TableView如何刷新指定的cell或section

    指定的section单独刷新 NSIndexSet *indexSet=[[NSIndexSet alloc]initWithIndex:indexPath.row]; [tableview relo ...

  7. 特殊的ASCII码对应的字符

    Special Characters " " " quotation mark u+0022 ISOnum p:before { content:"\0022& ...

  8. 搭建selenium自动化环境步骤

    1.下载pythonhttps://www.python.org/downloads/2.安装2.X或者3.X3.添加环境变量python和pip(与python一起安装)4.下载setuptools ...

  9. easyui自定义标签 datagrid edit combobox 手动输入保存不上问题解决办法

    使用onEndEdit事件(该事件可以获取到editor对象,onAfterEdit事件获取不到Editor对象) 通过editor拿到输入数据并保存. int ci = 0; for(Column ...

  10. C# 模拟上传图片

    上传图片的格式一定要按规定的写,不然没办法正确上传的. 我在上传的时候就是值前面没有空一行,导致上传不成功,很纠结的错误. 我要模拟的是一个FLASH的上传控件,我开始用HttpAnalyze抓包是抓 ...