## 一.题目描述
Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules:
[http://sudoku.com.au/TheRules.aspx](http://sudoku.com.au/TheRules.aspx) .
The Sudoku board could be partially filled, where empty cells are filled with the character ‘.’.
The following figure: A partially filled sudoku which is valid.
![](https://box.kancloud.cn/2016-01-05_568bb5eb210ef.jpg)
## 二.题目分析
关于Sudoku矩阵的性质,可以简单概括为:对于矩阵中每一行、每一列以及每个`3×3`的九宫格区域是否存在唯一的`0~9`排列组合,如果存在相同的元素,则该Sudoku矩阵不合法。
这道题比较简单,就是遍历Sudoku矩阵的所有元素,检查各元素是否满足Sudoku的性质。对于判断一个元素是否在某一行,某一列或者某个小区域内,我定义了一个用于存放0~9数字出现次数的`map`,使用`'1','2',...,'9'`作为关键字只要某个关键字的储存对象大于1,表示在该行、列或`3×3`的九宫格区域中有重复的某个关键字,通过这种方式进行遍历即可判断矩阵是否合法。
## 三.示例代码
~~~
#include <iostream>
#include <vector>
#include <map>
using namespace std;
class Solution
{
public:
bool isValidSudoku(vector<vector<char> >& board)
{
map<char, int> sudokuMap;
for (int i = 0; i < 9; i++)
{
sudokuMap.clear();
for (int j = 0; j < 9; j++)
{
sudokuMap[board[i][j]]++;
if ((board[i][j] != '.') && (sudokuMap[board[i][j]] > 1))
return false;
}
}
for (int i = 0; i < 9; i++)
{
sudokuMap.clear();
for (int j = 0; j < 9; j++)
{
sudokuMap[board[j][i]]++;
if ((board[j][i] != '.') && (sudokuMap[board[j][i]] > 1))
return false;
}
}
for (int i = 0; i < 9; i += 3)
{
for (int j = 0; j < 9; j += 3)
{
sudokuMap.clear();
for (int k = i; k < i + 3; k++)
{
for (int l = j; l < j + 3; l++)
{
sudokuMap[board[k][l]]++;
if ((board[k][l] != '.') && (sudokuMap[board[k][l]] > 1))
return false;
}
}
}
}
return true;
}
};
~~~
运行结果如下:
1.一个正确的数独矩阵:
![](https://box.kancloud.cn/2016-01-05_568bb5eb359b6.jpg)
2.一个错误的数独矩阵:
![](https://box.kancloud.cn/2016-01-05_568bb5eb62306.jpg)
## 四.小结
这道题不要求实现什么算法,只需设计一下矩阵运算,使用`map`用于存放每行、每列或每个九宫格中`1~9`每个数出现的次数,只要发现有大于一的,即可立即否定这个Sudoku,只有遍历整个Sudoku矩阵且符合要求,才判定为valid sudoku。
相关题目:Sudoku solver
- 前言
- 2Sum
- 3Sum
- 4Sum
- 3Sum Closest
- Remove Duplicates from Sorted Array
- Remove Duplicates from Sorted Array II
- Search in Rotated Sorted Array
- Remove Element
- Merge Sorted Array
- Add Binary
- Valid Palindrome
- Permutation Sequence
- Single Number
- Single Number II
- Gray Code(2016腾讯软件开发笔试题)
- Valid Sudoku
- Rotate Image
- Power of two
- Plus One
- Gas Station
- Set Matrix Zeroes
- Count and Say
- Climbing Stairs(斐波那契数列问题)
- Remove Nth Node From End of List
- Linked List Cycle
- Linked List Cycle 2
- Integer to Roman
- Roman to Integer
- Valid Parentheses
- Reorder List
- Path Sum
- Simplify Path
- Trapping Rain Water
- Path Sum II
- Factorial Trailing Zeroes
- Sudoku Solver
- Isomorphic Strings
- String to Integer (atoi)
- Largest Rectangle in Histogram
- Binary Tree Preorder Traversal
- Evaluate Reverse Polish Notation(逆波兰式的计算)
- Maximum Depth of Binary Tree
- Minimum Depth of Binary Tree
- Longest Common Prefix
- Recover Binary Search Tree
- Binary Tree Level Order Traversal
- Binary Tree Level Order Traversal II
- Binary Tree Zigzag Level Order Traversal
- Sum Root to Leaf Numbers
- Anagrams
- Unique Paths
- Unique Paths II
- Triangle
- Maximum Subarray(最大子串和问题)
- House Robber
- House Robber II
- Happy Number
- Interlaving String
- Minimum Path Sum
- Edit Distance
- Best Time to Buy and Sell Stock
- Best Time to Buy and Sell Stock II
- Best Time to Buy and Sell Stock III
- Best Time to Buy and Sell Stock IV
- Decode Ways
- N-Queens
- N-Queens II
- Restore IP Addresses
- Combination Sum
- Combination Sum II
- Combination Sum III
- Construct Binary Tree from Inorder and Postorder Traversal
- Construct Binary Tree from Preorder and Inorder Traversal
- Longest Consecutive Sequence
- Word Search
- Word Search II
- Word Ladder
- Spiral Matrix
- Jump Game
- Jump Game II
- Longest Substring Without Repeating Characters
- First Missing Positive
- Sort Colors
- Search for a Range
- First Bad Version
- Search Insert Position
- Wildcard Matching