Sudoku Solver
来源:互联网 发布:两组数据差值的标准差 编辑:程序博客网 时间:2024/06/08 16:41
class Solution {private: vector<int> xEmptyPos; vector<int> yEmptyPos; bool isValid(int x,int y,char val,const vector<vector<char> > &board) { int cnt[10]; memset(cnt,0,sizeof(cnt)); //row validate for(int i=0;i<9;++i) { if(board[x][i]!='.') { cnt[board[x][i]-'0']++; } } if(cnt[val-'0']!=0) { return false; } memset(cnt,0,sizeof(cnt)); //col validate for(int i=0;i<9;++i) { if(board[i][y]!='.') { cnt[board[i][y]-'0']++; } } if(cnt[val-'0']!=0) { return false; } memset(cnt,0,sizeof(cnt)); //group validate int xb=x/3; int yb=y/3; for(int i=0;i<3;++i) { for(int j=0;j<3;++j) { char c=board[i+xb*3][j+yb*3]; if(c!='.') { cnt[c-'0']++; } } } if(cnt[val-'0']!=0) { return false; } return true; } bool dfs(int index,vector<vector<char> > &board) { if(index>=xEmptyPos.size()) { return true; } for(char a='1';a<='9';++a) { if(isValid(xEmptyPos[index],yEmptyPos[index],a,board)) { board[xEmptyPos[index]][yEmptyPos[index]]=a; if(dfs(index+1,board)) { return true; } else { board[xEmptyPos[index]][yEmptyPos[index]]='.'; } } } return false; }public: void solveSudoku(vector<vector<char> > &board) { for(int i=0;i<9;++i) { for(int j=0;j<9;++j) { if(board[i][j]=='.') { xEmptyPos.push_back(i); yEmptyPos.push_back(j); } } } dfs(0,board); }};
0 0
- Sudoku Solver
- Sudoku Solver
- Sudoku Solver
- Sudoku Solver
- Sudoku Solver
- Sudoku Solver
- Sudoku Solver
- Sudoku Solver
- Sudoku Solver
- Sudoku Solver
- Sudoku Solver
- Sudoku Solver
- Sudoku Solver
- Sudoku Solver
- Sudoku Solver
- Sudoku Solver
- Sudoku Solver
- Sudoku Solver
- C. Vanya and Exams
- 使用Nginx的proxy_cache缓存功能取代Squid[原创]
- PHP参数修改不生效的问题
- 如何判断$_POST传送的<textarea>标签中的值是否为空
- Reorder List
- Sudoku Solver
- Orace新建临时表空间、数据表空间、用户
- Word Ladder II
- Android Activity和Fragment的转场动画
- 表结构文档(考核准备)
- Add Binary
- Valid Sudoku
- 玻璃钢废气净化塔的安装与维护保养
- 16位灰度图之TIFF