leetcode_36 question--valid Sudoku ()

Source: Internet
Author: User

Valid SudokuTotal accepted:33447 Total submissions:122453my submissions QuestionSolution

Determine if a Sudoku is valid, according To:sudoku puzzles-the Rules.

The Sudoku board could be partially filled, where empty cells is 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.

Hide TagsHash TableHas you met this question in a real interview? Yes No

Discuss

This question asks is whether Gongge is valid, only for already filled the number, but is not has the solution question, here needs to consider is each row each column, also has each nine gongge whether in already filled the number to appear

Repeated, so think of is a two-cycle, followed by the downward traversal, and in the process of three judgments, and three arrays to record, row, column, nine Gongge, and here nine Gongge is used to convert i,j into the first few nine Gongge

#include <iostream> #include <vector>using namespace Std;int flag1[10];int flag2[10][10];int flag3[10][10] ; int Changeij (int i,int j) {int result;if (i<=2) Result=j/3;else if (i>2&&i<=5) Result=3+j/3;elseresult =6+j/3;return result;} BOOL Isvalidsudoku (vector<vector<char>>& board) {memset (flag2,0,sizeof (Flag2)); Memset (flag3,0, sizeof (FLAG3)), for (int i=0;i!=9;i++) {memset (flag1,0,sizeof (FLAG1)), for (int j=0;j!=9;j++) {if (board[i][j]!= '. ') {if (flag1[board[i][j]-48]==1) return false;elseflag1[board[i][j]-48]=1;if (flag2[board[i][j]-48][j]==1) return False;elseflag2[board[i][j]-48][j]=1;if (Flag3[changeij (i,j)][board[i][j]-48]==1) return False;elseflag3[changeij (I,J)] [Board[i][j]-48]=1;}}} return true;} int main () {vector<vector<char> > vec=[".... 5..1. ",". 4.3 ... "," ... 3..1 "," 8......2. ",".. 2.7 ..... ",". 15 ... "," ... 2 ... ",". 2.9 ... ",". 4 ... "];cout<<changeij (4,2) <<endl;}

  

leetcode_36 question--valid Sudoku ()

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.