Leetcode Sudoku Solver

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...

 

If you encounter problems with jiugongge, you can find and solve them in depth.

class Solution {public:    bool isValidRow(int row , vector<vector<char> >& board){        vector<int> cnt(10,0);        for(int col = 0; col < 9; ++ col){            char item = board[row][col];            if(item != ‘.‘){                if(cnt[item-‘0‘]!=0) return false;                else cnt[item-‘0‘]++;            }        }        return true;    }        bool isValidCol(int col, vector<vector<char> >& board ){        vector<int> cnt(10,0);        for(int row = 0; row < 9; ++ row){            char item = board[row][col];            if(item != ‘.‘){                if(cnt[item-‘0‘]!=0) return false;                else cnt[item-‘0‘]++;            }        }        return true;    }        bool isValidBox(int i,int j, vector<vector<char> >& board){        vector<int> cnt(10,0);        for(int row = 3*i;row < 3*i+3; ++row){            for(int col = 3*j; col < 3*j+3; ++col){                char item = board[row][col];                if(item != ‘.‘){                    if(cnt[item-‘0‘]!=0) return false;                    else cnt[item-‘0‘]++;                }            }        }        return true;    }    bool isValidSudoku(int x, int y,vector<vector<char> > &board) {        return isValidRow(x,board)&&isValidCol(y,board)&&isValidBox(x/3,y/3,board);    }        bool dfs(int pos ,vector<vector<char> >& board){        int n = board.size();        if(pos == n*n) return true;        int x = pos/n, y = pos%n;        if(board[x][y]==‘.‘){            for(char i = ‘1‘; i<=‘9‘;++ i){                board[x][y]=i;                if(isValidSudoku(x,y,board) && dfs(pos+1,board)) return true;                board[x][y]=‘.‘;            }        }else{            if(dfs(pos+1,board)) return true;        }        return false;    }        void solveSudoku(vector<vector<char> > &board) {        dfs(0,board);    }};

 

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.