[Leetcode] 036. Valid Sudoku (Easy) (c + +) __c++

Source: Internet
Author: User

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);
 }
};


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.