题目地址: 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. 【云计算】开源装机自动化系统 CloudBoot OSInstall 介绍

    "CloudBoot"(OSinstall) 发布了. 产品更新及特点如下: 新增虚拟化操作系统适配:支持主流操作系统:RedHat.CentOS.SUSE.Ubuntu.Wind ...

  2. iOS 第三方自定义Alertview项目MBProcessHud中的重要代码分析

    做ios,弹出一个自定义的alertview挺常见的.ios7以前,我们可以对系统的UIAlertView进行一点操作,实现一点简单的定制,但是ios7不再允许我们这样做了.因此,我们需要自己创建一个 ...

  3. Linux删除乱码文件

    2015年2月28日 17:11:54 1.  ls -i  列出文件的inode号 ??Φ-ͦ?+?-ˬͩ-????-??.doc 2.  find folder -inum -delete -de ...

  4. 【python】捕获所有异常

    如下所示,在不知道异常名的情况下可以捕获所有异常 try: a=b b=c except Exception,ex: print Exception,":",ex

  5. 【图文详解】scrapy安装与真的快速上手——爬取豆瓣9分榜单

    写在开头 现在scrapy的安装教程都明显过时了,随便一搜都是要你安装一大堆的依赖,什么装python(如果别人连python都没装,为什么要学scrapy….)wisted, zope interf ...

  6. int *p()与int (*p)()的区别

    int *p()是返回指针的函数 int (*p)()是指向函数的指针   返回指针的函数: int *a(int x,int y); 有若干个学生的成绩(每个学生有4门课程),要求在用户输入学生序号 ...

  7. php 字符串处理

    <?php $a = " n001|n002|n003|n004 "; //echo strlen($a);//取字符串的长度 //var_dump(strcmp(" ...

  8. 【转】如何在 Eclipse 中進行 TFS 的版本管控

    转自:http://www.dotblogs.com.tw/franma/archive/2010/05/04/15009.aspx 和上一篇一樣!所使用的版本也是 3.4 的 之前有被問到 Team ...

  9. svn 文件夹 无法提交

    [root@v01 www]# svn add localsvn/kkk/ svn: warning: 'localsvn/kkk' is already under version control ...

  10. C#的面向对象特性之封装

    在C#语言中,共有五种访问修饰符:public.private.protected.internal.protected internal. public 公有访问.不受任何限制.private 私有 ...