问题: 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.
There are just 3 rules to Sudoku
1) Each row must have the numbers 1-9 occuring just once.
2) Each column must have the numbers 1-9 occuring just once.
3) And the numbers 1-9 must occur just once in each of the 9 sub-boxes of the grid.
每一行必须是数字1~9且不重复
每一列必须是数字1~9且不重复
每一个小九宫格(互不交叉,总共九个小九宫格)必须是数字1~9且不重复
思路分析
依次检查每行,每列,每个子九宫格是否出现重复元素,如果出现返回false,否则返回true.
难点在于表示第i个九宫格每个格点的坐标。
观察行号规律:
第0个九宫格:000111222; 第1个九宫格:000111222; 第2个九宫格:000111222;
第3个九宫格:333444555; 第4个九宫格:333444555; 第5个九宫格:333444555;
第6个九宫格:666777888; 第7个九宫格:666777888; 第8个九宫格:666777888;
可见对于每三个九宫格行号增3;对于单个九宫格,每三个格点行号增1。
因此第i个九宫格的第j个格点的行号可表示为i/33+j/3(每个小九宫格j都是从0~9递增)
观察列号规律:
第0个九宫格:012012012; 第1个九宫格:345345345; 第2个九宫格:678678678;
第3个九宫格:012012012; 第4个九宫格:345345345; 第5个九宫格:678678678;
第6个九宫格:012012012; 第7个九宫格:345345345; 第8个九宫格:678678678;
可见对于下个九宫格列号增3,循环周期为3;对于单个九宫格,每个格点行号增1,周期也为3。
周期的数学表示就是取模运算mod。
因此第i个九宫格的第j个格点的列号可表示为i%33+j%3(每个小九宫格j都是从0~9递增)
HashSet实现 (Java)
public boolean isValidSudoku(char[][] board) {for (int i = 0; i < 9; i++) {HashSet<Character> row = new HashSet<>();HashSet<Character> column = new HashSet<>();HashSet<Character> cube = new HashSet<>();for (int j = 0; j < 9; j++) {// 检查第i行,在横坐标位置if (board[i][j] != '.' && !row.add(board[i][j]))return false;// 检查第i列,在纵坐标位置if (board[j][i] != '.' && !column.add(board[j][i]))return false;// 行号+偏移量int RowIndex = 3 * (i / 3) + j / 3;// 列号+偏移量int ColIndex = 3 * (i % 3) + j % 3;//每个小九宫格,总共9个if (board[RowIndex][ColIndex] != '.'&& !cube.add(board[RowIndex][ColIndex]))return false;}}return true;}
HashSet实现 (C++)
bool isValidSudoku_hashset(vector<vector<char>>& board) {unordered_set<char> row;unordered_set<char> column;unordered_set<char> cube;for (int i = 0; i < 9; i++) {for (int j = 0; j < 9; j++) {if (board[i][j] != '.' && row.find(board[i][j])!=row.end()) {return false;}else {row.insert(board[i][j]);}if (board[j][i] != '.' && column.find(board[j][i])!=column.end()) {return false;}else {column.insert(board[j][i]);}int row_index = 3 * (i / 3) + j / 3;int column_index = 3 * (i % 3) + j % 3;if (board[row_index][column_index] != '.' && cube.find(board[row_index][column_index])!=cube.end()) {cout << row_index << "," << column_index << endl;return false;}else {cube.insert(board[row_index][column_index]);}}}return true;}
位图法实现 (Java)
// 使用位图法,出现置为1
public boolean isValidSudoku(char[][] board) {
for (int i = 0; i < 9; i++) {
int[] bit_map_row = new int[9];
int[] bit_map_col = new int[9];
int[] bit_map_cube = new int[9];
// 注意值减去1,下标从0开始
for (int j = 0; j < 9; j++) {
if (board[i][j] != '.')
if (bit_map_row[board[i][j] - '1'] == 1)
return false;
else
bit_map_row[board[i][j] - '1'] = 1;
if (board[j][i] != '.')
if (bit_map_col[board[j][i] - '1'] == 1)
return false;
else
bit_map_col[board[j][i] - '1'] = 1;
int RowIndex = 3 * (i / 3) + j / 3;
int ColIndex = 3 * (i % 3) + j % 3;
int val = board[RowIndex][ColIndex];
if (val != '.')
if (bit_map_cube[val - '1'] == 1)
return false;
else
bit_map_cube[val - '1'] = 1;
}
}
return true;
}
