- tags: LeetCode
https://leetcode.com/problems/valid-sudoku/
<- high -- low ->
+------------------- wow(row(i):0,col(j):0) 0 -> [ 0010, 0010 ]
| 1 -> [ 0000, 0000 ]
| 2 -> [ 0000, 0000 ]
|
| +--------------- wow(row(i):0,col(j):1) 0 -> [ 0010 | 1 = 0011, 0010 ]
| | 1 -> [ 0000, 0000 | 1 = 0001 ]
| | 2 -> [ 0000, 0000 ]
| |
| | +----------- wow(row(i):0,col(j):2) 0 -> [ 0011 | 3 = 0100, 0010 ]
| | | 1 -> [ 0000, 0001 ]
| | | 2 -> [ 0000, 0000 | 3 = 0100 ]
+---+---+---+
| 2 | 1 | 3 |
+---+---+---+
----| 3 | 2 | 1 |
| +---+---+---+
| | 1 | 3 | 2 |
| +---+---+---+ |- wow(row(i):1,col(j):0) 0 -> [0010 | 3 = 0110, 0010]
+---------------------+ 1 -> [0000, 0001 | 3 = 0101] 2 -> [0000, 0100]
class Solution {
public:
bool isValidSudoku(vector<vector<char>>& board) {
vector<int> wow(9,0);
int mux1;
int mux2;
int mux3;
int box_index;
for(int i=0;i<9;i++){
for(int j=0;j<9;j++){
if(board[i][j] == '.'){
continue;
}
mux1 = 0x01 << (board[i][j] - '1');
mux2 = 0x01 << 9 << (board[i][j] - '1');
mux3 = 0x01 << 9 << 9 << (board[i][j] - '1');
box_index = (i/3) * 3 + j/3;
if((wow[i]&mux1) != mux1 && (wow[j]&mux2) != mux2 && (wow[box_index]&mux3) != mux3){
wow[i] = wow[i]|mux1;
wow[j] = wow[j]|mux2;
wow[box_index] = wow[box_index]|mux3;
}
else{
return false;
}
}
}
return true;
}
};