LeetCode-Sudoku Solver (递归解法)
题目地址: 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 (递归解法)的更多相关文章
- [LeetCode] Sudoku Solver 解数独,递归,回溯
Write a program to solve a Sudoku puzzle by filling the empty cells. Empty cells are indicated by th ...
- [LeetCode] Sudoku Solver 求解数独
Write a program to solve a Sudoku puzzle by filling the empty cells. Empty cells are indicated by th ...
- Leetcode: Sudoku Solver
July 19, 2015 Problem statement: Write a program to solve a Sudoku puzzle by filling the empty cells ...
- leetcode—sudoku solver
1.题目描述 Write a program to solve a Sudoku puzzle by filling the empty cells. Empty cells are indicate ...
- LEETCODE —— Sudoku Solver
Write a program to solve a Sudoku puzzle by filling the empty cells. Empty cells are indicated by th ...
- [LeetCode] Sudoku Solver(迭代)
Write a program to solve a Sudoku puzzle by filling the empty cells. Empty cells are indicated by th ...
- leetcode Sudoku Solver python
#the define of Sudoku is on this link : http://sudoku.com.au/TheRules.aspx Write a program to solve ...
- Leetcode 笔记 36 - Sudoku Solver
题目链接:Sudoku Solver | LeetCode OJ Write a program to solve a Sudoku puzzle by filling the empty cells ...
- 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 ...
随机推荐
- 简单制作 OS X Yosemite 10.10 正式版U盘USB启动安装盘方法教程
转自: http://www.iplaysoft.com/osx-yosemite-usb-install-drive.html http://jingyan.baidu.com/article/02 ...
- Android app主线程UI更新间歇性崩溃的问题
对App进行开发测试时,偶尔出现app崩溃的问题.日志如下: 10-25 18:44:52.935 15290-15290/com.zzq.cnblogs E/AndroidRuntime﹕ FATA ...
- 一些LUA函数(转载)
转自http://hi.baidu.com/chevallet/item/9a3a6410c20d929198ce3363 一些LUA函数 1.assert (v [, message]) 功能:相当 ...
- Java for LeetCode 051 N-Queens
Given an integer n, return all distinct solutions to the n-queens puzzle. Each solution contains a d ...
- mongoose学习笔记3--简单查询1
简述 查询就是返回一个集合中的文档的子集 Mongoose 模型提供了 find. findOne. findById 三种方法用于文档查询. 为了方便后面课程的有效学习,我们先添加一些测试数据. T ...
- java类初始化优先级
父类静态变量.父类静态代码块.子类静态变量.子类静态代码块.父类非静态变量.父类非静态代码块.父类构造函数.子类非静态变量.子类非静态代码块.子类构造函数
- Android之Intent深入
Android中的意图包含多种用法,本文主要包括以下内容 显式意图 隐匿意图 要求结果回传的意图 显式意图 :必须指定要激活的组件的完整包名和类名 (应用程序之间耦合在一起) 一般激活自己应用的组件的 ...
- java中值类型和引用类型的区别
[定义] 引用类型表示你操作的数据是同一个,也就是说当你传一个参数给另一个方法时,你在另一个方法中改变这个变量的值,那么调用这个方法是传入的变量的值也将改变. 值类型表示复制一个当前变量传给方法,当你 ...
- Eclipse中项目面板字体的修改
修改eclipse安装目录中的如下文件,添加黄色标记部分,并设定自己需要的字体大小(这里是10px)即可: \eclipse\plugins\org.eclipse.ui.themes_1.1.1.v ...
- HDU 1166 敌兵布阵 线段树
敌兵布阵 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submis ...