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 ...
随机推荐
- macos php安装扩展sqlsrv连接sqlserver
Install the PHP Drivers for SQL Serve sudo pecl install pdo_sqlsrv sudo pecl install sqlsrv 微软官方文档 ...
- ulimit资源配置
基本理解 linux对每个用户能使用的系统资源有一定限制.如果没有限制,在多用户登录,并且都消耗大量资源时,对系统产生复杂的影响.ulimit内建一套参数,来规定一个用户能使用多少资源. [root@ ...
- 正睿OI提高组十连测 day1 总结
可能是最简单的一场比赛了吧,结果却打得这么差... T1是个找规律题,结果一开始愚蠢地找错了规律,然后又对拍,到1h多一点才过掉 然后看t2和t3,以为t2是个水题,t3也只要处理一下就好了,先写t2 ...
- git简单使用方法
跟远程库关联起来: http://www.cnblogs.com/Gabriel-Wei/p/6564102.html http://www.liaoxuefeng.com/wiki/00137395 ...
- 牛客国庆集训派对Day_7
A.Relic Discovery 题目描述 Recently, paleoanthropologists have found historical remains on an island in ...
- PHP的知识点总结1
PHP 基础知识总结 2015-06-03 分类: 编程技术 PHP 代表 PHP: Hypertext Preprocessor PHP 文件可包含文本.HTML.JavaScript代码和 P ...
- PL/SQL笔记(1)-流程控制,循环,异常,块
流程控制 1.If,then,else,elsif(不是elseif) ' then null; endif; 2.Case 简单case表达式: 搜索型Case表达式: 3.goto语句 begin ...
- AJPFX简述abstract class和interface的区别
含有abstract修饰符的class即为抽象类,abstract类不能创建的实例对象.含有abstract方法的类必须定义为abstract class,abstract class类中的方法不必是 ...
- bootstrap CSS表单、按钮和字体图标
基础表单 <form role="form"> <div class="form-group"> <l ...
- 【学习笔记】深入理解js原型和闭包(9)—— 简述【执行上下文】下
继续上一篇文章(https://www.cnblogs.com/lauzhishuai/p/10078231.html)的内容. 上一篇我们讲到在全局环境下的代码段中,执行上下文环境中有如何数据: 变 ...