Problem : 解决数独问题,给出一个二维数组,将这个数独进行求解。
 
思路:
  1. 嵌套循环,三层循环体,每一行,每一列,填入从1到9的数字。判断填入之后是否合理
  2. 判断数独是否合理的函数
 
参考代码: 
 
package leetcode_50;

/***
*
* @author pengfei_zheng
* 求解数独问题
*/
public class Solution37 {
public static void solveSudoku(char[][] board) {
if(board==null ||board.length ==0)
return;
else
solve(board);
} private static boolean solve(char[][] board) {
for(int i = 0 ; i < 9;i++){
for(int j = 0 ; j < 9;j++){
if(board[i][j]=='.'){
for(char c = '1'; c<='9';c++){
if(isValid(board,i,j,c)){
board[i][j]=c;
if(solve(board))
return true;
else
board[i][j]='.';
}
}
return false;
}
}
}
return true;
} private static boolean isValid(char[][] board, int row, int column, char c) {
for(int i = 0 ; i < 9; i ++){
if(board[row][i]==c) return false;
if(board[i][column]==c) return false;
if(board[3 * (row / 3) + i / 3][ 3 * (column / 3) + i % 3] != '.' &&
board[3 * (row / 3) + i / 3][3 * (column / 3) + i % 3] == c) return false; //check 3*3 block
}
return true;
}
public static void main(String[]args){
long start = System.currentTimeMillis();
char[][] board={{'8','.','.','.','.','.','.','.','.'},
{'.','.','3','6','.','.','.','.','.'},
{'.','7','.','.','9','.','2','.','.'},
{'.','5','.','.','.','7','.','.','.'},
{'.','.','.','.','4','.','7','.','.'},
{'.','.','.','1','.','5','.','3','.'},
{'.','.','1','.','.','.','.','6','8'},
{'.','.','8','5','.','.','.','1','.'},
{'.','9','.','.','.','.','4','.','.'}
};
solveSudoku(board);
long end = System.currentTimeMillis();
for(int i = 0 ; i < 9 ; i ++){
for(int j = 0 ; j < 9 ;j ++){
System.out.print(board[i][j]+" ");
}
System.out.println();
}
System.out.println("耗时: "+ (double)(end-start)/1000+" s");
}
}

LeetCode 37 Sudoku Solver(求解数独)的更多相关文章

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

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

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

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

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

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

  4. [LeetCode] Sudoku Solver 求解数独

    Write a program to solve a Sudoku puzzle by filling the empty cells. Empty cells are indicated by th ...

  5. Java [leetcode 37]Sudoku Solver

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

  6. [leetcode 37]sudoku solver

    1 题目: 根据给出的数独,全部填出来 2 思路: 为了做出来,我自己人工做了一遍题目给的数独.思路是看要填的数字横.竖.子是否已经有1-9的数字,有就剔除一个,最后剩下一个的话,就填上.一遍一遍的循 ...

  7. leetcode 37 Sudoku Solver java

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

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

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

  9. LeetCode:Valid Sudoku,Sudoku Solver(数独游戏)

    Valid Sudoku Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules. The Sudoku bo ...

随机推荐

  1. CSS3小清新下拉菜单 简易大方

    之前有分享过几款CSS3菜单和jQuery菜单,像这款HTML5/CSS3自定义下拉框 3D卡片折叠动画3D效果非常华丽,这次要分享的这款相对比较简单,很适合用在用户的操作面板上.先来看看效果图: 怎 ...

  2. yii2 页面渲染方法解析

    render渲染.renderPartial渲染部分.renderContent.renderAjax.renderFile ① render显示view和layout ② renderPartial ...

  3. linux内置软件安装命令

    yum -y install epel-release

  4. ubuntu 12.04和Windows 7双系统的安装方法

    ubuntu是一个操作系统,和安卓一样同是linux内核下的子民,所以来这贡献一下,也求JW放出安卓4.0原生正式版,我只想要这个. 前几天发布了ubuntu12.04LTS 版本,兴奋ing. 看到 ...

  5. [Learn AF3]第四章 App framework组件之Button

    Button    组件名称:Button     是否js控件:否     使用说明:如果说panel组件是af3的“核心(heart of the ui)”,那么Button就是af中的五虎上将之 ...

  6. Thinkphp5 多图上传

    html代码 <div class="content" id="content_list"> <!-- 上传部分 --> <for ...

  7. php json_encode 返回false

    今天用ajax请求不到数据,发现问题出现在最后一句 echo json_encode($data); var_dump(json_encode($data));输出false 使用json_last_ ...

  8. Linux的wget命令

    wget是linux最常用的下载命令, 一般的使用方法是: wget + 空格 + 要下载文件的url路径 例如: # wget http://www.linuxsense.org/xxxx/xxx. ...

  9. Centos7以上的版本 mysql 无法启动,无法停止问题

    service mysqld start 始终提示如下: Failed to issue method call: Unit mysqld.service failed to load: No suc ...

  10. php单元测试断言方法

    1.assertArrayHasKey() 用法:$this->assertArrayHasKey('foo', ['bar' => 'baz']); 等同于array_key_exist ...