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.
» Solve this problem[解题思路]
实现题。先检查行列,再检查小9格。
[Code]
1: bool isValidSudoku(vector<vector<char> > &board) {
2: // Start typing your C/C++ solution below
3: // DO NOT write int main() function
4: if(board.size() == 0) return false;
5: int row[9], col[9];
6: for(int i =0; i<9; i++)
7: {
8: memset(row, 0, 9*sizeof(int));
9: memset(col, 0, 9*sizeof(int));
10: for(int j =0; j<9; j++)
11: {
12: if(board[i][j] != '.')
13: {
14: if(row[board[i][j]-49] ==1)
15: return false;
16: row[board[i][j]-49]++;
17: }
18: if(board[j][i] != '.')
19: {
20: if(col[board[j][i]-49] ==1)
21: return false;
22: col[board[j][i]-49]++;
23: }
24: }
25: }
26: for(int i =0; i< 9; i+=3)
27: {
28: for(int j =0; j<9; j+=3)
29: {
30: memset(row, 0, 9*sizeof(int));
31: for(int m=0; m<3; m++)
32: {
33: for(int n =0; n<3; n++)
34: {
35: if(board[m+i][n+j] == '.')
36: continue;
37: if(row[board[m+i][n+j]-49] ==1)
38: return false;
39: row[board[m+i][n+j]-49]++;
40: }
41: }
42: }
43: }
44: return true;
45: }