Leetcode Sudoku Solver

Source: Internet
Author: User

Title Address: https://leetcode.com/problems/sudoku-solver/

Problem Analysis: Use the most stupid method, the line priority traversal need to fill the empty, use 1 to 9 to try, and then determine whether to meet the conditions, if not meet the criteria to try the next number, if the condition is eligible to try to fill the next empty. Implemented using recursive methods.

Topic Answer:

 Public classSolution { Public voidSolvesudoku (Char[] board) {Solvesudoku (board,0,0); }         Public BooleanSolvesudoku (Char[] board,intRowintcolumn) {        if(Row >= 9){            return true; }                if(Column >= 9){            returnSolvesudoku (board,row+1,0); }                if(Board[row][column] = = '. '){             for(intK = 1;k<=9;k++) {Board[row][column]= (Char) (' 0 ' +k); if(IsValid (Board,row,column) &&solvesudoku (board,row,column+1)){                    return true; } Board[row][column]= '. '; }            return false; }Else {            returnSolvesudoku (board,row,column+1); }    }        Private BooleanIsValid (Char[] board,intRowintcolumn) {         for(inti = 0;i<9;i++){            if(I!=row && board[i][column] = =Board[row][column]) {                return false; }        }                 for(inti = 0;i<9;i++){            if(I!=column && board[row][i] = =Board[row][column]) {                return false; }        }                intBoxstartrow = (ROW/3); intBoxstartcolumn = (COLUMN/3);  for(inti = boxstartrow;i<boxstartrow+3;i++){             for(intj = boxstartcolumn;j<boxstartcolumn+3;j++){                if(i!=row&&j!=column&&board[i][j]==Board[row][column]) {                    return false; }            }        }        return true; }}

Leetcode Sudoku Solver

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.