LeetCode(37) 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…
分析
与36 Valid Sudoku本质相同的题目,上一题要求判定所给九宫格能否满足数独要求,本题要求给定唯一解。
所采取基本方法 — 遍历 , 遍历整个9*9宫格每一元素,对所有的 ′.′ 分别判定1~9是否满足要求,对于判定,我们采用上一题的思路,但是此时只需判定当前位置所在行、列、及3*3块是否满足要求即可。
AC代码
class Solution {
private:
const int N = 9;
public:
void solveSudoku(vector<vector<char>>& board) {
if (board.empty())
return;
isValidSudoku(board);
}
bool isValidSudoku(vector<vector<char> > &board)
{
//所给非空九宫格
for (int r = 0; r < N; r++)
{
for (int c = 0; c < N; c++)
{
//如果当前为待填充数字
if ('.' == board[r][c])
{
for (int i = 1; i <= 9; i++)
{
board[r][c] = i + '0';
//判断当前填充数字是否合理
if (judge(board, r, c))
{
if (isValidSudoku(board))
return true;
}//if
board[r][c] = '.';
}//for
return false;
}//if
}//for
}//for
}//isValid
//判断当前row,col中所填数字是否合理,只需要判断当前行,当前列,以及当前所在的3*3块
bool judge(vector<vector<char> > &board, int row, int col)
{
//(1)判断当前行
for (int j = 0; j < N; j++)
{
if (col != j && board[row][j] == board[row][col])
return false;
}
//(2)判断当前列
for (int i = 0; i < N; i++)
{
if (row != i && board[i][col] == board[row][col])
return false;
}//for
//(3)判断当前3*3块
for (int i = row / 3 * 3; i < (row / 3 + 1) * 3; ++i)
{
for (int j = col / 3 * 3; j < (col / 3 + 1) * 3; ++j)
{
if (row != i && j != col && board[row][col] == board[i][j])
return false;
}//for
}//for
return true;
}
};
LeetCode(37) Sudoku Solver的更多相关文章
- LeetCode(37): 每k个一组翻转链表
Hard! 题目描述: 编写一个程序,通过已填充的空格来解决数独问题. 一个数独的解法需遵循如下规则: 数字 1-9 在每一行只能出现一次. 数字 1-9 在每一列只能出现一次. 数字 1-9 在每一 ...
- node实现http上传文件进度条 -我们到底能走多远系列(37)
我们到底能走多远系列(37) 扯淡: 又到了一年一度的跳槽季,相信你一定准备好了,每每跳槽,总有好多的路让你选,我们的未来也正是这一个个选择机会组合起来的结果,所以尽可能的找出自己想要的是什么再做决定 ...
- 构建ASP.NET MVC4+EF5+EasyUI+Unity2.x注入的后台管理系统(37)-文章发布系统④-百万级数据和千万级数据简单测试
原文:构建ASP.NET MVC4+EF5+EasyUI+Unity2.x注入的后台管理系统(37)-文章发布系统④-百万级数据和千万级数据简单测试 系列目录 我想测试EF在一百万条数据下的显示时间! ...
- Windows Phone开发(37):动画之ColorAnimation
原文:Windows Phone开发(37):动画之ColorAnimation 上一节中我们讨论了用double值进行动画处理,我们知道动画是有很多种的,今天,我向大家继续介绍一个动画类--Colo ...
- Qt 学习之路 2(37):文本文件读写
Qt 学习之路 2(37):文本文件读写 豆子 2013年1月7日 Qt 学习之路 2 23条评论 上一章我们介绍了有关二进制文件的读写.二进制文件比较小巧,却不是人可读的格式.而文本文件是一种人可读 ...
- LeetCode(275)H-Index II
题目 Follow up for H-Index: What if the citations array is sorted in ascending order? Could you optimi ...
- LeetCode(220) Contains Duplicate III
题目 Given an array of integers, find out whether there are two distinct indices i and j in the array ...
- LeetCode(154) Find Minimum in Rotated Sorted Array II
题目 Follow up for "Find Minimum in Rotated Sorted Array": What if duplicates are allowed? W ...
- LeetCode(122) Best Time to Buy and Sell Stock II
题目 Say you have an array for which the ith element is the price of a given stock on day i. Design an ...
随机推荐
- 最小生成树Prim算法和Kruskal算法(转)
(转自这位大佬的博客 http://www.cnblogs.com/biyeymyhjob/archive/2012/07/30/2615542.html ) Prim算法 1.概览 普里姆算法(Pr ...
- URAL 7077 Little Zu Chongzhi's Triangles(14广州I)
题目传送门 题意:有n根木棍,三根可能能够构成三角形,选出最多的三角形,问最大面积 分析:看到这个数据范围应该想到状压DP,这次我想到了.0010101的状态中,1表示第i根木棍选择,0表示没选,每一 ...
- android动画(3)layout动画,layoutChanged动画及算定义它,ListViewActivity的Layout动画(代码和xm配置两种实现l)
1.layout切换动画 代码: 本示例是fragment切换.在它的oncreateView中 public class LayoutAnimationFrgmt extends Fragment ...
- CentOS 安装图形化界面方法
登录系统,使用yum 安装 #yum groupinstall 'X Window System' -y 安装GNOME桌面环境 #yum groupinstall 'GNOME Desktop ...
- memcache的分布式配置
public static class MemcacheHelper { private static MemcachedClient mc; static MemcacheHelper() { St ...
- java设计模式之单例设计模式
单例设计模式 保证一个类在使用过程中,只有一个实例.优势就是他的作用,这个类永远只有一个实例. 优势:这个类永远只有一个实例,占用内存少,有利于Java垃圾回收. 单例设计模式关键点 私有的构造方法. ...
- FileZilla Server 端设置passive模式注意事项
1,需求和问题的产生 实践中需要分布在各地的各个客户端向云端服务器上传文件,因此在阿里云服务器上安装了FileZilla Server软件作为文件FTP服务端. 客户端程序采用FTP方式向服务端传输文 ...
- 使用colab运行深度学习gpu应用(Mask R-CNN)实践
1,目的 Google Colaboratory(https://colab.research.google.com)是谷歌开放的一款研究工具,主要用于机器学习的开发和研究.这款工具现在可以免费使用, ...
- configure: error: The LBL Packet Capture Library, libpcap, was not found!
configure: error: The LBL Packet Capture Library, libpcap, was not found! yum install libpcap*
- SVN与TFS自动同步脚本(很实用)
一直都在园子里看文章,因为各种原因懒得写文章.最近稍得空闲,把这几天的工作成果分享一下. 因为工作需要,开发人员使用Qt进行系统移动端的开发,Qt的版本控制却不提供连接TFS的设置,只有使用svn.没 ...