题解(小一点数组)
2024-04-13 15:25:40
发布于:江苏
#include <bits/stdc++.h>
using namespace std;
const int W[9][9]={
{6,6,6,6,6,6,6,6,6},
{6,7,7,7,7,7,7,7,6},
{6,7,8,8,8,8,8,7,6},
{6,7,8,9,9,9,8,7,6},
{6,7,8,9,10,9,8,7,6},
{6,7,8,9,9,9,8,7,6},
{6,7,8,8,8,8,8,7,6},
{6,7,7,7,7,7,7,7,6},
{6,6,6,6,6,6,6,6,6}
};
int a[10][10],I[10][10],J[10][10],B[10][10],N[10],R[10],A;
void dfs(int c,int S,int L){
if(c==81){A=max(A,S);return;}
if(S+9+L9<=A)return;
int i=R[c/9],j=c%9,b=i/33+j/3;
if(a[i][j])dfs(c****+a[i][j]W[i][j],L-a[i][j]);
else for(int k=1;k<=9;k++)if(!I[i][k]&&!J[j][k]&&!B[b][k])I[i][k]=J[j][k]=B[b][k]=1,dfs(c***+kW[i][j],L-k),I[i][k]=J[j][k]=B[b][k]=0;
}
main(){
A=-1;
for(int i=0;i<9;i++){
R[i]=i;
for(int j=0;j<9;j++){
cin>>a[i][j];
if(a[i][j])I[i][a[i][j]]=J[j][a[i][j]]=B[i/33+j/3][a[i][j]]=1;
else N[i]++;
}
}sort(R,R+9,[](int a,int b){return N[a]<N[b];}),dfs(0,0,405);
cout<<A;
return 0;
}
这里空空如也
有帮助,赞一个