Panax Sudoku Solver (Graph; WFS)

Source: Internet
Author: User

Write a program to solve a Sudoku puzzle by filling the empty cells.

Empty cells is indicated by the character ‘.‘ .

Assume that there would be is only one unique solution.

A Sudoku Puzzle ...

... and its solution numbers marked in red.

classSolution { Public:    voidSolvesudoku (vector<vector<Char>> &Board) {Backtracking (board,0); }    BOOLBacktracking (vector<vector<Char>> &board,intLine ) {        //Find first empty cell         for(inti = line; i<9; i++)        {            for(intj =0; j<9; J + +)            {                if(board[i][j]!='.')Continue; //First empty cell is found                 for(intK =1; k<=9; k++)//backtracking method to find the number that should be filled in the empty unit                {                    if(!isvalid (board,i,j,k+'0'))Continue; BOARD[I][J]= k +'0'; if(Backtracking (board,i))return true; } Board[i][j]='.'; return false;//If no valid value is found            }        }        return true;//If no empty cell is found    }    //Write a separate check function for backtracking    BOOLIsValid (vector<vector<Char>> &board,intLineintColumnCharvalue) {        //check a grid of nine Gongge        intUpperboard = line/3*3; intLeftboard = column/3*3;  for(inti =0; i<3; i++)        {             for(intj =0; j<3; J + +)            {                if(Board[upperboard+i][leftboard+j] = = value)return false; }        }             //Check Column         for(inti =0; i<9; i++)        {            if(Board[line][i] = = value)return false; }         for(inti =0; i<9; i++)        {            if(Board[i][column] = = value)return false; }        return true; }};

Panax Sudoku Solver (Graph; WFS)

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.