LeetCode 37 Sudoku Solver(求解数独)

时间:2022-11-10 15:48:39

 
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. &lbrack;LeetCode&rsqb; 37&period; Sudoku Solver 求解数独

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

  2. &lbrack;leetcode&rsqb;37&period; Sudoku Solver 解数独

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

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

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

  4. &lbrack;LeetCode&rsqb; Sudoku Solver 求解数独

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

  5. Java &lbrack;leetcode 37&rsqb;Sudoku Solver

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

  6. &lbrack;leetcode 37&rsqb;sudoku solver

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

  7. leetcode 37 Sudoku Solver java

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

  8. &lbrack;Leetcode&rsqb;&lbrack;Python&rsqb;37&colon; Sudoku Solver

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

  9. LeetCode&colon;Valid Sudoku&comma;Sudoku Solver(数独游戏)

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

随机推荐

  1. 翻译:打造Edge渲染内核的浏览器

    最近开始了解UWP和Edge内核,在微软技术博客中找到一篇文章,主要是介绍Edge渲染内核使用技术.顺手翻译了一下.不对之处请斧正! Over the past several months, we ...

  2. Sparse Matrix Multiplication

    Given two sparse matrices A and B, return the result of AB. You may assume that A's column number is ...

  3. ubuntu12&period;04下安卓编译环境搭建总结

    前言:      因为工作需要,经常要编译安卓下的动态库,公司有已经搭建好环境的服务器,但是第一自己想自己搭建一下了解一个整个过程,另外,公司的服务器也经常出现问 题,导致编译不了,所以就想自己搭建环 ...

  4. 关于AngularJS学习整理---浅谈&dollar;scope&lpar;作用域&rpar; 新手必备!

    作为初次接触 AngularJS的新手,想要深层理解里面的内容短时间还是不可能的,所以标题写了浅谈字样,以下内容是参考各位大神以及相关书籍整理加个人理解,出现错误的地方请大家指正. $scope(作用 ...

  5. promisify&comma;promisifyAll&comma;promise&period;all实现原理

    1.promisify function toPrimisify (fn){ return function (...args){      return new Promise(function(r ...

  6. 14、JDBC-DbUtils-API

    DbUtils /** * DbUtils :提供如关闭连接.装载 JDBC 驱动等操作的工具类,里面方法都是静态的. * * public static void close(…) throws j ...

  7. oracle查询2G以上的表

    SELECT a.*, b.comments  FROM (SELECT OWNER,               SEGMENT_NAME,               SEGMENT_TYPE,  ...

  8. 八月&lpar;The Summer is Gone&rpar;观后感

    第一次看到这部电影时觉得很亲近,黑白画面,国企改革的背景,浓浓的儿时画面感,原谅我只是一个三十不到的人,可能我比较早熟,对八九十年代还有些记忆,更早以前也通过电视.音乐.书籍等了解过一些,而那些听过又 ...

  9. hdoj1171 Big Event in HDU(01背包 &vert;&vert; 多重背包)

    题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=1171 题意 老师有一个属性:价值(value).在学院里的老师共有n种价值,每一种价值value对应着 ...

  10. &lbrack;Arc062&rsqb; Painting Graphs with AtCoDeer

    [Arc062] Painting Graphs with AtCoDeer Description 给定一张N点M边的无向图,每条边要染一个编号在1到K的颜色.你可以对一张染色了的图进行若干次操作, ...