# 有效的数独

<p>请你判断一个 <code>9x9</code> 的数独是否有效。只需要<strong> 根据以下规则</strong> ，验证已经填入的数字是否有效即可。</p>
<ol>
    <li>数字 <code>1-9</code> 在每一行只能出现一次。</li>
    <li>数字 <code>1-9</code> 在每一列只能出现一次。</li>
    <li>数字 <code>1-9</code> 在每一个以粗实线分隔的 <code>3x3</code> 宫内只能出现一次。（请参考示例图）</li>
</ol>
<p>数独部分空格内已填入了数字，空白格用 <code>'.'</code> 表示。</p>
<p><strong>注意：</strong></p>
<ul>
    <li>一个有效的数独（部分已被填充）不一定是可解的。</li>
    <li>只需要根据以上规则，验证已经填入的数字是否有效即可。</li>
</ul>
<p> </p>
<p><strong>示例 1：</strong></p><img
    src="https://img-service.csdnimg.cn/img_convert/8aed54207b4871a01fb402d8d21a128b.png"
    style="height:250px; width:250px" />
<pre><strong>输入：</strong>board = 
    [["5","3",".",".","7",".",".",".","."]
    ,["6",".",".","1","9","5",".",".","."]
    ,[".","9","8",".",".",".",".","6","."]
    ,["8",".",".",".","6",".",".",".","3"]
    ,["4",".",".","8",".","3",".",".","1"]
    ,["7",".",".",".","2",".",".",".","6"]
    ,[".","6",".",".",".",".","2","8","."]
    ,[".",".",".","4","1","9",".",".","5"]
    ,[".",".",".",".","8",".",".","7","9"]]
<strong>输出：</strong>true
</pre>
<p><strong>示例 2：</strong></p>
<pre><strong>输入：</strong>board = 
    [["8","3",".",".","7",".",".",".","."]
    ,["6",".",".","1","9","5",".",".","."]
    ,[".","9","8",".",".",".",".","6","."]
    ,["8",".",".",".","6",".",".",".","3"]
    ,["4",".",".","8",".","3",".",".","1"]
    ,["7",".",".",".","2",".",".",".","6"]
    ,[".","6",".",".",".",".","2","8","."]
    ,[".",".",".","4","1","9",".",".","5"]
    ,[".",".",".",".","8",".",".","7","9"]]
<strong>输出：</strong>false
<strong>解释：</strong>除了第一行的第一个数字从<strong> 5</strong> 改为 <strong>8 </strong>以外，空格内其他数字均与 示例1 相同。 但由于位于左上角的 3x3 宫内有两个 8 存在, 因此这个数独是无效的。</pre>
<p> </p>
<p><strong>提示：</strong></p>
<ul>
    <li><code>board.length == 9</code></li>
    <li><code>board[i].length == 9</code></li>
    <li><code>board[i][j]</code> 是一位数字或者 <code>'.'</code></li>
</ul>

以下程序实现了这一功能，请你填补空白处内容：

```cpp
#include <bits/stdc++.h>
using namespace std;
class Solution
{
public:
	bool isValidSudoku(vector<vector<char>> &board)
	{
		for (int i = 0; i < board.size(); i++)
		{
			vector<bool> mark(10);
			for (int j = 0; j < board.size(); j++)
			{
				if (!valid(board, mark, i, j))
				{
					return false;
				}
			}
		}
		for (int j = 0; j < board.size(); j++)
		{
			vector<bool> mark(10);
			for (int i = 0; i < board.size(); i++)
			{
				if (!valid(board, mark, i, j))
				{
					return false;
				}
			}
		}
		for (int k = 0; k < board.size(); k++)
		{
			int sr = k / 3 * 3;
			int sc = (k % 3) * 3;
			vector<bool> mark(10);
			for (int i = sr; i < sr + 3; i++)
			{
				________________________;
			}
		}
		return true;
	}
private:
	bool valid(vector<vector<char>> &board, vector<bool> &mark, int i, int j)
	{
		if (board[i][j] != '.')
		{
			int index = board[i][j] - '0';
			if (mark[index])
			{
				return false;
			}
			else
			{
				mark[index] = 1;
			}
		}
		return true;
	}
};
```

## template

```cpp
#include <bits/stdc++.h>
using namespace std;
class Solution
{
public:
	bool isValidSudoku(vector<vector<char>> &board)
	{
		for (int i = 0; i < board.size(); i++)
		{
			vector<bool> mark(10);
			for (int j = 0; j < board.size(); j++)
			{
				if (!valid(board, mark, i, j))
				{
					return false;
				}
			}
		}
		for (int j = 0; j < board.size(); j++)
		{
			vector<bool> mark(10);
			for (int i = 0; i < board.size(); i++)
			{
				if (!valid(board, mark, i, j))
				{
					return false;
				}
			}
		}
		for (int k = 0; k < board.size(); k++)
		{
			int sr = k / 3 * 3;
			int sc = (k % 3) * 3;
			vector<bool> mark(10);
			for (int i = sr; i < sr + 3; i++)
			{
				for (int j = sc; j < sc + 3; j++)
				{
					if (!valid(board, mark, i, j))
					{
						return false;
					}
				}
			}
		}
		return true;
	}
private:
	bool valid(vector<vector<char>> &board, vector<bool> &mark, int i, int j)
	{
		if (board[i][j] != '.')
		{
			int index = board[i][j] - '0';
			if (mark[index])
			{
				return false;
			}
			else
			{
				mark[index] = 1;
			}
		}
		return true;
	}
};
```

## 答案

```cpp
for (int j = sc; j < sc + 3; j++)
{
	if (!valid(board, mark, i, j))
	{
		return false;
	}
}
```

## 选项

### A

```cpp
for (int j = sc; j < sc; j++)
{
	if (!valid(board, mark, i, j))
	{
		return false;
	}
}
```

### B

```cpp
for (int j = sc; j < sc + 3; j++)
{
	if (valid(board, mark, i, j))
	{
		return false;
	}
}
```

### C

```cpp
for (int j = sc; j < sc; j++)
{
	if (valid(board, mark, i, j))
	{
		return false;
	}
}
```