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. CentOS 安装 Jexus

    官网:http://www.jexus.org/ 安装过程就照着页面上做就好了,前提是需要安装好mono 在VS2015中新建一个MVC应用程序,这里需要注意两个步骤: 第1步:移除bin下的Micr ...

  2. Java设计模式-命令模式(Command)

    命令模式很好理解,举个例子,司令员下令让士兵去干件事情,从整个事情的角度来考虑,司令员的作用是,发出口令,口令经过传递,传到了士兵耳朵里,士兵去执行.这个过程好在,三者相互解耦,任何一方都不用去依赖其 ...

  3. 【poj1090】 Chain

    http://poj.org/problem?id=1090 (题目链接) 题意 给出九连环的初始状态,要求将环全部取下需要走多少步. Solution 格雷码:神犇博客 当然递推也可以做. 代码 / ...

  4. POJ3038 Flying Right

    Description Figuring that they cannot do worse than the humans have, Farmer John's cows have decided ...

  5. bzoj3037 创世纪

    两种解法: 一.树状DP /*by SilverN*/ #include<iostream> #include<algorithm> #include<cstring&g ...

  6. Omnet++ 4.0 入门实例教程

    http://blog.sina.com.cn/s/blog_8a2bb17d01018npf.html 在网上找到的一个讲解omnet++的实例, 是4.0下面实现的. 我在4.2上试了试,可以用. ...

  7. hdu 1241 Oil Deposits(水一发,自我的DFS)

    解题思路: 1. 遍历扫描二维数组,遇到‘@’,结果ans++; 2. 将当前 i,j 位置置为‘*’,将当前‘@’的 i,j 传人到DFS函数中,开始遍历八个方向的字符 如果碰到 '@' 则先将当前 ...

  8. 改变字典内的value

    import re,os limit = "8000" username = "liuhuihuang" with open("users_dict& ...

  9. import numpy 和 from numpy import * 的区别

    对于 import xxx 使用函数的方法为 x'x'x.Afunc 而对于 from xxx import * 调用函数的方法为 Afunc

  10. html中input输入框屏蔽鼠标右键

    <label> <input id="ckdestinationId" type="text" oncontextmenu="ret ...