【Sudoku Solver】cpp
题目:
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<pair<int, int> > emptyCells;
vector<set<char> > rowSet, colSet, matrixSet;
set<char> row, col, matrix;
for ( size_t i = ; i < ; ++i )
{
row.clear(); col.clear(); matrix.clear();
for ( size_t j = ; j < ; ++j )
{
if (board[i][j]!='.'){
row.insert(board[i][j]);
}
else{
emptyCells.push_back(make_pair(i, j));
}
if (board[j][i]!='.') col.insert(board[j][i]);
int r = j/+(i/)*, c = j%+(i%)*;
if (board[r][c]!='.') matrix.insert(board[r][c]);
}
rowSet.push_back(row); colSet.push_back(col); matrixSet.push_back(matrix);
}
Solution::dfs(board, emptyCells, rowSet, colSet, matrixSet);
}
static bool dfs(
vector<vector<char> >& board,
vector<pair<int, int> >& emptyCell,
vector<set<char> >& rowSet,
vector<set<char> >& colSet,
vector<set<char> >& matrixSet)
{
if ( emptyCell.empty() ) return true;
int i = emptyCell.back().first;
int j = emptyCell.back().second;
for ( char v = ''; v<='' && !emptyCell.empty(); ++v )
{
if (rowSet[i].find(v)==rowSet[i].end() &&
colSet[j].find(v)==colSet[j].end() &&
matrixSet[(i/)*+j/].find(v)==matrixSet[(i/)*+j/].end() )
{
board[i][j]=v;
rowSet[i].insert(v);
colSet[j].insert(v);
matrixSet[(i/)*+j/].insert(v);
emptyCell.pop_back();
if ( Solution::dfs(board, emptyCell, rowSet, colSet, matrixSet) )
{
return true;
}
else
{
emptyCell.push_back(make_pair(i, j));
board[i][j] = '.';
rowSet[i].erase(v);
colSet[j].erase(v);
matrixSet[(i/)*+j/].erase(v);
}
}
}
return false;
}
};
tips:
采用深搜模板。
主要思路走一遍board,得到三个set,一个vector
1. 三个set分别为每行、列、子模块已有的数字
2. 一个vector中存放着'.'的位置
每次处理一个'.',遍历1到9:
1. 如果满足数独的条件,就往下走一层
2. 如果1到9都不满足,则退回到上一层,重新选择上一个位置的元素
这里有一个思维陷阱:
emptyCell.pop_back();
if ( Solution::dfs(board, emptyCell, rowSet, colSet, matrixSet) )
{
return true;
}
else
{
emptyCell.push_back(make_pair(i, j));
board[i][j] = '.';
rowSet[i].erase(v);
colSet[j].erase(v);
matrixSet[(i/)*+j/].erase(v);
}
注意:pop和push操作应该是对应的,之前一直以为可以不用push的操作,原因是忽略了一种情况:如果一个位置从1到9都不满足,那么必然要回溯到上一层;即,某一个位置的元素是可能遍历不止一次1到9的。
=================================
这里set的效率可能有些低,换一个hashmap的效率可能高一些。
class Solution {
public:
void solveSudoku(vector<vector<char> >& board)
{
vector<pair<int, int> > emptyCells;
vector<map<char,bool> > rowSet, colSet, matrixSet;
map<char,bool> row, col, matrix;
for ( size_t i = ; i < ; ++i )
{
for ( char v = '' ; v <= ''; ++v ) { row[v] = col[v] = matrix[v] = false; }
for ( size_t j = ; j < ; ++j )
{
if (board[i][j]!='.'){
row[board[i][j]] = true;
}
else{
emptyCells.push_back(make_pair(i, j));
}
if (board[j][i]!='.') col[board[j][i]]=true;
int r = j/+(i/)*, c = j%+(i%)*;
if (board[r][c]!='.') matrix[board[r][c]]=true;
}
rowSet.push_back(row); colSet.push_back(col); matrixSet.push_back(matrix);
}
Solution::dfs(board, emptyCells, rowSet, colSet, matrixSet);
}
static bool dfs(
vector<vector<char> >& board,
vector<pair<int, int> >& emptyCell,
vector<map<char,bool> >& rowSet,
vector<map<char,bool> >& colSet,
vector<map<char,bool> >& matrixSet)
{
if ( emptyCell.empty() ) return true;
int i = emptyCell.back().first, j = emptyCell.back().second;
for ( char v = ''; v<=''; ++v )
{
if (!rowSet[i][v] && !colSet[j][v] && !matrixSet[(i/)*+j/][v] )
{
board[i][j] = v;
rowSet[i][v] = colSet[j][v] = matrixSet[(i/)*+j/][v] = true;
emptyCell.pop_back();
if ( Solution::dfs(board, emptyCell, rowSet, colSet, matrixSet) ){
return true;
}
else{
emptyCell.push_back(make_pair(i, j));
rowSet[i][v] = colSet[j][v] = matrixSet[(i/)*+j/][v] = false;
}
}
}
return false;
}
};
tips:都换成了hashmap,效率有所提升。
============================================
第二次过这道题,还是sub board那块内容调了几次,AC了。
class Solution {
public:
void solveSudoku(vector<vector<char> >& board)
{
// restore all blank positions
vector<pair<int, int> > blanks;
for ( int i=; i<board.size(); ++i )
{
for ( int j=; j<board[i].size(); ++j )
{
if ( board[i][j]=='.' ) blanks.push_back(make_pair(i, j));
}
}
Solution::dfs(board, blanks);
}
static bool dfs(
vector<vector<char> >& board,
vector<pair<int,int> >& blanks )
{
if ( blanks.empty() ) return true;
const int r = blanks.back().first;
const int c = blanks.back().second;
for ( char v=''; v<=''; ++v )
{
bool valid = true;
// check row & column & subBoard
for ( int i=; i<; ++i )
{
if ( board[r][i]==v || board[i][c]==v || board[i/3+(r/3)*3][i%3+(c/3)*3]==v )
{
valid = false;
break;
}
}
if ( valid )
{
board[r][c] = v;
blanks.pop_back();
if ( Solution::dfs(board, blanks) ) return true;
blanks.push_back(make_pair(r, c));
board[r][c] = '.';
}
}
return false;
}
};
【Sudoku Solver】cpp的更多相关文章
- hdu 4739【位运算】.cpp
题意: 给出n个地雷所在位置,正好能够组成正方形的地雷就可以拿走..为了简化题目,只考虑平行于横轴的正方形.. 问最多可以拿走多少个正方形.. 思路: 先找出可以组成正方形的地雷组合cnt个.. 然后 ...
- Hdu 4734 【数位DP】.cpp
题意: 我们定义十进制数x的权值为f(x) = a(n)*2^(n-1)+a(n-1)*2(n-2)+...a(2)*2+a(1)*1,a(i)表示十进制数x中第i位的数字. 题目给出a,b,求出0~ ...
- 【Permutations II】cpp
题目: Given a collection of numbers that might contain duplicates, return all possible unique permutat ...
- 【Subsets II】cpp
题目: Given a collection of integers that might contain duplicates, nums, return all possible subsets. ...
- 【Sort Colors】cpp
题目: Given an array with n objects colored red, white or blue, sort them so that objects of the same ...
- 【Sort List】cpp
题目: Sort a linked list in O(n log n) time using constant space complexity. 代码: /** * Definition for ...
- 【Path Sum】cpp
题目: Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up ...
- 【Symmetric Tree】cpp
题目: Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center). F ...
- 【Same Tree】cpp
题目: Given two binary trees, write a function to check if they are equal or not. Two binary trees are ...
随机推荐
- ubuntu 16.04安装nVidia显卡驱动和cuda/cudnn踩坑过程
安装深度学习框架需要使用cuda/cudnn(GPU)来加速计算,而安装cuda/cudnn,首先需要安装nvidia的显卡驱动. 我在安装的整个过程中碰到了驱动冲突,循环登录两个问题,以至于最后不得 ...
- SqlServer作业指定目标服务器
用SSMS生成数据库作业的创建脚本的时候,有一步是sp_add_jobserver操作: EXEC @ReturnCode = msdb.dbo.sp_add_jobserver @job_id = ...
- 利用expect实现自动化操作
管理机上需要安装expect包 yum -y install expect 1.定义主机ip [root@localhost ~]# cat ip.txt 192.168.1.12 192.168.1 ...
- ThinkPHP添加扩展配置失败
扩展配置可以支持自动加载额外的自定义配置文件,并且配置格式和项目配置一样.设置扩展配置的方式如下(多个文件用逗号分隔): // 加载扩展配置文件 'LOAD_EXT_CONFIG' => 'us ...
- 虚拟机中Ubuntu安装vmtools
1.解压vmtools文件为VMWARETO.TGZ VMtools文件一般在系统桌面,如果没有可以点击左上方的"虚拟机-安装VMware Tools"即可出现在桌面,也可以通过U ...
- 往ABAP gateway system上和Cloud Foundry上部署HTML5应用
ABAP Gateway system 在我的公众号文章里有详细介绍:SAP Fiori应用的三种部署方式 用WebIDE部署 用Eclipse Team provider部署 执行report /U ...
- IOS 强指针(strong)和弱指针(weak)
// strong 强指针 // weak 弱指针 // ARC, 只要对象没有强指针就会自动释放 // OC中默认都是强指针
- pip 安装出现异常
MacBookPro:~ mac$ pip install numpy Collecting numpy Downloading numpy-1.13.1-cp35-cp35m-macosx_10_6 ...
- cloudera manager的卸载以及重新安装
1 卸载cloudera 参照 http://www.cnblogs.com/chenfool/p/3738540.html Cloudera 的官方介绍: http://www.cloudera.c ...
- opencv anaconda
from: http://blog.csdn.net/fairylrt/article/details/43560525 Anaconda是一个python的一个包装,或者不单单是这样.你可以认为An ...