Topic URL: https: //www.luogu.com.cn/problem/P1074
The effect that the number of filling in a Sudoku 9 * 9, requiring the ranks of the palace are nine different from each other figures, given a certain score mechanism to require solving the maximum score. Roughly from the least idea of zero line to start the search (Imagine reality you do Sudoku must be this way), then as long as the information is stored 0 position, depth is the total value. Fill one by one in the order of priority search, in order to save computational resources, before searching information related to preprocessing 0 position.
1 #include<bits/stdc++.h> 2 using namespace std; 3 typedef unsigned int ui; 4 typedef long long ll; 5 typedef unsigned long long ull; 6 #define mem(a) memset(a,0,sizeof(a)) 7 #define prime1 1e9+7 8 #define prime2 1e9+9 9 #define scand(x) scanf("%llf",&x) 10 #define dbg(args) cout<<#args<<":"<<args<<endl; 11 #define pb(i) push_back(i) 12 #define ppb (X) pop_back (X) 13 is #define Scan (A) Scanf ( "% D", & A) 14 #define PF the printf 15 #define F (I, A, B) for (int I = A; I <= B; I ++) 16 int n-, the init, TOT, ANS = - . 1 , A [ 10 ] [ 10 ], Hang [ 10 ] [ 10 ], Lie [ 10 ] [ 10 ], Gong [ 10 ] [ 10 ] , S [ 100 ] [ . 5 ]; . 17 // S [] [0] line to save, s [] [1] save the column, s [] [2] storage house, s [] [3] stored score 18 is struct Node { 19 int hang,zero_cnt; 20 }p[10]; 21 bool cmp(node& a, node& b) 22 { 23 return a.zero_cnt<b.zero_cnt; 24 } 25 26 int score(int x,int y) 27 { 28 if(x==5&&y==5)return 10; 29 else if(x>=4&&x<=6&&y>=4&&y<=6)return 9; 30 else if(x>=3&&x<=7&&y>=3&&y<=7)return 8; 31 else if(x>=2&&x<=8&&y>=2&&y<=8)return 7; 32 else return 6; 33 } 34 int which(int x,int y) 35 { 36 if(x>=1&&x<=3&&y>=1&&y<=3)return 1; 37 else if(x>=1&&x<=3&&y>=4&&y<=6)return 2; 38 else if(x>=1&&x<=3&&y>=7&&y<=9)return 3; 39 else if(x>=4&&x<=6&&y>=1&&y<=3)return 4; 40 else if(x>=4&&x<=6&&y>=4&&y<=6)return 5; 41 else if(x>=4&&x<=6&&y>=7&&y<=9)return 6; 42 else if(x>=7&&x<=9&&y>=1&&y<=3)return 7; 43 else if(x>=7&&x<=9&&y>=4&&y<=. 6 ) return . 8 ; 44 is the else IF (X> = . 7 && X <= . 9 && Y> = . 7 && Y <= . 9 ) return . 9 ; 45 } 46 is void DFS ( int DEP, int SUM) // search depth and the total score of 47 { 48 IF (DEP == TOT) 49 { 50 // dbg (SUM); 51 is IF (SUM> ANS) ANS = SUM; 52 is return ; 53 is } 54 is F (I, . 1 , . 9 ) // figures 1-9 in a position to fill the depth dep 55 { 56 // ranks not conflict house 57 is IF (! Hang [S [dep] [ 0 ]] [I] && ! Lie [S [DEP] [ . 1 ]] [I] &&! Gong [S [DEP] [ 2 ]] [I]) 58 { 59 Hang [S [DEP] [ 0 ]] [I] = Lie [S [DEP] [ . 1 ]] [I] = Gong [S [DEP] [ 2 ]] [I] = . 1 ; 60 DFS (DEP + . 1 , SUM + I * S [DEP] [ . 3 ]); 61 is Hang [S [DEP] [ 0]][i]=lie[s[dep][1]][i]=gong[s[dep][2]][i]=0; 62 } 63 } 64 return; 65 } 66 int main() 67 { 68 //freopen("input.txt","r",stdin); 69 //freopen("output.txt","w",stdout); 70 std::ios::sync_with_stdio(false); 71 f(i,1,9) 72 { 73 p[i].hang=i; 74 f(j,1,9) 75 { 76 scan(a[i][j]); 77 if(a[i][j]) 78 { 79 init+=a[i][j]*score(i,j); 80 hang[i][a[i][j]]=1; 81 lie[j][a[i][j]]=1; 82 gong[which(i,j)][a[i][j]]=1; 83 } 84 else 85 { 86 p[i].zero_cnt++; 87 } 88 } 89 } 90 91 is Sort (P + . 1 , P + 10 , CMP); 92 93 F (I, . 1 , . 9 ) 94 { 95 F (J, . 1 , . 9 ) 96 { 97 IF (A [P [I] .hang] [J] == 0 ) 98 { 99 S [TOT] [ 0 ] = P [I] .hang; // save the zero position for each row, column, and score house, or will be repeated in dfs computing 100 S [TOT] [ . 1 ] = J; 101 S [TOT] [ 2 ] = Which (P [I] .hang, J); 102 S [TOT ++] [ . 3 ] = Score (P [I] .hang, J ); 103 } 104 } 105 } 106 // dbg (the init); 107 // dbg (TOT); 108 DFS ( 0 , the init); // start the search from the position of 0 zero, the initial score of the init 109 PF ( " % D " , ANS); 110 return 0; 111 }