[leetcode]算法题目 - Sudoku Solver
最近,新加坡总理李显龙也写了一份代码公布出来,大致瞧了一眼,竟然是解数独题的代码!前几天刚刚写过,数独主要算法当然是使用回溯法。回溯法当时初学的时候在思路上比较拧,不容易写对。写了几个回溯法的算法之后心里总算有了点底。回溯法的代码一般都是长成下面这样子:
void backtracking(int[] arr, int boundary, int current, int[] result)
{
if(current>=boundary) //到达终止条件
{
//判断result是否符合要求
//如果符合,记录结果,即result数组的值 return;
} for(int i=0; i<arr.length;i++)
{
STEP 1: //从arr中取出第i个元素
STEP 2: //用第i个元素加入到result中
STEP 3: backtracking(arr, boundary, current+1, result); //进入下一步探索
STEP 4: //把第i个元素从result中拿出来
STEP 5: //把第i个元素再放回arr中去
}
}
回溯法代码特点非常鲜明,一上来先判断递归的终止条件,到达终止条件,就检查结果是否合格,合格就记录下来。而回溯法的精髓就在下面的for循环中,一共有5个步骤,后两个步骤刚好是前两个步骤的反操作,因此才被成为‘回溯’。我们可以认为,回溯法就是暴力(brute force)搜索的一种优化方式。
扯了点闲话,来看Sudoku Solver题:
/*************************Question**************************
* 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.
*
* 5 3 . . 7 . . . .
* 6 . . 1 9 5 . . .
* . 9 8 . . . . 6 .
* 8 . . . 6 . . . 3
* 4 . . 8 . 3 . . 1
* 7 . . . 2 . . . 6
* . 6 . . . . 2 8 .
* . . . 4 1 9 . . 5
* . . . . 8 . . 7 9
* A sudoku puzzle...
* 5 3 4 6 7 8 9 1 2
* 6 7 2 1 9 5 3 4 8
* 1 9 8 3 4 2 5 6 7
* 8 5 9 7 6 1 4 2 3
* 4 2 6 8 5 3 7 9 1
* 7 1 3 9 2 4 8 5 6
* 9 6 1 5 3 7 2 8 4
* 2 8 7 4 1 9 6 3 5
* 3 4 5 2 8 6 1 7 9
*
* ...and its solution.
***********************************************************/
我的代码如下:
bool isValid(char *board[], int row, int column, char number){
for(int i = ; i < ; i++){
if(board[row][i] == number){
return false;
}
if(board[i][column] == number){
return false;
}
}
for (int i = ; i < ; i++){
int a = (row / ) * + i / ;
int b = (column / ) * + i % ;
if(board[a][b] == number){
return false;
}
}
return true;
}
bool sudokuSolution(char *board[], int row, int column){
if(row >= ){
return true;
}
if(board[row][column] != '.'){
if(column >= ){
return sudukuSolution(board, row+, );
}else {
return sudukuSolution(board, row, column+);
}
}
for(int i=; i<;i++){
if(isValid(board, row, column, (char)(''+i))){
board[row][column] = (char)(''+i);
if(column >= && sudukuSolution(board, row+, )){
return true;
}else if (sudukuSolution(board, row, column +)){
return true;
}
board[row][column] = '.';
}
}
return false;
}
void solveSudoku(char *board[]){
if(sudokuSolution(board, , )){
//successfully find the solution
} else {
// cannot find a solution
}
return;
}
代码使用C语言编写,其中bool sudokuSolution函数就很明显有回溯法的函数结构。
[leetcode]算法题目 - Sudoku Solver的更多相关文章
- LeetCode算法题目解答汇总(转自四火的唠叨)
LeetCode算法题目解答汇总 本文转自<四火的唠叨> 只要不是特别忙或者特别不方便,最近一直保持着每天做几道算法题的规律,到后来随着难度的增加,每天做的题目越来越少.我的初衷就是练习, ...
- Leetcode 笔记 36 - Sudoku Solver
题目链接:Sudoku Solver | LeetCode OJ Write a program to solve a Sudoku puzzle by filling the empty cells ...
- [Leetcode][Python]37: Sudoku Solver
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 37: Sudoku Solverhttps://oj.leetcode.co ...
- 【LeetCode】37. Sudoku Solver
Sudoku Solver Write a program to solve a Sudoku puzzle by filling the empty cells. Empty cells are i ...
- [leetcode]算法题目 - Reverse Nodes in k-Group
Given a linked list, reverse the nodes of a linked list k at a time and return its modified list. If ...
- leetcode problem 37 -- Sudoku Solver
解决数独 Write a program to solve a Sudoku puzzle by filling the empty cells. Empty cells are indicated ...
- LeetCode OJ:Sudoku Solver(数独游戏)
Write a program to solve a Sudoku puzzle by filling the empty cells. Empty cells are indicated by th ...
- 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
July 19, 2015 Problem statement: Write a program to solve a Sudoku puzzle by filling the empty cells ...
随机推荐
- spring ehcache 页面、对象缓存
一.Ehcache基本用法 CacheManager cacheManager = CacheManager.create(); // 或者 cacheManager = CacheManager.g ...
- android-xxxx must implement the inherited abstract method报错
public class ContactMainFragment extends Fragment implements OnClickListener { 提示:ContactMainFragmen ...
- 初涉Linux ----------> Ubuntu15.04的安装与美化
“你玩 Linux 吗?” “什么 Linux ?” “你连 Linux 都不知道?还说是学计算机的呢...” “干嘛要用 Linux 啊?windows多好,Linux?没兴趣” 一. 前言 ...
- [转]ASP.NET MVC4+BootStrap 实战(一)
本文转自:http://leelei.blog.51cto.com/856755/1587301 好久没有写关于web开发的文章了,进到这个公司一直就是winform和Silverlight,实在是没 ...
- Solrj和Solr DIH索引效率对比分析
测试软件环境: 1.16G windows7 x64 32core cpu . 2.jdk 1.7 tomcat 6.x solr 4.8 数据库软件环境: 1.16G windows7 x64 ...
- POJ1837 Balance[分组背包]
Balance Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 13717 Accepted: 8616 Descript ...
- Linux强化论:15步打造一个安全的Linux服务器
Linux强化论:15步打造一个安全的Linux服务器 Alpha_h4ck2016-11-30共28761人围观 ,发现 8 个不明物体专题系统安全 可能大多数人都觉得Linux是安全的吧?但我要告 ...
- vijos1431[noip2007]守望者的逃离(背包动规)
描述 恶魔猎手尤迪安野心勃勃,他背叛了暗夜精灵,率领深藏在海底的娜迦族企图叛变.守望者 在与尤迪安的交锋中遭遇了围杀,被困在一个荒芜的大岛上.为了杀死守望者,尤迪安开始对这 个荒岛施咒,这座岛很快就会 ...
- Android中分页滑动实现总结
手机的屏幕相对较小,因此会出现当有多项内容需要展示而不得不进行分页的情况.例如手机桌面的应用图标的展示.一般一屏可以显示4*4=16个小方块形的应用程序,可以通过左右滑动进行屏幕的选择.而Androi ...
- iOS 中 const static extern 关键字总结
在看一些高手所写的代码时,总是可以看到我们小白平常不用的关键字,一次,两次,三次,不能总是不明不白,现在总结一下日常开发中常用的关键字的作用: 关键字const/static/extern的释义和用法 ...