Sudoku Solver

Write a program to solve a Sudoku puzzle by filling the empty cells.

Empty cells are indicated by the character '.'.

You may assume that there will be only one unique solution.

A sudoku puzzle...

...and its solution numbers marked in red.

采用回溯法,利用递归实现
如果当前的元素满足数独条件,则继续判断下一个元素。
如果当前的元素不满足数独条件,则返回,递归回溯到上一个元素继续查找
 
由于肯定有解,所以在判断的时候可以不必使用hash表,直接判断其他位置的元素与当前要判断的元素是否相等就可以了。
 
 
 class Solution {
public: bool isValid(vector<vector<char> > &board,int i0,int j0)
{
char target=board[i0][j0]; for(int i=;i<;i++)
{
if(i==i0) continue;
if(board[i][j0]==target)
{
return false;
}
} for(int j=;j<;j++)
{
if(j==j0) continue;
if(board[i0][j]==target)
{
return false;
}
} for(int i=i0/*;i<i0/*+;i++)
{ for(int j=j0/*;j<j0/*+;j++)
{
if(i==i0&&j==j0) continue;
if(board[i][j]==target)
{
return false;
}
}
} return true;
} bool scanPos(vector<vector<char> > &board,int pos)
{
if(pos==) return true; bool flag=false;
int i0=pos/;
int j0=pos%; if(board[i0][j0]!='.')
{
return scanPos(board,pos+);
} for(int j=;j<=;j++)
{ board[i0][j0]=''+j;
if(isValid(board,i0,j0))
{
if(scanPos(board,pos+))
{
flag=true;
break;
}
}
} if(flag==false)
{
board[i0][j0]='.';
return false;
}
else
{
return true;
}
} void solveSudoku(vector<vector<char> > &board) {
scanPos(board,);
}
};

【leetcode】Sudoku Solver的更多相关文章

  1. 【LeetCode】哈希表 hash_table(共88题)

    [1]Two Sum (2018年11月9日,k-sum专题,算法群衍生题) 给了一个数组 nums, 和一个 target 数字,要求返回一个下标的 pair, 使得这两个元素相加等于 target ...

  2. 【LeetCode】回溯法 backtracking(共39题)

    [10]Regular Expression Matching [17]Letter Combinations of a Phone Number [22]Generate Parentheses ( ...

  3. 【LeetCode】36. Valid Sudoku 解题报告(Python)

    [LeetCode]36. Valid Sudoku 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址 ...

  4. 【LeetCode】Minimum Depth of Binary Tree 二叉树的最小深度 java

    [LeetCode]Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum dept ...

  5. 【Leetcode】Pascal&#39;s Triangle II

    Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3, Return [1,3 ...

  6. 53. Maximum Subarray【leetcode】

    53. Maximum Subarray[leetcode] Find the contiguous subarray within an array (containing at least one ...

  7. 27. Remove Element【leetcode】

    27. Remove Element[leetcode] Given an array and a value, remove all instances of that value in place ...

  8. 【刷题】【LeetCode】007-整数反转-easy

    [刷题][LeetCode]总 用动画的形式呈现解LeetCode题目的思路 参考链接-空 007-整数反转 方法: 弹出和推入数字 & 溢出前进行检查 思路: 我们可以一次构建反转整数的一位 ...

  9. 【刷题】【LeetCode】000-十大经典排序算法

    [刷题][LeetCode]总 用动画的形式呈现解LeetCode题目的思路 参考链接 000-十大经典排序算法

随机推荐

  1. iOS开发小技巧--设置按钮圆角

    方法一:代码设置 方法二:通过图形化界面

  2. 【BZOJ-2809】dispatching派遣 Splay + 启发式合并

    2809: [Apio2012]dispatching Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 2334  Solved: 1192[Submi ...

  3. POJ3784 Running Median

    Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 1670   Accepted: 823 Description For th ...

  4. ECSHOP \admin\edit_languages.php GETSHELL Based On Injection PHP Code Into /languages/zh_cn/user.php

    目录 . 漏洞描述 . 漏洞触发条件 . 漏洞影响范围 . 漏洞代码分析 . 防御方法 . 攻防思考 1. 漏洞描述 对于很多CMS网站来说,它们都需要保存很多的网站META信息,最常用的最佳实践是以 ...

  5. Erlang第一课----基本变量

    1.(in Erlang shell)A sequence of expressions must be terminated with a period followed by whitespace ...

  6. TCP/IP详解 学习四

    ARP地址解析协议 当一台主机把以太网数据帧发送到位于同一局域网上的另一台主机时,是根据 48 bit的以太网地址来确定目的接口的.设备驱动程序从不检查 I P数据报中的目的 I P地址. ARP的分 ...

  7. HDU2196computer(树上最远距离 + DP)

    Computer Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Su ...

  8. 微型 ORM-FluentData 温故知新系列

    http://www.cnblogs.com/_popc/archive/2012/12/26/2834726.html 引言:FluentData 是微型 ORM(micro-ORM)家族的一名新成 ...

  9. 软件开发过程中的审查 (Review)

    http://blog.csdn.net/horkychen/article/details/5035769 软件开发过程中的审查 (Review)   希望别人做些什么->定义出流程 希望别人 ...

  10. Spring学习4-面向切面(AOP)之Spring接口方式

    一.初识AOP    关于AOP的学习可以参看帮助文档:spring-3.2.0.M2\docs\reference\html目录下index.html的相关章节       1.AOP:Aspect ...