Leetcode-----Sudoku Solution every day

Source: Internet
Author: User
Tags bool valid
Valid Sudoku

Original title link valid Sudoku

Determines whether a given Sudoku disk is valid, and there may be empty positions in the Sudoku disk.

Briefly describe the rules of Sudoku, refer to connecting Sudoku Puzzles-the. Each row, the number 1-9 can only appear once each column, the number 1-9 can only appear once each 3 * 3 squares, the number 1-9 can only appear once. Here 3 * 3 squares include only 9, that is, the image of the bold black line separate 9 squares

In fact, each point can only exist one of 1-9 of these nine numbers, satisfy each row, each column, each 3 * 3 squares can not appear duplicate numbers

To determine whether a Sudoku disk is valid, just determine whether the above three rules are met. Also, it doesn't matter if the given Sudoku is not filled with numbers like pictures. Only need to judge the part of the number is good, for example, the first line is only 5, 3, 7, then you can judge the first line does not appear duplicate numbers, is to meet the rule one.

Definitely need to traverse all over again, each traversal to a position, to determine its row, where the column, where the 3 * 3 squares are already existing the same number, if present, return false, otherwise, add this number to the row, column, 3 * 3 in the record of the grid.
So you need to record each row separately, each column, every 3 * 3 squares have those numbers appear, in fact, is 3 two-dimensional array records what numbers appear in each row,vector<vector<int>> rows (9, Vector<int > (10, 0)); Record what numbers appear in each column,vector<vector<int>> columns (9, vector<int> (10, 0)); Record what numbers appear in each of the 3 * 3 squares,vector<vector<int>> boxes (9, vector<int> (10, 0));

for 3 * 3 squares, here with vector<vector<int>> boxes (9, vector<int> (10, 0)), the form means that the Sudoku is made up of 9 3 * 3 squares, numbered from 0 to 8. For a location (row, column), the 3 * 3 squares It is in is numbered ROW/3 * 3 + COLUMN/3.

Representation method, assuming that the current position is (I, j), the number is n Rows[i][n] indicates the number of number n on line I, either 1 or 0 Columns[j][n] indicates the number of number n in column J, not 1 is 0 BOXES[I/3 * 3 + j/3][n] represents the current position The number of numbers n is present in the square, either 1 or 0.

The code is as follows

class Solution {Public:bool Isvalidsudoku (vector<vector<char>>& board) {
        vector<vector<int>> rows (9, vector<int> (10, 0));
        vector<vector<int>> columns (9, vector<int> (10, 0));

        Vector<vector<int>> boxes (9, vector<int> (10, 0));
                for (int i = 0; i < board.size (), ++i) {for (int j = 0; J < board[i].size (); ++j) {
                    if (board[i][j] = = '. ')

                Continue
                int n = board[i][j]-' 0 ';
                    /* If previous occurrences (not 0), the Sudoku disk is invalid */if (Rows[i][n] | | columns[j][n] | | BOXES[I/3 * 3 + j/3][n])
                return false; /* Otherwise, update each row, each column, the contents of the grid */else rows[i][n] = columns[j][n] = BOXES[I/3 * 3 + j/3][
            n] = 1;
    }} return true; }

};

Extended Sudoku Solver

Original title Link Sudoku Solver

Given a valid Sudoku disk, the results are solved.
To solve a Sudoku disk is required to fill all the blanks in the number, the request is still satisfied with the above three rules, that is, each row, the number 1-9 can only appear once each column, the number 1-9 can only appear once per 3 * 3 squares, the number 1-9 can only appear once. Here 3 * 3 squares include only 9, that is, the image of the bold black line separate 9 squares

For a space, the number that it can fill will need to meet in the same row did not appear in the row did not appear in the 3 * 3 box did not appear

So in the above question, already each row, each column, each 3 * 3 squares appears the number has found, next is the depth first (DFS) fills each space the number, certainly fills the number to satisfy above request.
If there is no optional number in the fill to a location, it means that one of the previous locations was selected incorrectly, then fall back to the previous position and select another number that meets the above requirements

The code is as follows

Class Solution {Public:void Solvesudoku (vector<vector<char>>& board) {Vector<vector<i
        nt>> rows (9, vector<int> (10, 0));
        vector<vector<int>> columns (9, vector<int> (10, 0));

        Vector<vector<int>> boxes (9, vector<int> (10, 0));  /* Calculates each row, each column, whether each number appears in each 3 * 3 box */for (int i = 0; i < board.size (); ++i) {for (int j = 0; j < Board[i].size ();
                    ++J) {if (board[i][j] = = '. ')

                Continue
                int n = board[i][j]-' 0 ';
            Rows[i][n] = Columns[j][n] = BOXES[I/3 * 3 + j/3][n] = 1;
        }} bool done = FALSE;   
    DFS (board, rows, columns, boxes, 0, 0, done);
             } private:void DFS (vector<vector<char>>& board, vector<vector<int>>& rows, vector<vector<int>>& columns, VECTOR&LT;VECTOR&LT;INT&GT;&Gt;& boxes, int row, int column, bool& done) {/* Fill complete */if (row >= board.size (
            ) {done = true;
        Return }/* At the end of the current line, change to the next line */else if (column >= board[row].size ()) {DFS (board, rows, Colu
        MNS, boxes, row + 1, 0, done);
        }/* If there are numbers, do not need to populate, continue next */else if (board[row][column]! = '. ')
        {DFS (board, rows, columns, boxes, row, column + 1, done); 
                } else {for (int n = 1; n <= 9; ++n) {/* If the number appears, it cannot be populated to the current position */ if (Rows[row][n] | | columns[column][n] | | BOXES[ROW/3 * 3 + column/3][n]) continue

                ;
                /* Record populated numbers down */rows[row][n] = columns[column][n] = BOXES[ROW/3 * 3 + column/3][n] = 1;
                Board[row][column] = n + ' 0 '; /* Recursively fills the next space */DFS (board, rows, colUmns, boxes, row, column + 1, done);
                /* If completed, exit */if (done) {return; }/* Otherwise, go back to the state before the fill, re-find the number */else {rows[row][n] = Colum
                    Ns[column][n] = BOXES[ROW/3 * 3 + column/3][n] = 0;
                Board[row][column] = '. '; }
            }
        }
    }
};

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.