[LeetCode] 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:
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(迭代)的更多相关文章
- [LeetCode] Sudoku Solver 求解数独
Write a program to solve a Sudoku puzzle by filling the empty cells. Empty cells are indicated by th ...
- Leetcode: Sudoku Solver
July 19, 2015 Problem statement: Write a program to solve a Sudoku puzzle by filling the empty cells ...
- LEETCODE —— Sudoku Solver
Write a program to solve a Sudoku puzzle by filling the empty cells. Empty cells are indicated by th ...
- leetcode—sudoku solver
1.题目描述 Write a program to solve a Sudoku puzzle by filling the empty cells. Empty cells are indicate ...
- leetcode Sudoku Solver python
#the define of Sudoku is on this link : http://sudoku.com.au/TheRules.aspx Write a program to solve ...
- [LeetCode] Sudoku Solver 解数独,递归,回溯
Write a program to solve a Sudoku puzzle by filling the empty cells. Empty cells are indicated by th ...
- Leetcode 笔记 36 - Sudoku Solver
题目链接:Sudoku Solver | LeetCode OJ Write a program to solve a Sudoku puzzle by filling the empty cells ...
- [Leetcode][Python]37: Sudoku Solver
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 37: Sudoku Solverhttps://oj.leetcode.co ...
- Leetcode之回溯法专题-37. 解数独(Sudoku Solver)
Leetcode之回溯法专题-37. 解数独(Sudoku Solver) 编写一个程序,通过已填充的空格来解决数独问题. 一个数独的解法需遵循如下规则: 数字 1-9 在每一行只能出现一次.数字 1 ...
随机推荐
- ural 1218. Episode N-th: The Jedi Tournament
1218. Episode N-th: The Jedi Tournament Time limit: 1.0 secondMemory limit: 64 MB Decided several Je ...
- POJ 1548 (二分图+最小路径覆盖)
题目链接:http://poj.org/problem?id=1548 题目大意:给出一张地图上的垃圾,以及一堆机器人.每个机器人可以从左->右,上->下.走完就废.问最少派出多少个机器人 ...
- 关于 List<T>
System.Object System.Collections.Generic.List<T> list<string,string>,这种形式本身就是错误的,你可以 ...
- 【wikioi】1116 四色问题
题目链接 算法:DFS 刚开始卡了一下,但后面想了想,于是 放上代码: #include <iostream> using namespace std; bool map[9][9]; i ...
- git 创建branch分支
开发者user1 负责用getopt 进行命令解析的功能,因为这个功能用到getopt 函数,于是将这个分支命名为user1/getopt.(1)确保是在开发者user1的工作区中cd /home/j ...
- 通过ReflectionMethod,我们可以得到Person类的某个方法的信息
PHP反射api由若干类组成,可帮助我们用来访问程序的元数据或者同相关的注释交互.借助反射我们可以获取诸如类实现了那些方法,创建一个类的实例(不同于用new创建),调用一个方法(也不同于常规调用),传 ...
- MongoDB空间整理
测试环境:192.168.1.55,单机数据量: 4千万左右.测试:db.repaireDatabase效果db.compact 效果通过stats命令获取该数据库的相关信息:db.stats() { ...
- mysq数据库再次理解
1.表中的一条记录就是一个object,object有很多属性,对应表中的字段.object的属性对应的值就是字段值 2.外键是关联表关系用的.表关系的确立只能通过外键 但更高效的策略是,在数据库中部 ...
- Nginx_Lua
http://www.ttlsa.com/nginx/nginx-lua/ 1.1. 介绍ngx_lua – 把lua语言嵌入nginx中,使其支持lua来快速开发基于nginx下的业务逻辑该模块不在 ...
- push to deploy
1.server端 建立裸仓 $ mkdir ~/repo.git $ cd ~/repo.git $ git init --bare 脚本 $ vim ~/repo.git/hooks/post-r ...