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. velocity 判断 变量 是否不是空或empty

    原先的 #if($mobile) 这种写法是不准确的 ,请换成 "$!{ mobile}"!="" 说明 :    #if($mobile)   这种写法 只能 ...

  2. Codeforces Round #192 (Div. 2) B. Road Construction

    #include <iostream> #include <vector> using namespace std; int main(){ int n,m; cin > ...

  3. MySQL配置SQL Assistant提示

    以前开发一直使用SQL Server数据库,提示插件采用的就是SQL Assistant,写起脚本来相当有效率.这段时间公司转型要采用MySQL数据库,试用了mysql workbench.mysql ...

  4. hdu 最大报销额

    本题也是一个背包的问题,我觉得这道题的核心就是根据精确度将浮点型转化为整型然后利用动态规划进行求解,注意对题意的理解,有3种支票是不能够报销的. 我开始照着这个思路进行思考,但是敲出来的第一个代码居然 ...

  5. git实用攻略

    Git是一款免费.开源的分布式版本控制系统,用于敏捷高效地处理任何或小或大的项目. 下面是实用教程,免基础. 一.安装git https://git-scm.com/downloads 二.git创建 ...

  6. 在CSV文件中增加一列属性值

    具体参见:系统管理\将文件夹复制到列表中的远程主机   修改前: column1, column2 1,b 2,c 3,5   修改后: column1, column2, column3 1,b, ...

  7. 【转】Java跨平台原理

    原文地址:http://www.cnblogs.com/gw811/archive/2012/09/09/2677386.html 1.是么是平台 Java是可以跨平台的编程语言,那我们首先得知道什么 ...

  8. mapreduce运用

    测试环境:192.168.1.55 mongo 192.168.1.55:30001show dbsuse gwgps 测试目标,求出两个班的总数,人数,平均分数等.可以根据不同的业务需求,定制map ...

  9. Frenetic HelloSDNWorld

    Follow Frenetic-Github HelloSDNWorld 实验环境: Frenetic虚拟机: 实验步骤: 1.Start up a terminal window - – two a ...

  10. PHP 开发 APP 接口 学习笔记与总结 - APP 接口实例 [6] 版本升级接口开发

    判定 app 是否需要加密:通过 app 表中的 status 字段来判定,加密的字符串为 app 表中的 key 字段. 在获取的客户端和服务器端(数据库表中相应字段)的版本号不一致时,返回 dat ...