题目地址: https://leetcode.com/problems/sudoku-solver/

// 将字符串的数独题转换成 int[9][9]
void setBoard(int board[][], char ** b, int boardRowSize, int boardColSize){
for (int row = ; row < boardRowSize; ++row)
for (int col = ; col < boardColSize; ++col){
if (b[row][col] == '.')
board[row][col] = ;
else
board[row][col] = b[row][col] - '';
}
}
// 计算位置{row,col}的候选个数,并在cands返回候选列表
int calcCandNum(int board[][], int row, int col, int * cands){
if (board[row][col] != )
return ;// 已知位置
int cand[] = { , , , , , , , , };//
// 筛除 非法的数字
// 按行筛除
for (int i = ; i < ; ++i){
if (board[row][i] != )
cand[board[row][i] - ] = ;
} // 按列
for (int i = ; i < ; ++i){
if (board[i][col] != )
cand[board[i][col] - ] = ;
} // 按block
// 计算左上角坐标
int r0, c0;
r0 = (row / ) * ;
c0 = (col / ) * ;
for (int i = r0; i < r0 + ; ++i)
for (int j = c0; j < c0 + ; ++j){
if (board[i][j] != )
cand[board[i][j] - ] = ;
}
// 剩下的1 的总和便是 候选个数
int sum = ;
for (int i = ; i < ; ++i){
if (cand[i]){
cands[sum] = i + ;
sum++;
}
}
return sum;
} typedef struct tagCandidate{
int row, col; // 位置
int n; // 候选个数
int cands[]; // 候选列表
int solved; // 数独是否已经解开
}Candidate; // 从数独板中返回候选个数最少的位置,其候选信息存入 cand
void getCandidate(int board[][], Candidate* cand){
int candList[];
int currN;
cand->n = ;
cand->solved = ;
for (int row = ; row < ; ++row)
for (int col = ; col < ; ++col){
if (board[row][col] != ){ // 该位置有值了
continue;
}
// 说明数独还有空洞
cand->solved = ; // 计算该孔洞的候选个数,及候选序列
currN = calcCandNum(board, row, col,candList);
if (currN < cand->n){
cand->n = currN;
cand->row = row;
cand->col = col;
for (int i = ; i < currN; ++i)
cand->cands[i] = candList[i];
}
}
} int solveBoard(int board[][]){
Candidate cand;
getCandidate(board, &cand);
if (cand.solved){
return ;
}
for (int i = ; i < cand.n; ++i){
// guess[cand.row][cand.col] = recursiveFlag; // flag we guess 好像这个没什么用
board[cand.row][cand.col] = cand.cands[i]; // fill what we guess
if (solveBoard(board))
// we'd solved it
return ;
else{
// we'd not solved it
// clear what we guess
// clearGuess(int board[9][9], int guess[])
board[cand.row][cand.col] = ;
}
}
// 到这里来!! 不可能 , 无解????
return ;
}
// 将结果写回字符数组 b 中
void outputBoard(char **b, int board[][]){
for(int row = ; row < ; ++row)
for(int col = ; col < ; ++col){
b[row][col] = '' + (board[row][col]);
}
}
void solveSudoku(char** b, int boardRowSize, int boardColSize) {
int board[][];
setBoard(board, b, boardRowSize, boardColSize);
solveBoard(board);
outputBoard(b,board);
}

LeetCode-Sudoku Solver (递归解法)的更多相关文章

  1. [LeetCode] Sudoku Solver 解数独,递归,回溯

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

  2. [LeetCode] Sudoku Solver 求解数独

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

  3. Leetcode: Sudoku Solver

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

  4. leetcode—sudoku solver

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

  5. LEETCODE —— Sudoku Solver

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

  6. [LeetCode] Sudoku Solver(迭代)

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

  7. leetcode Sudoku Solver python

    #the define of Sudoku is on this link : http://sudoku.com.au/TheRules.aspx Write a program to solve ...

  8. Leetcode 笔记 36 - Sudoku Solver

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

  9. 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 ...

随机推荐

  1. MYSQL索引失效的各种情形总结

    1) 没有查询条件,或者查询条件没有建立索引  2) 在查询条件上没有使用引导列  3) 查询的数量是大表的大部分,应该是30%以上.  4) 索引本身失效 5) 查询条件使用函数在索引列上,或者对索 ...

  2. MVC3和MVC4相关问题

    从TFS获取的MVC项目生成一直提示这个错误,我的VS中MVC3和MVC4都有,TFS项目中的MVC版本应该是MVC3的,现在我要用这个项目,但是一直是这个错误,请高手指点,积分不多了,见谅 程序集“ ...

  3. Sass 中的 @ 规则

    一. @import Sass 扩展了 CSS 的 @import 规则,让它能够引入 SCSS 和 Sass 文件. 所有引入的 SCSS 和 Sass 文件都会被合并并输出一个单一的 CSS 文件 ...

  4. [Android Pro] 内容提供者ContentProvider的基本使用

    一.ContentProvider简介 当应用继承ContentProvider类,并重写该类用于提供数据和存储数据的方法,就可以向其他应用共享其数据.ContentProvider为存储和获取数据提 ...

  5. grep -w

    [root@86 ~]# mysqladmin -uroot -p123456 -S /tmp/mysql.sock extended-status|grep -w "Com_update& ...

  6. Eclipse 代码格式化

    http://blog.csdn.net/prstaxy/article/details/7839197 http://jingyan.baidu.com/article/9158e00044efb6 ...

  7. PHP文件处理类

    /** * 文件读写类 * 读取时,支持跳过N个/行字符然后再读取M个/行字符 * 支持每次读取时使用回调函数 * * 示例: * $file = new File('a.txt', 'r'); * ...

  8. html中rel标签是什么意思

    <a> 标签的 rel 属性用于指定当前文档与被链接文档的关系.用于 <a> 标签的可选属性 rel 和 rev 分别表示源文档与目标文档之间正式的关系和方向.rel 属性指定 ...

  9. SparkStreaming+Flume出现ERROR ReceiverTracker: Deregistered receiver for stream 0: Error starting receiver 0 - org.jboss.netty.channel.ChannelException

    文章发自http://www.cnblogs.com/hark0623/p/4204104.html ,转载请注明 我发现太多太多的坑要趟了… 向yarn提交sparkstreaming了,提交脚本如 ...

  10. 16.2.13 asp.net 学习随笔

    using System.Data.SqlClient;//连接数据库必须的 using System.Configuration; CommandType所在的命名空间 system.data; P ...