题目

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;
}
};

GitHub测试程序源码

LeetCode(37) Sudoku Solver的更多相关文章

  1. LeetCode(37): 每k个一组翻转链表

    Hard! 题目描述: 编写一个程序,通过已填充的空格来解决数独问题. 一个数独的解法需遵循如下规则: 数字 1-9 在每一行只能出现一次. 数字 1-9 在每一列只能出现一次. 数字 1-9 在每一 ...

  2. node实现http上传文件进度条 -我们到底能走多远系列(37)

    我们到底能走多远系列(37) 扯淡: 又到了一年一度的跳槽季,相信你一定准备好了,每每跳槽,总有好多的路让你选,我们的未来也正是这一个个选择机会组合起来的结果,所以尽可能的找出自己想要的是什么再做决定 ...

  3. 构建ASP.NET MVC4+EF5+EasyUI+Unity2.x注入的后台管理系统(37)-文章发布系统④-百万级数据和千万级数据简单测试

    原文:构建ASP.NET MVC4+EF5+EasyUI+Unity2.x注入的后台管理系统(37)-文章发布系统④-百万级数据和千万级数据简单测试 系列目录 我想测试EF在一百万条数据下的显示时间! ...

  4. Windows Phone开发(37):动画之ColorAnimation

    原文:Windows Phone开发(37):动画之ColorAnimation 上一节中我们讨论了用double值进行动画处理,我们知道动画是有很多种的,今天,我向大家继续介绍一个动画类--Colo ...

  5. Qt 学习之路 2(37):文本文件读写

    Qt 学习之路 2(37):文本文件读写 豆子 2013年1月7日 Qt 学习之路 2 23条评论 上一章我们介绍了有关二进制文件的读写.二进制文件比较小巧,却不是人可读的格式.而文本文件是一种人可读 ...

  6. LeetCode(275)H-Index II

    题目 Follow up for H-Index: What if the citations array is sorted in ascending order? Could you optimi ...

  7. LeetCode(220) Contains Duplicate III

    题目 Given an array of integers, find out whether there are two distinct indices i and j in the array ...

  8. LeetCode(154) Find Minimum in Rotated Sorted Array II

    题目 Follow up for "Find Minimum in Rotated Sorted Array": What if duplicates are allowed? W ...

  9. 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 ...

随机推荐

  1. Java笔记-序列化的注意点

    1.使用serialVersionUID 在Eclipse中,如果一个类实现了Serializable接口,且没有给这个类设置一个serialVersionUID,就会有一个警告标志: The ser ...

  2. UvalLive4670(AC自动机模板)

    放上刘汝佳的模板: #include <cstdio> #include <cstring> #include <string> #include <algo ...

  3. [NOIP2018校模拟赛]T2矩阵分组 Matrix

    题目链接: 矩阵分组 分析: 这道题求的是两部分极差当中大的那个的最小值.对于这种求最值的问题,我们很自然(其实并没有)地想到二分答案. 这个题有两个结论: (好像当时看出来了第一个?然后发现下面都不 ...

  4. 福建工程学院第七届ACM程序设计新生赛 (同步赛)

    A.关电脑 #include<bits/stdc++.h> using namespace std; typedef long long LL; int T,h1,m1,s1,h2,m2, ...

  5. 转 Oracle最新PSU大搜罗

    Quick Reference to Patch Numbers for Database/GI PSU, SPU(CPU), Bundle Patches and Patchsets (文档 ID ...

  6. [已读]HTML5与CSS3设计模式

    我想说,不要被书名骗了,其实并没有涉及丁点h5与css3的内容,但是确实称得上比较详细的一本关于css的书.看它的页数就知道了,481~~ 今年上半年看完的,现在想想,觉得自己还是蛮拼的.内容会比较枯 ...

  7. solr 查询获取数量getCount()

    //前期设置好查询条件和参数 long numFound = 0; SolrQuery query = new SolrQuery("*:*"); query.setQuery(& ...

  8. P1720 月落乌啼算钱

    题目背景 (本道题目木有以藏歌曲……不用猜了……) <爱与愁的故事第一弹·heartache>最终章. 吃完pizza,月落乌啼知道超出自己的预算了.为了不在爱与愁大神面前献丑,只好还是硬 ...

  9. 数据库SQL server 删除一张表中的重复记录

    --建立一张表 create table cat( catId int, catName varchar(40) ) --将下边的插入语句,多执行几次. insert into catvalues(1 ...

  10. Python调用Java代码部署及初步使用

    Python调用Java代码部署: jpype下载地址:https://www.lfd.uci.edu/~gohlke/pythonlibs/#jpype 下载的时候需要使用Chrome浏览器进行下载 ...