[LeetCode] Sudoku Solver(迭代)

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.

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

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;

};
时间: 2024-11-08 23:22:11

[LeetCode] Sudoku Solver(迭代)的相关文章

LeetCode: Sudoku Solver [036]

[题目] 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. [题意] 解

leetcode Sudoku Solver python

# 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. class Sol

[LeetCode] Sudoku Solver 解数独,递归,回溯

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. Hide Tags B

LeetCode:Sudoku Solver &amp;&amp; Valid Sudouku

其实数独还是我挺喜欢的一个游戏.原来有本数独的书. 其实Sudoku是基于Valid Sudouku.其实一开始有点想太多.基于平常玩数独的经验,有很多解数独的规则.貌似这个人为判断因素比较多. 而且一开始理解的valid是有解无解,其实这里要求的是给定的board里的数字是否符合规则,不一定能解. 其实我这里有3个函数,一个是判断行除该位置外是否有相同值的,一个是列,最后一个是小正方形里. 而求解使用的是暴力,一个个尝试. public class SudokuSolver {        

[LeetCode] Sudoku Solver 求解数独

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. 这道求解数独的题是在之

【leetcode刷题笔记】Sudoku Solver

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. 题解:递归.在每个空位

LeetCode:Valid Sudoku,Sudoku Solver(数独游戏)

Valid Sudoku Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules. The Sudoku board could be partially filled, where empty cells are filled with the character '.'. A partially filled sudoku which is valid. Note: A valid Sudoku boa

[LeetCode][JavaScript]Sudoku Solver

Sudoku Solver 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 re

Sudoku Solver leetcode java

题目: 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. 题解: 第一反