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. jquery获取所有被选中checkbox

    想要得到所有被选中的checkbox的value ,并且传给后台 var headers = ""; $('input[name="header"]:check ...

  2. 分享Db4o的便捷封装类源码

    导言 大家好,话说真是好久好久没写文章了,哈哈. 最近在写网站,个人对传统数据库天然抵触,感觉非常繁冗,即便是Entity Framework也过于庞杂了,Db4o这种轻量级且读写.配置都极其方便的新 ...

  3. Android框架之AndroidAnnotations基础

    一:开源网址 https://github.com/excilys/androidannotations/wiki 二:AndroidAnnotation特点 (1)依赖注入 可以注入 views, ...

  4. onethink和thinkphp3.2学习

    thinkphp发布3.2版本之后,也发布了一个简单的内容管理系统onthink,这样有助于理解thinkphp3.2的使用: 一.首先最关键的一点是thinkphp3.2中加入了命名空间的使用 什么 ...

  5. uploadify 后台动态传参数

    最近项目中用到上传控件,参数需要动态传参,经过查询总结了一下Uploadify 动态传参 jQuery(document).ready(function () { var ctrlid = getQu ...

  6. Hibernate简介

    Hibernate是一个面向JAVA环境的对象/关系数据库映射工具,用来把对象模型表示的对象映射到基于SQL的关系模型数据结构中去 ,Hibernate的目标主要是为了释放开发者通常的数据持久化相关的 ...

  7. 【C#】Excel做的数据表、SQLParameter代码生成工具

    转载请注明出处http://www.cnblogs.com/Vulpers/ 做了一个小的代码生成工具,用于新建数据表时能够快速生成一些重复性很高的代码,目前仅支持SqlServer数据库及C#语言, ...

  8. 用Backbone.js创建一个联系人管理系统(三)

    原文: Build a Contacts Manager Using Backbone.js: Part 3 欢迎回到这系列的教程,关注使用Backbone.js构建应用程序. 如果你还没看过第一,二 ...

  9. IIS7.5 由于 Web 服务器上的“ISAPI 和 CGI 限制”列表设置,无法提供您请求的页面

    IIS7.5中将一网站应用程序池托管管道模式改为经典后,网站页面打不开,错误信息: 引用内容 HTTP 错误 404.2 - Not Found由于 Web 服务器上的“ISAPI 和 CGI 限制” ...

  10. 遇到的sql关键字

    select count(1)  相当于  select count(*)  网上有比较差别的 菜鸟不用管