题目

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. 仅用递归函数操作逆序一个栈(Swift 4)

    /// 取出栈底的数 func getAndRemoveLastElement(_ items: inout [Int]) -> Int { let value = items.remove(a ...

  2. Shell 根据名称杀掉进程

    代码如下: #!/bin/sh # 从命令行读取进程名称 NAME=$ echo "---------------" echo 'killing ->' $NAME # 过滤 ...

  3. HDU6438(贪心技巧)

    第一眼喜闻乐见的股票问题dp可以暴力,然鹅时间不允许. 于是考虑怎么贪. 这篇题解说得很生动了. 因为每支股票都有买入的潜力所以肯定都加在优先队列里. 然后考虑的是哪些需要加入两次.这是我第二次见到类 ...

  4. 洛谷p1955[NOI2015]程序自动分析

    题目: 在实现程序自动分析的过程中,常常需要判定一些约束条件是否能被同时满足. 考虑一个约束满足问题的简化版本:假设x1,x2,x3...代表程序中出现的变量,给定n个形如xi=xj或xi≠xj的变量 ...

  5. 水题 Codeforces Round #286 (Div. 2) A Mr. Kitayuta's Gift

    题目传送门 /* 水题:vector容器实现插入操作,暴力进行判断是否为回文串 */ #include <cstdio> #include <iostream> #includ ...

  6. C. Molly's Chemicals 暴力 + 统计技巧

    http://codeforces.com/contest/776/problem/C 一开始做的时候,就发现是预处理前缀和,然后对于每一个前缀和,如果他能成为一个贡献,就是能和前面的某些段 组合成和 ...

  7. Android学习笔记(十六) ContentProvider

    1.相关概念 ContentProvider:不同应用程序之间进行数据交换的标准API:程序“暴露”数据的方法. ContentResolver:一个程序访问另一个程序被“暴露”的数据的方法. Uri ...

  8. (转)Spring4.2.5+Hibernate4.3.11组合开发

    http://blog.csdn.net/yerenyuan_pku/article/details/52887573 搭建和配置Spring与Hibernate整合的环境 今天我们来学习Spring ...

  9. 目录下 shift 右键菜单 打开cmd 或者在 地址栏输入cmd 回车进入cmd

    目录下 shift 右键菜单 打开cmd 或者在 地址栏输入cmd 回车进入cmd

  10. es 集群部署

    下载 [root@localhost ~]# wget https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-6.5.1 ...