【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 ...
随机推荐
- 并发访问sqlite数据库出现databse is locked的错误的一个解决办法
作者:朱金灿 来源:http://blog.csdn.net/clever101 在并发访问sqlite数据库会出现这样一个错误:databseis locked,这是sqlite数据库对并发支持不太 ...
- 浏览器兼容圆角Border-radius的问题
圆角css代码:border-radius只有在以下版本的浏览器:Firefox4.0+.Google Chrome 10.0+.Opera 10.5+.IE9+支持border-radius标准语法 ...
- Java的几种加载驱动的方法
1.Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver"); 2.DriverManager.registerD ...
- linux 命令——40 wc (转)
Linux系统中的wc(Word Count)命令的功能为统计指定文件中的字节数.字数.行数,并将统计结果显示输出. 1.命令格式: wc [选项]文件... 2.命令功能: 统计指定文件中的字节数. ...
- sql常识
1.UNION与UNION ALL的区别UNION去重且排序UNION ALL不去重不排序2.sql语句or与union all的执行效率比较:union all>union> in &g ...
- iptables (1) 原理
网上看到这个配置讲解得还比较易懂,就转过来了,大家一起看下,希望对您工作能有所帮助. iptables简介 netfilter/iptables(简称为iptables)组成Linux平台下的包过滤防 ...
- Android(java)学习笔记85:使用SQLite的基本流程
- 【HHHOJ】NOIP2018 模拟赛(二十四) 解题报告
点此进入比赛 得分: \(100+60+100\)(挺好的,涨了一波\(Rating\)) 排名: \(Rank\ 1\) \(Rating\):\(+115\) \(T1\):[HHHOJ13]金( ...
- find - 递归地在层次目录中处理文件
总览 SYNOPSIS find [path...] [expression] 描述 DESCRIPTION 这个文档是GNU版本 find 命令的使用手册. find 搜索目录树上的每一个文件名,它 ...
- CSS之常见文字样式整理
常见文字样式 行高:line-height,当我i们将行高的大小设置成当前元素的高度时,可以实现当行文本在当前元素中垂直方向居中显示的效果 水平对齐方式:text-align:left|center| ...