[Leetcode] Sudoku solver (iteration)

Source: Internet
Author: User

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

Empty cells are indicated by the character‘.‘.

You may assume that there will be only one unique solution.

A Sudoku puzzle...

 

... And its solution numbers marked in red.

 

Method: the key to solving this problem is to try it one by one in the alternative set. Not every space can start with a unique fixed number.

class Solution {public:    void solveSudoku(vector<vector<char> > &board) {        vector<set<char>> rowMap(9);        vector<set<char>> colMap(9);        vector<set<char>> boxMap(9);        vector<pair<int,int>> blank;        for(int i=0;i<9;i++)        {            for(int j=0;j<9;j++)            {                if(board[i][j]==‘.‘)                {                    blank.push_back(pair<int,int>(i,j));                    continue;                }                rowMap[i].insert(board[i][j]);            }        }        for(int j=0;j<9;j++)        {            for(int i=0;i<9;i++)            {                if(board[i][j]==‘.‘)                    continue;                colMap[j].insert(board[i][j]);            }        }        for(int i=0;i<9;i=i+3)        {            for(int j=0;j<9;j=j+3)            {                vector<int> mp(10,0);                for(int k=0;k<3;k++)                {                    for(int m=0;m<3;m++)                    {                        if(board[i+k][j+m]==‘.‘)                            continue;                        boxMap[(i/3) * 3 +j/3].insert(board[i+k][j+m]);                    }                }            }        }        found = false;        DFS(0,blank,rowMap,colMap,boxMap,board);    }private:    void DFS(int t, vector<pair<int,int>> &blank,  vector<set<char>> &rowMap,  vector<set<char>> &colMap,  vector<set<char>> &boxMap, vector<vector<char> > &board)    {        if(t>=blank.size())        {            found = true;        }        else        {            int i= blank[t].first;            int j= blank[t].second;            for(char digit =‘1‘;digit<=‘9‘;digit++)            {                if(rowMap[i].count(digit)>0 || colMap[j].count(digit)>0 || boxMap[i/3 * 3 + j/3].count(digit)>0)                {                    continue;                }                board[i][j]=digit;                rowMap[i].insert(digit);                colMap[j].insert(digit);                boxMap[i/3*3+j/3].insert(digit);                DFS(t+1,blank,rowMap,colMap,boxMap,board);                rowMap[i].erase(digit);                colMap[j].erase(digit);                boxMap[i/3*3+j/3].erase(digit);                if(found)                    return;            }        }    }private:    bool found;};

 

 

[Leetcode] Sudoku solver (iteration)

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.