Sudoku Solver
Write a program to solve a Sudoku puzzle by filling the empty cells.
Empty cells are indicated by the character '.'.
You may assume that there will be only one unique solution.
![]()
A sudoku puzzle...
![]()
...and its solution numbers marked in red.
做了这道题,对backtracking的理解又加深了一点点。
1 每个backtracking的题目,最好都有独立判断isValid的程序,这样架构清楚。同时,valid判断函数在这里可以稍微研究一下。只要当前要判断的位置上的数值和本行没有重复,本列没有重复,九宫格没有重复就可以。一旦重复立即返回,减少判断次数。
2 backtracking的递归函数,怎么能没有返回值呢?!因为要判断递归的方案正确与否,所以这里的递归一定是有返回值的(除非是combination那种没有正确错误概念的backtracking)!
3 可以考虑“先放置,再判断”的方案。比如这里,首先判断当前位置是否为空,如果为空,那么放置一个元素,检查它是否正确。如果正确,就继续进行下面的递归(也就是第29行 isValid&&solveSudoku的作用)。当函数返回错误之后,将刚刚的数值变为空,再进行下一次尝试即可。
4 所有的方案(k从1到9)完毕之后,应该返回错误,这个是不应该被忽略的。
5 最后一点需要注意的是,当i,j循环完毕之后,第36行应该返回true。这里实际上是最终/最底层的一次循环,表明已经解出了sudoku,返回true!切记切记,最终情况!
public class Solution {
public boolean solveSudoku(char[][] board) {
// Note: The Solution object is instantiated only once and is reused by each test case.
for (int i = 0; i < 9; i ++){
for (int j = 0; j < 9; j ++)
{
if (board[i][j] == '.')
{
for (int k = 1; k <= 9; k ++)
{
board[i][j] = (char)(k + '0');
if (isValid(board, i, j) && solveSudoku(board)){
return true;
}
board[i][j] = '.';
}
return false;
}
}
}
return true;
}
public boolean isValid(char[][] board, int x, int y){
int i = 0;
int j = 0;
for (; i < 9; i++)
if (i != x && board[i][y] == board[x][y])
return false;
for (; j < 9; j++)
if (j != y && board[x][j] == board[x][y])
return false;
for (i = 3 * (x / 3); i < 3 * (x / 3 + 1); i++)
for (j = 3 * (y / 3); j < 3 * (y / 3 + 1); j++)
if (i != x && j != y && board[i][j] == board[x][y])
return false;
return true;
}
}
第三遍:
public class Solution {
public boolean solveSudoku(char[][] board) {
for(int i = 0; i < 9; i ++)
for(int j = 0; j < 9; j ++)
if(board[i][j] == '.'){
for(int k = 1; k < 9; i ++){
board[i][j] = (char)(k + '0');
if(isValid(board, i , j) && solveSudoku(board)) return true;
board[i][j] = '.';
}
return false;
}
return true;
}
public boolean isValid(char[][] board, int a, int b){
for(int i = 0; i < 9; i ++){
if(i != a && board[a][b] == board[i][b]) return false;
if(i != b && board[a][b] == board[a][i]) return false;
}
for(int i = 3 * (a / 3); i < 3 * (a / 3) + 3; i ++)
for(int j = 3 * (b / 3); j < 3 * (b / 3) + 3; i ++)
if(i != a && j != b && board[i][j] == board[a][b]) return false;
return true;
}
}
Sudoku Solver的更多相关文章
- Leetcode 笔记 36 - Sudoku Solver
题目链接:Sudoku Solver | LeetCode OJ Write a program to solve a Sudoku puzzle by filling the empty cells ...
- LeetCode:Valid Sudoku,Sudoku Solver(数独游戏)
Valid Sudoku Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules. The Sudoku bo ...
- [leetcode]算法题目 - Sudoku Solver
最近,新加坡总理李显龙也写了一份代码公布出来,大致瞧了一眼,竟然是解数独题的代码!前几天刚刚写过,数独主要算法当然是使用回溯法.回溯法当时初学的时候在思路上比较拧,不容易写对.写了几个回溯法的算法之后 ...
- 【leetcode】Sudoku Solver
Sudoku Solver Write a program to solve a Sudoku puzzle by filling the empty cells. Empty cells are i ...
- [Leetcode][Python]37: Sudoku Solver
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 37: Sudoku Solverhttps://oj.leetcode.co ...
- leetcode 37. Sudoku Solver 36. Valid Sudoku 数独问题
三星机试也考了类似的题目,只不过是要针对给出的数独修改其中三个错误数字,总过10个测试用例只过了3个与世界500强无缘了 36. Valid Sudoku Determine if a Sudoku ...
- 【LeetCode】37. Sudoku Solver
Sudoku Solver Write a program to solve a Sudoku puzzle by filling the empty cells. Empty cells are i ...
- Valid Sudoku&&Sudoku Solver
Valid Sudoku Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules. The Sudoku bo ...
- LeetCode解题报告—— Reverse Nodes in k-Group && Sudoku Solver
1. Reverse Nodes in k-Group Given a linked list, reverse the nodes of a linked list k at a time and ...
- Leetcode之回溯法专题-37. 解数独(Sudoku Solver)
Leetcode之回溯法专题-37. 解数独(Sudoku Solver) 编写一个程序,通过已填充的空格来解决数独问题. 一个数独的解法需遵循如下规则: 数字 1-9 在每一行只能出现一次.数字 1 ...
随机推荐
- JAVA作业-1
编写一个程序,用户输入两个数,求出其加减乘除,并用消息框显示计算结果. import javax.swing.JOptionPane; // import class JOptionPane publ ...
- wix xslt for adding node
Using xslt to add new node item to wix source code. Original wix code: <Fragment> <Director ...
- mysql颠覆实战笔记(五)--商品系统设计(二):定时更新商品总点击量
继续回到沈老师的MYSQL颠覆实战,首先回顾下上一节课的内容,请大家会看下上节课写的存储过程. 打开prod_clicklog表, 我们只要把日期(不含时分秒)的部分存在数据库中, 如果同一日期有相同 ...
- 开始ubuntu 14.04 的装X模式---终端模式下中文输入,听歌,上irc 开启framebuffer看电影 截图
先上图吧 卡卡的全是在tty1 下的操作,看电影,听歌,截图 ,看图 ,上irc 等等,相当适合在小白面前装屁! 需要安装的软件: 为了能正常显示中文:安装fbterm sudo apt-get i ...
- Android混淆问题
最近做了2个项目,全部要混淆,刚接触,自己在网上找了还多资料,感觉各有千秋,自己总结了一下,第一次发帖,不喜勿喷.求各种指导!!! android应用程序的混淆打包规范 1.在工程文件project. ...
- 跨域名设置cookie或获取cookie
可以使用jquery里面的ajax中的jsonp的方式来访问就可以了.代码如下: $.ajax({ url: 'your url', data: {'xx' : 'xx', 'xx2' : 'xx2' ...
- 【转载】Powershell获取世纪互联Office365中所有用户的OWA时区
get-mailbox -resultsize unlimited | Get-MailboxRegionalConfiguration | select Identity,TimeZone | wh ...
- linux 文件在磁盘上的表示
基本构成 分区就是把磁盘像比萨饼一样切成大小一样的扇形, 当然分区大小不一样扇形的大小也就不一样 上下一串盘片中,相同半径的磁道所组成的一个圆柱型的环壁,就称为柱面 在柱面组中, 比较重要的两个结构是 ...
- ActiveMQ之Topic
与Queue不同,Topic实现的是发布/订阅模型,在下面的例子中,启动两个消费者共同监听一个Topic,然后循环给这个Topic发送多个消息. 例子: public class TopicTest ...
- 用Redis bitmap统计活跃用户、留存
Spool的开发者博客,描述了Spool利用Redis的bitmaps相关的操作,进行网站活跃用户统计工作. 原文:http://blog.getspool.com/2011/11/29/fast-e ...