P1074 靶形数独
按点排序太他喵玄学了,T两个点过不去,然后换行换列都不行,无奈按行排序,正常的搜索题
#include<bits/stdc++.h> using namespace std; int row[10][10],a,ln[10][10],num[10][10],ck[10][10],ans=-1,pt=0,tot,cg[10][10]; int vt[10][10]= {{0,0,0,0,0,0,0,0,0,0}, {0,6,6,6,6,6,6,6,6,6}, {0,6,7,7,7,7,7,7,7,6}, {0,6,7,8,8,8,8,8,7,6}, {0,6,7,8,9,9,9,8,7,6}, {0,6,7,8,9,10,9,8,7,6}, {0,6,7,8,9,9,9,8,7,6}, {0,6,7,8,8,8,8,8,7,6}, {0,6,7,7,7,7,7,7,7,6}, {0,6,6,6,6,6,6,6,6,6}}; struct node{int i,j;}ab[1000]; struct nd{int tr,val;}rt[10]; int cmp(nd x,nd y){return x.val>y.val;} void dfs(int pos) { int i=ab[pos].i,j=ab[pos].j; int tk=cg[i][j]; for(int k=1;k<=9;k++) if(!row[i][k]&&!ln[j][k]&&!ck[tk][k]) { row[i][k]=ln[j][k]=ck[tk][k]=1; pt+=vt[i][j]*k; if(pos==tot) ans=max(ans,pt); else dfs(pos+1); row[i][k]=ln[j][k]=ck[tk][k]=0; pt-=vt[i][j]*k; } } int main() { for(int i=1;i<=9;i++) for(int j=1;j<=9;j++) { cg[i][j]=(i-1)/3*3+(j-1)/3+1; cin>>a; if(a) { row[i][a]=1; rt[i].val++; ln[j][a]=1; ck[cg[i][j]][a]=1; pt+=vt[i][j]*a; num[i][j]=a; } } for(int i=1;i<=9;i++)rt[i].tr=i; sort(rt+1,rt+10,cmp); for(int i=1;i<=9;i++) { int ro=rt[i].tr; for(int j=1;j<=9;j++) if(!num[ro][j])ab[++tot].i=ro,ab[tot].j=j; } dfs(1); cout<<ans; }