判断一个九宫格是否成立

class Solution
{
public:
	bool isValidSudoku(const vector<vector<char>>& board)
	{
		for (int i = 0;i < 9;++i)
		{
			unordered_map<char, bool> a1;//检查行
			unordered_map<char, bool> a2;//检查列
			unordered_map<char, bool> a3;//检查每一个九宫格是否数字重复
			for (int j = 0;j < 9;++j)
			{
				if (board[i][j] != '.')
				{
					if (a1[board[i][j]] == true)
						return false;
					a1[board[i][j]] = false;
				}
				if (board[j][i] != '.')
				{
					if (a2[board[j][i]] == true)
						return false;
					a2[board[j][i]] = true;
				}
//第i个九宫格的第j个格点的行号可表示为i/3*3+j/3,第i个九宫格的第j个格点的列号可表示为i%3*3+j%3
				if (board[i / 3 * 3 + j / 3][i % 3 * 3 + j % 3] != '.')
				{
					if (a3[board[i / 3 *3 + j / 3][i % 3 *3  + j % 3]] == true)
						return false;
					a3[board[i / 3 + j / 3][i % 3 + j % 3]] = true;
				}
			}
			return true;
		}
	}
	
};

猜你喜欢

转载自blog.csdn.net/weixin_39916039/article/details/82564103