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

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. SQL Server:数据库角色

    除了上述固定数据库角色之外,还有一种特殊的固定数据库角色,名为public. 数据库的每个合法用户都必须属于public角色,它为数据库中的用户提供了所有默认权限. 一般情况下,public角色允许用 ...

  2. BootStrap入门教程 (一)

    BootStrap入门教程 (一)   2011年,twitter的"一小撮"工程师为了提高他们内部的分析和管理能力,用业余时间为他们的产品构建了一套易用.优雅.灵活.可扩展的前端 ...

  3. 网页实时聊天之js和jQuery实现ajax长轮询

    众所周知,HTTP协议是无状态的,所以一次的请求都是一个单独的事件,和前后都没有联系.所以我们在解决网页实时聊天时就遇到一个问题,如何保证与服务器的长时间联系,从而源源不段地获取信息. 一直以来的方式 ...

  4. 使用Hive或Impala执行SQL语句,对存储在Elasticsearch中的数据操作(二)

    CSSDesk body { background-color: #2574b0; } /*! zybuluo */ article,aside,details,figcaption,figure,f ...

  5. Linux IPC socket编程基础

    头文件 #include<unistd.h> #include <sys/types.h> #include <sys/socket.h> #include< ...

  6. 国内Hadoop应用现状

    Hadoop在国内主要以互联网公司为主,下面主要介绍大规模使用Hadoop或研究Hadoop的公司. 1. 百度 百度在2006年就关注了Hadoop并开始调研和使用,截止2012年,总的集群规模超过 ...

  7. android中BuildConfig.DEBUG的使用

    ADT(r17)中添加了一个新功能可以允许开发者只在Debug模式下允许某些代码.Build系统生成一个名称为BuildConfig的类,该类包含一个DEBUG 常量,该常量会根据您的Build类型自 ...

  8. linux进程间通信-信号量(semaphore)

    一 为什么要使用信号量 为了防止出现因多个程序同时访问一个共享资源而引发的一系列问题,我们需要一种方法,它可以通过生成并使用令牌来授权,在任一时刻只能有一个执行线程访问 代码的临界区域.临界区域是指执 ...

  9. nodejs模块——fs模块

    fs模块用于对系统文件及目录进行读写操作. 一.同步和异步 使用require('fs')载入fs模块,模块中所有方法都有同步和异步两种形式. 异步方法中回调函数的第一个参数总是留给异常参数(exce ...

  10. BNUOJ 52325 Increasing or Decreasing 数位dp

    传送门:BNUOJ 52325 Increasing or Decreasing题意:求[l,r]非递增和非递减序列的个数思路:数位dp,dp[pos][pre][status] pos:处理到第几位 ...