最近,新加坡总理李显龙也写了一份代码公布出来,大致瞧了一眼,竟然是解数独题的代码!前几天刚刚写过,数独主要算法当然是使用回溯法。回溯法当时初学的时候在思路上比较拧,不容易写对。写了几个回溯法的算法之后心里总算有了点底。回溯法的代码一般都是长成下面这样子:

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的更多相关文章

  1. LeetCode算法题目解答汇总(转自四火的唠叨)

    LeetCode算法题目解答汇总 本文转自<四火的唠叨> 只要不是特别忙或者特别不方便,最近一直保持着每天做几道算法题的规律,到后来随着难度的增加,每天做的题目越来越少.我的初衷就是练习, ...

  2. Leetcode 笔记 36 - Sudoku Solver

    题目链接:Sudoku Solver | LeetCode OJ Write a program to solve a Sudoku puzzle by filling the empty cells ...

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

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

  4. 【LeetCode】37. Sudoku Solver

    Sudoku Solver Write a program to solve a Sudoku puzzle by filling the empty cells. Empty cells are i ...

  5. [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 ...

  6. leetcode problem 37 -- Sudoku Solver

    解决数独 Write a program to solve a Sudoku puzzle by filling the empty cells. Empty cells are indicated ...

  7. LeetCode OJ:Sudoku Solver(数独游戏)

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

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

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

  9. Leetcode: Sudoku Solver

    July 19, 2015 Problem statement: Write a program to solve a Sudoku puzzle by filling the empty cells ...

随机推荐

  1. mysql-1

    接触mysql已经一年多了,但是平时很少用到,仅限于安装部署,最近在学习运维开发,需要用到数据库,于是买了一本mysql必知必会,给自己一个两个星期的时间,学完这本书, 写这一系列的博客,就是记录学习 ...

  2. 利用 druid 解析器解析SQL

    最近参与一个开源项目,一个功能的实现,用到了 druid 解析器来解析SQL,记录下如果使用 druid 来解析SQL,实现对SQL的拦截改写. 1. 对 insert 语句进行解析: private ...

  3. 关于InnoDB的Next-Key lock

    最近一段时间在准备新员工培训的材料,本来打算介绍介绍概念就OK的,但是既然写了事务的章节,就特别想介绍一下锁,介绍了锁,就忍不住想介绍一下Next-Key Lock. 大家知道,标准的事务隔离级别有R ...

  4. x01.os.17: 换心术

    在 linux 中编译 linux, 于是 linux 便有了再生能力.这不同于自然界的缓慢进化,可用神速来形容.—— 和强大的 windows 相抗衡,便是证明! 我在 ubuntu 中的编译方法如 ...

  5. 深入理解linux系统的目录结构(总结的非常详细)

    对于每一个Linux学习者来说,了解Linux文件系统的目录结构,是学好Linux的至关重要的一步.,深入了解linux文件目录结构的标准和每个目录的详细功能,对于我们用好linux系统只管重要,下面 ...

  6. javaScript事件(二)事件处理程序

    一.事件 二.事件流 以上内容见:javaScript事件(一)事件流 三.事件处理程序 前面提到,事件是用户或浏览器自身执行的某种动作,如click,load和mouseover都是事件的名字.响应 ...

  7. plain framework 1 参考手册 入门指引之 代码风格

    代码风格 介绍 介绍 框架自身采用了google的C++风格,作者也鼓励在你的应用中使用此风格,有关此风格你可以查阅相关资料了解.下面是一段plain framework中的代码,以便大家参考: 你可 ...

  8. 译:Google的大规模集群管理工具Borg(一)------ 用户视角的Borg特性

    概述 Google的Borg系统是一个集群管理工具,在它上面运行着成千上万的job,这些job来自许许多多不同的应用,并且跨越多个集群,而每个集群又由大量的机器构成. Borg通过组合准入控制,高效的 ...

  9. Android 之窗口小部件详解--App Widget

    Android 之窗口小部件详解--App Widget  版本号 说明 作者 日期  1.0  添加App Widge介绍和示例  Sky Wang 2013/06/27        1 App ...

  10. Spring AOP 5种通知与java动态代理

    接口,要求为每个方法前后添加日志  @Component("arithmeticCalculator") public class ArithmeticCalculatorImpl ...