标签:
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.
不理解数独的概念的人好亏。
1.每行每列的数字是1~9,且不得重复
2.空的填‘.‘
3.每个九宫格也是数字1~9,且不得重复
class Solution { public: bool isValidSudoku(vector<vector<char>>& board) { char temp; map<char,bool> row; map<char,bool> column; map<char,bool> grid; for(int i=0;i<9;i++){ //先检查每行 for(int j=0;j<9;) { temp=board[i][j]; if(‘1‘<=temp<=‘9‘&&!row[temp]) {j++; row[temp]=true;} else if(temp==‘.‘) j++; else return false; } row.clear(); } //再检查每列 for(int m=0;m<9;m++){ for(int k=0;k<9;) { temp=board[k][m]; if(‘1‘<=temp<=‘9‘&&!column[temp]) {k++; column[temp]=true;} else if(temp==‘.‘) k++; else return false; } column.clear(); } //在检查每个九宫格 int hang=0,lie=0; for(int l=0;l<9;l++){ for(int q=0;q<9;q++){ hang=(l/3)*3+q/3; lie=q%3+(l%3)*3; temp=board[hang][lie]; if(‘1‘<=temp<=‘9‘&&!grid[temp]) {grid[temp]=true;continue;} else if(temp==‘.‘) continue; else return false; } grid.clear(); } return true; } };
标签:
原文地址:http://www.cnblogs.com/LUO77/p/5037226.html