标签:
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.
1 class Solution { 2 public: 3 bool isValidSudoku(vector<vector<char>>& board) { 4 bool used[9]; 5 6 for(int i=0;i<9;i++) 7 { 8 fill(used,used+9,false); 9 for(int j=0;j<9;j++) //row 10 { 11 if(check(board[i][j],used)==false) 12 return false 13 } 14 15 fill(used,used+9,false); 16 for(int j=0;j<9;j++) //col 17 { 18 if(check(board[j][i],used)==false) 19 return false; 20 } 21 } 22 23 for(int r=0;r<3;r++) 24 { 25 for(int c=0;c<3;c++) 26 { 27 fill(used,used+9,false); 28 for(int i=r*3;i<r*3+3;i++) 29 { 30 for(int j=c*3;j<c*3+3;j++) 31 if(check(board[i][j],used)==false) 32 return false; 33 } 34 } 35 } 36 37 return true; 38 } 39 40 bool check(char ch,bool used[9]) 41 { 42 if(ch==‘.‘) return true; 43 44 if(used[ch-‘1‘]) return false; 45 46 return used[ch-‘1‘]=true; 47 48 } 49 };
标签:
原文地址:http://www.cnblogs.com/jawiezhu/p/4511880.html