Valid Sudoku

Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules.

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.

class Solution {
public:
    bool isValidSudoku(vector<vector<char>>& board) {
        bool rowFlag[9][9];
	    memset(rowFlag, false, 9*9*sizeof(bool));
	    bool columnFlag[9][9];
	    memset(columnFlag, false, 9*9*sizeof(bool));
	    bool subBoxFlag[3][3][9];
	    memset(subBoxFlag, false, 3*3*9*sizeof(bool));

	    for (int i = 0; i < 9; i++)
	    {
		    for (int j = 0; j < 9; j++)
		    {
			    if (board[i][j] != '.')
			    {
				    int num = board[i][j] - '1';
				    if (rowFlag[i][num])
				    {
					    return false;
				    }
				    else
				    {
					    rowFlag[i][num] = true;
				    }

				    if (columnFlag[j][num])
				    {
					    return false;
				    }
				    else
				    {
					    columnFlag[j][num] = true;
				    }

				    if (subBoxFlag[i/3][j/3][num])
				    {
					    return false;
				    }
				    else
				    {
					    subBoxFlag[i/3][j/3][num] = true;
				    }
			    }
		    }
	    }

	    return true;
    }
};


你可能感兴趣的:(Valid Sudoku)