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