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 ...
随机推荐
- HBuilder mui 报错No 'Access-Control-Allow-Origin' header
Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' he ...
- The 17th Zhejiang University Programming Contest Sponsored by TuSimple J
Knuth-Morris-Pratt Algorithm Time Limit: 1 Second Memory Limit: 65536 KB In computer science, t ...
- 洛谷 P4585 [FJOI2015]火星商店问题
(勿看,仅作笔记) bzoj权限题... https://www.luogu.org/problemnew/show/P4585 对于特殊商品,直接可持久化trie处理一下即可 剩下的,想了一段时间c ...
- linux下实现多台服务器同步文件(inotify-tools+rsync实时同步文件安装和配置)
inotify-tools+rsync实时同步文件安装和配置 注:转载https://www.linuxidc.com/Linux/2012-06/63624.htm
- magento 自定义url路径 和 filter data 小结
背景是往一个第三方的搜索插件里面加入filter功能. 首先是路径,插件自己定义了一个router,类似于cms.那首先说说router好了,从入口一路追查的话,会发现最后进入的是Mage_Core_ ...
- Apache Kylin的核心概念
不多说,直接上干货! 1.表(table):This is definition of hive tables as source of cubes,在build cube 之前,必须同步在 kyli ...
- Unity3D中GameObject 详细介绍
通过Hierarchy面板下的Create菜单可以手动地创建一个GameObject,它可以是一个相机,一个灯光,或者一个简单的模型,当我们要在程序里面动态地创建一个相机的时候,可以new一个Game ...
- [转].NET MVC 分页以及增删查改
本文转自:http://blog.csdn.net/sust2012/article/details/30761867 . 数据库操作,DAL 层: using System; using Syste ...
- 使用原生javascript实现jquery的$(function(){ })
在使用jquery的时候,经常用到$(function(){})方法或者是$(document).read(function(){})来作为页面dom节点加载完成之后javascript的执行入口,现 ...
- STM32&AT指令NBIOT模组
#include "nbiot.h" #include "string.h" #include "stdlib.h" #include &q ...