题目地址: 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. XPath常用定位节点元素语句总结

    将一个XML或HTML文档转换成了DOM树结构后,如何才能定位到特定的节点?XPath实现了这样的功能,它通过DOM树中节点的路径和属性来导航,通过XPath路径表达式可以选择DOM树中的nodes( ...

  2. percona-xtrabackup备份mysql

    title: 1.percona-xtrabackup备份mysql date: 2016-04-10 23:19:12 tags: mysql categories: mysql --- 一.per ...

  3. 【xml】python的lxml库使用

    1.官方教程:http://lxml.de/tutorial.html#parsing-from-strings-and-files  最重要的文档,看完基本就能用了 2.lxml支持xpath,xp ...

  4. HDU2067卡特兰数

    小兔的棋盘 Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submi ...

  5. iOS PickerView动态加载数据

    将新的数据放入临时数组 NSMutableArray *tmp=[[NSMutableArray alloc] init]; [tmp addObject:[[NSString alloc] init ...

  6. [编解码] 关于base64编码的原理及实现

    转载自: http://www.cnblogs.com/hongru/archive/2012/01/14/2321397.html [Base64]-base64的编码都是按字符串长度,以每3个8b ...

  7. javascript输出AscII码扩展集中的字符

    function test(){ var c=""; for(var i=1;i<65536;i++){ if((i%10)==0){ c+=i+':\t'+String.f ...

  8. HTML基础2 表单和框架

    表单: <form id="" name="" method="post/get" action"负责处理的服务端" ...

  9. JAVA读取XML文件数据

    XML文档内容如下: <?xml version="1.0" encoding="UTF-8"?> <root> <field t ...

  10. js实现快速排序(in-place)简述

    快速排序,又称划分交换排序.以分治法为策略实现的快速排序算法. 本文主要要谈的是利用javascript实现in-place思想的快速排序 分治法: 在计算机科学中,分治法是建基于多项分支递归的一种很 ...