码迷,mamicode.com
首页 > 其他好文 > 详细

[LeetCode] Sudoku Solver(迭代)

时间:2014-08-26 00:18:35      阅读:290      评论:0      收藏:0      [点我收藏+]

标签:style   blog   http   color   io   for   ar   问题   div   

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.

bubuko.com,布布扣

A sudoku puzzle...

 

bubuko.com,布布扣

...and its solution numbers marked in red.

 

方法:解此问题的关键是要在备选集合里挨个进行试,不是每个空格一开始只有一个唯一的固定数可以填的

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(迭代)

标签:style   blog   http   color   io   for   ar   问题   div   

原文地址:http://www.cnblogs.com/Xylophone/p/3936163.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!