【leetcode】Sudoku Solver
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.
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的更多相关文章
- 【LeetCode】哈希表 hash_table(共88题)
[1]Two Sum (2018年11月9日,k-sum专题,算法群衍生题) 给了一个数组 nums, 和一个 target 数字,要求返回一个下标的 pair, 使得这两个元素相加等于 target ...
- 【LeetCode】回溯法 backtracking(共39题)
[10]Regular Expression Matching [17]Letter Combinations of a Phone Number [22]Generate Parentheses ( ...
- 【LeetCode】36. Valid Sudoku 解题报告(Python)
[LeetCode]36. Valid Sudoku 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址 ...
- 【LeetCode】Minimum Depth of Binary Tree 二叉树的最小深度 java
[LeetCode]Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum dept ...
- 【Leetcode】Pascal's Triangle II
Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3, Return [1,3 ...
- 53. Maximum Subarray【leetcode】
53. Maximum Subarray[leetcode] Find the contiguous subarray within an array (containing at least one ...
- 27. Remove Element【leetcode】
27. Remove Element[leetcode] Given an array and a value, remove all instances of that value in place ...
- 【刷题】【LeetCode】007-整数反转-easy
[刷题][LeetCode]总 用动画的形式呈现解LeetCode题目的思路 参考链接-空 007-整数反转 方法: 弹出和推入数字 & 溢出前进行检查 思路: 我们可以一次构建反转整数的一位 ...
- 【刷题】【LeetCode】000-十大经典排序算法
[刷题][LeetCode]总 用动画的形式呈现解LeetCode题目的思路 参考链接 000-十大经典排序算法
随机推荐
- python 中颜色的表示
字背景颜色范围:40----49 40:黑 41:深红 42:绿 43:黄色 44:蓝色 45:紫色 46:深绿 47:白色 字颜色:30-----------39 30:黑 31:红 32:绿 33 ...
- oracle-3-子查询和常用函数
主要内容: >子查询 >伪例 >锁的概念 >>>1.子查询 子查询在SELECT ,UPDATE ,DELETE 语句内部可以出现SELECT 语句,内部的SELE ...
- codevs 1360 xth砍树 线段树不能再水的题了
连标记都不用打.. #include<cstdio> #include<cstring> #include<algorithm> using namespace s ...
- 【CodeForces 613B】Skills
题 题意 给你n个数,可以花费1使得数字+1,最大加到A,最多花费m.最后,n个数里的最小值为min,为A的有k个,给你cm和cf,求force=min*cm+k*cf 的最大值,和n个数操作后的结果 ...
- Teradata 语句简单优化
Teradata 基本查询语言和SQL server 是一致的.有很小的区别. 功能没有SQL 全面,界面没有SQL 好看~ 1. teradata 里面经常会报一种错误: no enough spo ...
- 18.Android之SharedPreferences数据存储学习
SharedPreferences是Android中最容易理解的数据存储技术,实际上SharedPreferences处理的就是一个key-value(键值对)SharedPreferences常用来 ...
- HDU 1060 Left-most Digit
传送门 Leftmost Digit Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Other ...
- iOS 知识点梳理
OC的理解与特性 OC作为一门面向对象的语言,自然具有面向对象的语言特性:封装.继承.多态.它既具有静态语言的特性(如C++),又有动态语言的效率(动态绑定.动态加载等).总体来讲,OC确实是一门不错 ...
- 学习 easyui 之一:easyloader 分析与使用
http://www.cnblogs.com/haogj/archive/2013/04/22/3036685.html 使用脚本库总要加载一大堆的样式表和脚本文件,在 easyui 中,除了可以使用 ...
- JS(截取字符串,显示当前系统时间yyyy-MM-dd,从文本框得到的数值计算)
截取字符串: var str = "1234567890"; var a = str.substring(0,8); //==str.substring(8)---结果:12 ...