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. CentOS 多网卡绑定bonding

    1.查看环境 ip a |grep -v lo 2.加载bonding模块 modprobe bonding 3.开机自动加载模块到内核 echo 'modprobe bonding &> ...

  2. [Unity2D]Box Collider 2D盒子碰撞器

    盒子碰撞器(BoxCollider2D)是Unity2D中常用的碰撞器,所有为碰撞器,顾名思义,就是用于检测物体之间的碰撞情况的,Unity2D里面除了BoxCollider2D碰撞器之外还集成Box ...

  3. BZOJ3211 花神游历各国

    Description   Input   Output 每次x=1时,每行一个整数,表示这次旅行的开心度 Sample Input 4 1 100 5 5 5 1 1 2 2 1 2 1 1 2 2 ...

  4. jQuery 写的插件图片上下切换幻灯效果

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  5. 什么是SQL注入式攻击

    什么是SQL注入式攻击? 所谓SQL注入式攻击,就是攻击者把SQL命令插入到Web表单的输入域或页面请求的查询字符串,欺骗服务器执行恶意的SQL命令.在某些表单中,用户输入的内容直接用来构造(或者影响 ...

  6. Excel 中单元格和范围的引用(即访问的表示方法)

    计算机中,无非是数据和数据的处理这两件事.Excel的工作表能存储大量数据,除了这些原始数据,我们还要用函数来处理这些数据,比如求和求积,求平均值,排序等等,并把处理结果也存在单元格里.在Excel中 ...

  7. jquery选中将select下拉框中一项后赋值给text文本框

    jquery选中将select下拉框中一项后赋值给text文本框,出现无法将第一个下拉框的value赋值给文本框 因为select默认选中第一项..在选择第一项时,便导致无法激发onchange事件. ...

  8. 云计算中心网络资源分配-Faircloud: sharing the network in cloud computing

    网络资源同计算资源以及存储资源一样,是一种可被租户共享使用并提高利用率的资源.但是,不同租户的计算资源以及存储资源之间,有很强的隔离性,可以实现按需按比例分配的使用方式,但是网络资源却不可以. 主要原 ...

  9. Listener-监听器+ServletContext+ApplicationContext

    参考资料 ServletContext和ApplicationContext有什么区别 ServletContext:是web容器的东西, 一个webapp一个, 比session作用范围要大, 从中 ...

  10. oracle utf8字符集转gbk(转)

    近日有同事在外面部署系统时,安装数据库时可能选择了UTF-8编码格式,导入insert语句时,一个汉字被认为三个字节,这是不行的. 结合上网搜到的资料,将oracle数据库的编码格式,从utf-8改为 ...