说是爆搜,却还是有点技巧
首先数独应该有个想法,从最有可能填上数字的那些地方开始
也就是说哪些行0最少,就从那儿开始
为了最大限度地利用已有信息,就先把所有0的位置扣出来单独填
最后别忘了回溯就行
但是这种做法仍然过不了下面这个样例:
0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0
还是应该学学DLX,此外用拼音做数组名实在太爽了
代码:
#include <bits/stdc++.h> #define int long long #define sc(a) scanf("%lld",&a) #define scc(a,b) scanf("%lld %lld",&a,&b) #define sccc(a,b,c) scanf("%lld %lld %lld",&a,&b,&c) #define scs(a) scanf("%s",a) #define schar(a) scanf("%c",&a) #define pr(a) printf("%lld",a) #define fo(i,a,b) for(int i=a;i<b;++i) #define re(i,a,b) for(int i=a;i<=b;++i) #define rfo(i,a,b) for(int i=a;i>b;--i) #define rre(i,a,b) for(int i=a;i>=b;--i) #define prn() printf("\n") #define prs() printf(" ") #define mkp make_pair #define pii pair<int,int> #define pub(a) push_back(a) #define pob() pop_back() #define puf(a) push_front(a) #define pof() pop_front() #define fst first #define snd second #define frt front() #define bak back() #define mem0(a) memset(a,0,sizeof(a)) #define memmx(a) memset(a,0x3f3f,sizeof(a)) #define memmn(a) memset(a,-0x3f3f,sizeof(a)) #define debug #define db double #define yyes cout<<"YES"<<endl; #define nno cout<<"NO"<<endl; using namespace std; typedef vector<int> vei; typedef vector<pii> vep; typedef map<int,int> mpii; typedef map<char,int> mpci; typedef map<string,int> mpsi; typedef deque<int> deqi; typedef deque<char> deqc; typedef priority_queue<int> mxpq; typedef priority_queue<int,vector<int>,greater<int> > mnpq; typedef priority_queue<pii> mxpqii; typedef priority_queue<pii,vector<pii>,greater<pii> > mnpqii; const int maxn=500005; const int inf=0x3f3f3f3f3f3f3f3f; const int MOD=100000007; const db eps=1e-10; int qpow(int a,int b){int tmp=a%MOD,ans=1;while(b){if(b&1){ans*=tmp,ans%=MOD;}tmp*=tmp,tmp%=MOD,b>>=1;}return ans;} int lowbit(int x){return x&-x;} int max(int a,int b){return a>b?a:b;} int min(int a,int b){return a<b?a:b;} int mmax(int a,int b,int c){return max(a,max(b,c));} int mmin(int a,int b,int c){return min(a,min(b,c));} void mod(int &a){a+=MOD;a%=MOD;} bool chk(int now){} int half(int l,int r){while(l<=r){int m=(l+r)/2;if(chk(m))r=m-1;else l=m+1;}return l;} int ll(int p){return p<<1;} int rr(int p){return p<<1|1;} int mm(int l,int r){return (l+r)/2;} int lg(int x){if(x==0) return 1;return (int)log2(x)+1;} bool smleql(db a,db b){if(a<b||fabs(a-b)<=eps)return true;return false;} bool bigeql(db a,db b){if(a>b||fabs(a-b)<=eps)return true;return false;} bool eql(db a,db b){if(fabs(a-b)<eps) return 1;return 0;} db len(db a,db b,db c,db d){return sqrt((a-c)*(a-c)+(b-d)*(b-d));} bool isp(int x){if(x==1)return false;if(x==2)return true;for(int i=2;i*i<=x;++i)if(x%i==0)return false;return true;} inline int read(){ char ch=getchar();int s=0,w=1; while(ch<48||ch>57){if(ch=='-')w=-1;ch=getchar();} while(ch>=48&&ch<=57){s=(s<<1)+(s<<3)+ch-48;ch=getchar();} return s*w; } inline void write(int x){ if(x<0)putchar('-'),x=-x; if(x>9)write(x/10); putchar(x%10+48); } int gcd(int a, int b){ if(a==0) return b; if(b==0) return a; if(!(a&1)&&!(b&1)) return gcd(a>>1,b>>1)<<1; else if(!(b&1)) return gcd(a,b>>1); else if(!(a&1)) return gcd(a>>1,b); else return gcd(abs(a-b),min(a,b)); } int lcm(int x,int y){return x*y/gcd(x,y);} int getidx(int i,int j){ if(i<=3) { if(j<=3) return 1; else if(j<=6) return 2; else return 3; } else if(i<=6) { if(j<=3) return 4; else if(j<=6) return 5; else return 6; } else { if(j<=3) return 7; else if(j<=6) return 8; else return 9; } } int getval(int i,int j) { if(i==1||j==1||i==9||j==9) return 6; if(i==2||j==2||i==8||j==8) return 7; if(i==3||j==3||i==7||j==7) return 8; if(i==4||j==4||i==6||j==6) return 9; return 10; } int mp[10][10]; bool hang[10][10],lie[10][10],gong[10][10]; int ans=-1,mem[100][4]; int t=0; struct node{ int v,id; bool operator < (const node& o)const{return v<o.v;} }row[10]; void dfs(int p,int sum){ // cout<<p<<endl; if(p==t){ ans=max(ans,sum); return; } re(i,1,9){ if(!hang[mem[p][0]][i]&&!lie[mem[p][1]][i]&&!gong[mem[p][3]][i]){ hang[mem[p][0]][i]=lie[mem[p][1]][i]=gong[mem[p][3]][i]=1; dfs(p+1,sum+mem[p][2]*i); hang[mem[p][0]][i]=lie[mem[p][1]][i]=gong[mem[p][3]][i]=0; } } } signed main(){ ios_base::sync_with_stdio(0); cin.tie(0),cout.tie(0); re(i,1,9) row[i].id=i; int now=0; re(i,1,9){ re(j,1,9){ cin>>mp[i][j]; if(mp[i][j]==0) row[i].v++; else{ int k=mp[i][j]; hang[i][k]=lie[j][k]=gong[getidx(i,j)][k]=1; now+=k*getval(i,j); } } } sort(row+1,row+10); re(i,1,9){ re(j,1,9){ if(mp[row[i].id][j]==0){ mem[t][0]=row[i].id; mem[t][1]=j; mem[t][2]=getval(row[i].id,j); mem[t][3]=getidx(row[i].id,j); t++; } } } dfs(0,now); cout<<ans<<endl; return 0; } /* 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 */