Advertisement
peienwu1216

數獨

Mar 30th, 2021
1,227
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.72 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. #define ios ios::sync_with_stdio(0),cin.tie(0)
  3. using namespace std;
  4.  
  5. class Sudoku{
  6. private:
  7.     int maze[9][9];
  8.     bool flag = false;
  9.    
  10. public:
  11.     void print(string s);//
  12.     void scan_maze();//
  13.     vector<int> select(int, int);
  14.     int next_empty(int, int);
  15.     void solving();
  16.     void dfs(int row, int col );
  17.    
  18. };
  19.  
  20. void Sudoku::print(string s){
  21.     cout<<endl<<"====="<<s<<"======"<<endl;
  22.     for (int i=0; i<9;i++){
  23.         cout<<"|";
  24.         for (int j=0;j<9;j++){
  25.             cout<<maze[i][j];
  26.             if(j%3==2)cout<<"|";
  27.         }
  28.         cout<<endl;
  29.         if(i%3==2)cout<<"-------------"<<endl;
  30.     }
  31. }
  32.  
  33. vector<int> Sudoku::select(int r, int c){
  34.     bool box[9]={0};
  35.    
  36.     for(int i=0;i<9;i++){
  37.         if(maze[i][c]>0)box[maze[i][c]-1] = 1;
  38.         if(maze[r][i]>0)box[maze[r][i]-1] = 1;
  39.     }
  40.     int row_start = 3*(r/3),col_start = 3*(c/3);
  41.  
  42.     for(int i=0;i<3;i++){
  43.         for(int j=0;j<3;j++){
  44.             if(maze[row_start+i][col_start+j]>0)
  45.                 box[maze[row_start+i][col_start+j]-1] = 1;
  46.         }
  47.     }
  48.     vector<int> ans;
  49.     for(int i=0;i<9;i++)
  50.         if(!box[i])ans.push_back(i+1);
  51.     return ans;
  52. }
  53.  
  54. int Sudoku::next_empty(int row, int col){
  55.     int ind = col;
  56.     for(int i=row;i<9;i++){
  57.         while(ind<9){
  58.             if(maze[i][ind]==0){
  59.                 int pos = i*9+ind;
  60.                 return pos;
  61.             }
  62.             ind++;
  63.         }
  64.         ind = 0;
  65.     }
  66.     return -1;
  67. }
  68.  
  69. void Sudoku::dfs(int row, int col ){
  70.     int pos = next_empty(row, col),nr,nc;
  71.     if(pos == -1){
  72.         flag = true;
  73.         return;
  74.     }
  75.     vector<int> candidate = select(row, col);
  76.     int len = candidate.size();
  77.    
  78.     for(int i=0;i<len;i++){
  79.         maze[row][col] = candidate[i];
  80.         pos = next_empty(row, col);
  81.         nr = pos/9; nc = pos%9;
  82.         dfs(nr, nc);
  83.         if(flag)return;
  84.     }
  85.     maze[row][col] = 0;
  86. }
  87. void Sudoku::scan_maze(){
  88.     for(int i=0;i<9;i++){
  89.         for(int j=0;j<9;j++){
  90.             char temp;cin>>temp;
  91.             if(temp=='.')maze[i][j] = 0;
  92.             else maze[i][j] = temp -'0';
  93.         }
  94.     }
  95. }
  96.  
  97. void Sudoku::solving(){
  98.     int first_empty = next_empty(0, 0),nr,nc;
  99.     nr = first_empty/9; nc = first_empty%9;
  100.     dfs(nr, nc);
  101.     bool f = false;
  102.     for(int i=0;i<9;i++){
  103.         for(int j=0;j<9;j++){
  104.             vector<int> v = select(i,j);
  105.             if(v.size()>0)f = true;
  106.         }
  107.     }
  108.     if(!flag || f)cout<<"No solution."<<endl;
  109.     else{
  110.         print("solved");
  111.     }
  112.     flag = false;
  113. }
  114.  
  115. int main(){
  116.     ios;
  117.    
  118.     Sudoku sodoku1;
  119.     sodoku1.scan_maze();
  120.     sodoku1.print("begin");
  121.     sodoku1.solving();
  122.    
  123. }
  124.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement