This topic is a "Sudoku" problem solving, Sudoku is composed of 9 nine Gongge, the rule is that each row does not include the number 1~9, and each nine Gongne is also composed of 1~9, now let us judge whether the Sudoku is legal.
This question is more conventional, we act according to the rules, since the rule of the Sudoku is: each row, each column, each house , is a digital 1~9, (this problem does not give the number, not given the place to use '. ' instead), so we just need to judge each row, each column, the number of each house character is qualified can
This topic is only to examine the rules of the master can solve problems, it is not difficult, so I did not carefully explain, if we want to give the solution of the Sudoku problem, it is troublesome, remember the algorithm inside study backtracking, I estimate to use the backtracking method, really very difficult to write code.
The code is as follows:
Class Solution {Public:bool Isvalidsudoku (vector<vector<char>>& board) {//Determines whether the elements in the nine lattice are legal, only the elements that have been filled, Unfilled elements do not take into account */* The Law of nine Gongge is that each row of each column contains 1~9 each, so we just need to determine whether each row of each column of the filled element of each house is between 1~9 and no duplicates, so we can use set to solve the problem ... */char *tmp = new Char[9] ; for (int i = 0; i<9; ++i) {int count = 0;for (int j = 0; j<9; ++j) {tmp[count++] = board[i][j];} if (!check (TMP)) {return false;} Count = 0;for (int j = 0; j<9; ++j) {tmp[count++] = board[j][i];} if (!check (TMP)) {return false;} int x = (I/3) * 3;int y = (i% 3) * 3;count = 0;for (int j = x; j< x + 3; ++j) {for (int k = y; k < y + 3; ++k) {tmp [count++] = Board[k][j];}} if (!check (TMP)) {return false;}} Delete[] Tmp;return true;} BOOL Check (char* str) {set<char> S; S.clear (); for (int i = 0; i<9; ++i) {if (str[i] = = '. ') {continue;} if (Str[i] <= ' 0 ' && str[i] > ' 9 ') {return false;} Else{if (S.find (str[i]) = = S.end ()) {S.insert (str[i]);} Else{return false;}}} return true;}};
Written question 49. Leetcode OJ (36)