9 * 9 Sudoku

# Question is intended
to enter a string containing 81 characters, representing the data in the box 81, each character is a digit or a '', indicating that the grid does not fill,
assuming there is only one unique number for each solution the end of the program, end represents a file

# Explanations
subscript character string starting from 0 to map it into the subscript in the matrix is (i / 9, i% 9
matrix mapped to the index string subscripts i * 9 + j
a total of 3 * 3 = 9 * 3 3 size squares, index starts from 0, the x / 3 under rounding, y / 3 rounding can be calculated at which a grid is positioned above

Each column of each row, each 3 * 3 grid are represented by a binary digital numbers 1-9 which may be filled
by & calculation is the intersection of digital this position can be filled

P arrays and the preprocessing num arrays,
a corresponding number on the p-array represents 9-bit binary number each bit,
1 to 2 array represents num 9 each number on the number of 1s -1
pruning Optimization:
1 ) optimize the search order to fill a minimum number of position from the beginning of the current legal digital can fill, which can effectively reduce the size of the search tree, improve time complexity, for example, that the only one you can directly fill, the number of backtracking process will fewer
2) to exclude any equivalent redundancy in the number of states are simply looking to fill a position can be, as other locations may be searched in the search deeper layer
3) using the first 1 to seek lowbit

 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 const int N=9;
 4 int r[N],c[N];
 5 int num[1<<N],p[1<<N];
 6 int g[3][3];
 7 char str[81];
 8 inline int lowbit(int x){
 9    return x & -x;
10 }
11 inline int get(int x,int y){
12    return r[x] & c[y] & g[x/3][y/3];
13 }
14 inline void init(){
15    for(int i=0;i<N;i++)
16       r[i] = c[i] = (1 << N) - 1;
17    for(int i=0;i<3;i++)
18       for(int j=0;j<3;j++)
19          g[i][j]=(1<<N)-1;
20 }
21 bool dfs(int cnt){
22    if(!cnt) return 1;
23 
24    int min_=10;
25    int x,y;
26 
27    for(int i=0;i<N;i++)
28       for(int j=0;j<N;j++)
29          if(str[i*9+j]=='.'){
30             int t=num[get(i,j)];
31             if(t < min_){
32                min_=t;
33                x=i;
34                y=j;
35             }
36          }
37 
38    for(int i = get(x,y); i; i-=lowbit(i)){
39       int t = p[lowbit(i)];
40       r[x]-=1<<t;
41       c[y]-=1<<t;
42       g[x/3][y/3]-=1<<t;
43       str[x*9+y]=t+'1';
44       if(dfs(cnt-1)) return 1;
45       r[x]+=1<<t;
46       c[y]+=1<<t;
47       g[x/3][y/3]+=1<<t;
48       str[x*9+y]='.';
49    }
50    return false;
51 }
52 inline void h(){
53    for(int i=0;i<N;i++)
54       p[1<<i]=i;
55    for(int i = 0;i< 1<<N;i++){
56       int cnt=0;
57       for(int j=i;j;j-=lowbit(j))
58          cnt++;
59       num[i]=cnt;
60    }
61 }
62 int main(){
63    h();
64    while(cin>>str,str[0]!='e'){
65       init();
66       int cnt=0;
67       for(int i=0,k=0;i<N;i++)
68          for(int j=0;j<N;j++,k++)
69             if(str[k]!='.'){
70                int t = str[k]-'1';
71                r[i] -= 1<<t;
72                c[j] -= 1<<t;
73                g[i/3][j/3] -= 1 << t;
74             }
75             else cnt++;
76       dfs(cnt);
77       cout<<str<<endl;
78    }
79    return 0;
80 }

 

Guess you like

Origin www.cnblogs.com/hhyx/p/12401918.html
9
3/9