题目地址: 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. jquery unbind bind

    $(selector).unbind(); --unbind() 方法会删除指定元素的所有事件处理程序 $(selector).unbind("click"); --unbind( ...

  2. 45. Singleton类的C++/C#实现[Singleton]

    [题目] 设计一个类,我们只能生成该类的一个实例. [分析] 单例模式的意图是保证一个类仅有一个实例,并提供一个访问它的全局访问点.让类自身负责保存它的唯一实例.这个类可以保证没有其他实例可.以被创建 ...

  3. Android Fragment间对象传递

    由于Activity相对较为笨重,因而在日常的开发中很多时候会用到Fragment.然而Activity之间是通过Intent进行数据的传递,那Fragment是通过什么来进行传递的呢?Fragmen ...

  4. weblogic 安装和部署项目(原创)

    1.下载weblogic(含破解文件,土豪请支持正版,谢谢!) 2.安装如下图: 3.新建domain 4.打开weblogic Console 5.开始部署项目 6.部署成功

  5. Android 图标添加消息提醒

    实现方法: 1. 在对应的布局放置TextView或者ImageView. 2. 用Canvas在原来Icon的bitmap基础上进行绘制 3. 利用开源项目ViewBadger进行添加,很方便,而且 ...

  6. Android之jni入门

    jni即java native interface,使用jni我们可以在JAVA中调用C代码,提高了效率,可以复用代码,可以灵活的应用于各种场景 怎么使用JNI 安装软件 1.NDK 用于将C代码编译 ...

  7. 学习Hadoop整体理解

    HDFS是Hadoop的核心模块之一,围绕HDFS是什么.HDFS的设计思想和HDFS的体系结构三方面来介绍. Hadoop的设计思想受到Google公司的GFS设计思想的启示,基于一种开源的理念实现 ...

  8. svn update 每更新一项就输出一行信息,使用首字符来报告执行的动作 这些字符的含义是:

    A 已添加 D 已删除 U 已更新 C 合并冲突 G 合并成功 例子: [root@ok 资料库]# svn ci -m "" Sending 资料库/简历 Transmittin ...

  9. Struts表单格局;theme三个属性值:simple,xhtml,css_xhtml

    转自:http://www.educity.cn/wenda/7156.html 解决Struts2 Form表单自己布局之前先看看 theme 属性, theme属性提供 三个属性值:simple, ...

  10. Codeforces Gym 100203I I - I WIN 网络流最大流

    I - I WINTime Limit: 2 Sec Memory Limit: 256 MB 题目连接 http://acm.hust.edu.cn/vjudge/contest/view.acti ...