Index: [Leetcode] leetcode key index (C++/JAVA/PYTHON/SQL)
Github:https://github.com/illuz/leetcode 036. Valid Sudoku (Easy) link :
Title: https://leetcode.com/problems/valid-sudoku/
Code (GitHub): Https://github.com/illuz/leetcode :
Determine whether a Sudoku is effective.
Effective Sudoku is not forced to have solutions. Analysis :
As long as the same row, column, block without the same number on the line.
Open the number of records on the line, there is no difficulty, you can use the binary to indicate that the table-bit operation accelerated.
(Note is to judge effective, not have solution, I just started to solve, TLE a lot of times ...) ) code :
C++:
class Solution {private:int row[9], col[9], sqr[3][3]; BOOL Check (int x, int y, int val) {return! (row[x]>>val) &1) &&! ((col[y]>>val) &1) &&!
((Sqr[x/3][y/3]>>val) &1);
} void mark (int x, int y, int val) {row[x] |= (1<<val);
Col[y] |= (1<<val);
SQR[X/3][Y/3] |= (1<<val); }<pre name= "code" class= "Java" >//Solver Sudoku
void unmark (int x, int y, int val) {//row[x]-= (1<<val);//col[y]-= (1<<val);//SQR[X/3][Y/3]-= (
1<<val); }//bool Dfs (int pos, vector<vector<char> > &board) {////x = pos/9, y = pos% 9//if (pos = 8
1)//return true; if (board[pos/9][pos%9]!= '. ') {//Return DFS (POS + 1, board);/} else {/for (int i = 0; i < 9; i++)//if (check (POS/9, pos%9, i)) {//
Mark (POS/9, pos%9, i);
if (DFS (POS + 1, board))//return true;
Unmark (POS/9, pos%9, i);
}//}//return false;
} public:bool Isvalidsudoku (vector<vector<char> > &board) {memset (row, 0, sizeof (row));
memset (col, 0, sizeof (COL));
memset (SQR, 0, sizeof (SQR)); for (int i = 0; i < board.size (); i++) for (int j = 0; J < Board[i].size (); j + +) if (Board[i][j]!= '. ')
{if (!check (i, J, Board[i][j]-' 1 ') return false;
Mark (i, J, Board[i][j]-' 1 '); return true;
Return DFS (0, board);
}
};