【问题描述】
Determine if a Sudoku is valid, according to: .
The Sudoku board could be partially filled, where empty cells are filled with the character '.'.
A partially filled sudoku which is valid.
Note:
A valid Sudoku board (partially filled) is not necessarily solvable. Only the filled cells need to be validated.
【解决方案】
-
bool isValidSudoku(char** board, int boardRowSize, int boardColSize) {
-
bool flag[9] = { 0 };
-
int i, j;
-
-
/* rows */
-
for (i = 0; i < boardRowSize; i++)
-
{
-
for (j = 0; j < boardColSize; j++)
-
{
-
if (board[i][j] != '.')
-
{
-
int n = board[i][j] - '1'; /* digits start with 1 */
-
if (flag[n]) return false;
-
else flag[n] = true;
-
}
-
}
-
memset(flag, 0, sizeof(flag));
-
}
-
-
/* columns */
-
for (j = 0; j < boardColSize; j++)
-
{
-
for (i = 0; i < boardRowSize; i++)
-
{
-
if (board[i][j] != '.')
-
{
-
int n = board[i][j] - '1';
-
if (flag[n]) return false;
-
else flag[n] = true;
-
}
-
}
-
memset(flag, 0, sizeof(flag));
-
}
-
-
/* boxes */
-
int ii, jj;
-
for (ii = 0; ii < boardRowSize; ii += 3)
-
{
-
for (jj = 0; jj < boardColSize; jj += 3)
-
{
-
for (i = 0; i < 3; i++)
-
{
-
for (j = 0; j < 3; j++)
-
{
-
if (board[i + ii][j + jj] != '.')
-
{
-
int n = board[i + ii][j + jj] - '1';
-
if (flag[n]) return false;
-
flag[n] = true;
-
}
-
}
-
}
-
memset(flag, 0, sizeof(flag));
-
}
-
}
-
return true;
-
}
阅读(255) | 评论(0) | 转发(0) |