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 ()