[Leetcode] Valid Sudoku Problem Solving report C language

Source: Internet
Author: User

Topic
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 '.

Note:
A Valid Sudoku board (partially filled) is not necessarily solvable. Only the filled cells need to be validated.
"Problem Analysis"
There are many solutions to this problem, which can be spent in exchange for time or time for space.
The 1th method is a double loop at a time, each traversing a number, then the number as one of the subscript is placed in the corresponding detection array, a total of 3 arrays, detection of rows, columns, small squares.
"The specific code is as follows"

BOOLIsvalidsudoku (Char* * Board,intBoardrowsize,intBoardcolsize) {if((boardrowsize!=9)|| (boardcolsize!=9))return false;intflagbox[9][Ten]={0};intflagcol[9][Ten]={0};intflagrow[Ten]={0};intI,j,k;inttmp for(i=0; i<boardrowsize;i++) { for(j=0; j<boardcolsize;j++) {if(board[i][j]!='. ') {tmp=board[i][j]-' 0 ';if(flagrow[tmp]==0) flagrow[tmp]=1;Else return false;if(flagcol[j][tmp]==0) flagcol[j][tmp]=1;Else return false;if(flagbox[i/3*3+j/3][tmp]==0) flagbox[i/3*3+j/3][tmp]=1;Else return false; }        } for(k=0;k<Ten; k++) {flagrow[k]=0; }    }return true;}

"Personal Summary"
In this way, only a double loop is required to validate the rows, columns, and small squares.

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

[Leetcode]valid Sudoku Problem Solving report C language

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.