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:
void solveSudoku(vector<vector<char> > &board) {
vector<set<char>> rowMap();
vector<set<char>> colMap();
vector<set<char>> boxMap();
vector<pair<int,int>> blank;
for(int i=;i<;i++)
{
for(int j=;j<;j++)
{
if(board[i][j]=='.')
{
blank.push_back(pair<int,int>(i,j));
continue;
}
rowMap[i].insert(board[i][j]);
}
}
for(int j=;j<;j++)
{
for(int i=;i<;i++)
{
if(board[i][j]=='.')
continue;
colMap[j].insert(board[i][j]);
}
}
for(int i=;i<;i=i+)
{
for(int j=;j<;j=j+)
{
vector<int> mp(,);
for(int k=;k<;k++)
{
for(int m=;m<;m++)
{
if(board[i+k][j+m]=='.')
continue;
boxMap[(i/) * +j/].insert(board[i+k][j+m]);
}
}
}
}
found = false;
DFS(,blank,rowMap,colMap,boxMap,board); }
private:
void DFS(int t, vector<pair<int,int>> &blank, vector<set<char>> &rowMap, vector<set<char>> &colMap, vector<set<char>> &boxMap, vector<vector<char> > &board)
{
if(t>=blank.size())
{
found = true;
}
else
{
int i= blank[t].first;
int j= blank[t].second;
for(char digit ='';digit<='';digit++)
{
if(rowMap[i].count(digit)> || colMap[j].count(digit)> || boxMap[i/ * + j/].count(digit)>)
{
continue;
}
board[i][j]=digit;
rowMap[i].insert(digit);
colMap[j].insert(digit);
boxMap[i/*+j/].insert(digit);
DFS(t+,blank,rowMap,colMap,boxMap,board);
rowMap[i].erase(digit);
colMap[j].erase(digit);
boxMap[i/*+j/].erase(digit);
if(found)
return;
}
}
}
private:
bool found; };

[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

    July 19, 2015 Problem statement: Write a program to solve a Sudoku puzzle by filling the empty cells ...

  3. LEETCODE —— Sudoku Solver

    Write a program to solve a Sudoku puzzle by filling the empty cells. Empty cells are indicated by th ...

  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 python

    #the define of Sudoku is on this link : http://sudoku.com.au/TheRules.aspx Write a program to solve ...

  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 笔记 36 - Sudoku Solver

    题目链接:Sudoku Solver | LeetCode OJ Write a program to solve a Sudoku puzzle by filling the empty cells ...

  8. [Leetcode][Python]37: Sudoku Solver

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 37: Sudoku Solverhttps://oj.leetcode.co ...

  9. Leetcode之回溯法专题-37. 解数独(Sudoku Solver)

    Leetcode之回溯法专题-37. 解数独(Sudoku Solver) 编写一个程序,通过已填充的空格来解决数独问题. 一个数独的解法需遵循如下规则: 数字 1-9 在每一行只能出现一次.数字 1 ...

随机推荐

  1. Comparing the Performance of .NET Serializers(zz)

    The .NET framework comes with a variety of different serializers. Hopefully, my overview of these se ...

  2. BZOJ4373 : 算术天才⑨与等差数列

    设$pre[i]$表示第$i$个数上一次出现的位置,$d[i]=abs(a[i]-a[i+1])$. 用线段树维护区间内$a$的最小值.最大值,$pre$的最大值以及$d$的$\gcd$. 对于询问$ ...

  3. BZOJ2310 : ParkII

    单路径最大和问题,设f[i][j][S]表示到达(i,j),轮廓线状态为S的最优解. S用4进制m+1位数表示,0表示无插头,1表示左括号,2表示右括号,3表示独立插头. 在DP之前先进行一次预处理, ...

  4. BZOJ3831 : [Poi2014]Little Bird

    设f[i]表示到i最少休息次数,f[i]=min(f[j]+(h[j]<=a[i])),i-k<=j<i,单调队列优化DP #include<cstdio> #defin ...

  5. 【UVa】11270 Tiling Dominoes

    http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&p ...

  6. hdu-acm steps Monkey and Banana

    这道题是典型的dp题.首先是数据的处理上,因为每个长方体的3条不同长度的棱都可以作为高,因此一个长方体可以看成3个不同的长方体.从而将数据扩展为3*n,然后将所有的长方体以长度为第一排序条件,宽度为第 ...

  7. Asp反向代理程序,调用远程站点全站数据,一款脚本级反向代理程序.

    前些天临时写的一脚本级反向代理程序,用法很简单,设置好目标站地址,然后放到你网站根目录:index.asp,再将404页面自定义为:index.asp,即可. 由于暂时没有 url 替换需要,所以没有 ...

  8. SQL判断语句用法和多表查询

    1.格式化时间sql语句 本例中本人随便做了两张表,和实际不是很相符,只是想说明sql语句的写法. 例1表格式如下: 需求:查询出本表,但需要使time字段的时间格式为yyyy-MM-dd,比如:20 ...

  9. 在windows下使用gnu的工具

    mingw Minimalist GNU for Windows http://www.mingw.org/ 想要在Windows使用wget,之前使用的是单独的安装包,现在又想使用bash的命令提示 ...

  10. Nginx 笔记与总结(9)rewrite 重写规则

    重写的规则可以放在 serverer 里,也可以放在 location 里. rewrite 规则: 常用的命令有 ① if(条件){} 设定条件,再进行重写 if 语法: if 空格 (条件){   ...