111 lines
2.3 KiB
C++
111 lines
2.3 KiB
C++
#include<cstdio>
|
|
#include<cstring>
|
|
#include<vector>
|
|
using namespace std;
|
|
const int maxn=81*4+5;
|
|
const int maxnode=9*9*9*4*4+5;
|
|
const int Slot=0,Row=1,Col=2,Sub=3;
|
|
const int p[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}
|
|
};
|
|
inline int encode(int a,int b,int c) { return a*81+b*9+c+1; }
|
|
inline void decode(int code,int &a,int &b,int &c)
|
|
{
|
|
code--;
|
|
c=code%9; code/=9;
|
|
b=code%9; code/=9;
|
|
a=code;
|
|
}
|
|
int res=-1,mp[9][9];
|
|
struct DLX
|
|
{
|
|
int n,sz;
|
|
int s[maxn];
|
|
int row[maxnode],col[maxnode];
|
|
int L[maxnode],R[maxnode],U[maxnode],D[maxnode];
|
|
void init(int n)
|
|
{
|
|
this->n=n;
|
|
sz=n+1;
|
|
memset(s,0,sizeof(s));
|
|
for(int i=0;i<=n;i++) { U[i]=i; D[i]=i; L[i]=i-1; R[i]=i+1; }
|
|
R[n]=0; L[0]=n;
|
|
}
|
|
void addNodes(int r,const vector<int> &columns)
|
|
{
|
|
int first=sz,c_sz=columns.size();
|
|
for(int i=0;i<c_sz;i++)
|
|
{
|
|
int c=columns[i];
|
|
L[sz]=sz-1; R[sz]=sz+1; D[sz]=c; U[sz]=U[c];
|
|
D[U[c]]=sz; U[c]=sz;
|
|
row[sz]=r; col[sz]=c;
|
|
s[c]++;sz++;
|
|
}
|
|
R[sz-1]=first; L[first]=sz-1;
|
|
}
|
|
#define For(i,A,s) for(int i=A[s];i!=s;i=A[i])
|
|
void remove(int c)
|
|
{
|
|
L[R[c]]=L[c];
|
|
R[L[c]]=R[c];
|
|
For(i,D,c) For(j,R,i) { U[D[j]]=U[j]; D[U[j]]=D[j]; --s[col[j]]; }
|
|
}
|
|
void restore(int c)
|
|
{
|
|
For(i,U,c) For(j,L,i) { ++s[col[j]]; U[D[j]]=j; D[U[j]]=j; }
|
|
L[R[c]]=c;
|
|
R[L[c]]=c;
|
|
}
|
|
bool dfs(int val)
|
|
{
|
|
if(R[0]==0) { res=max(res,val); return true; }
|
|
int c=R[0],flag=false;
|
|
For(i,R,0) if(s[i]<s[c]) c=i;
|
|
remove(c);
|
|
For(i,D,c)
|
|
{
|
|
For(j,R,i) remove(col[j]);
|
|
int nr,nc,nv;
|
|
decode(row[i],nr,nc,nv);
|
|
if(dfs(val+p[nr][nc]*(nv+1))) flag=true;
|
|
For(j,L,i) restore(col[j]);
|
|
}
|
|
restore(c);
|
|
return flag;
|
|
}
|
|
#undef For
|
|
};
|
|
DLX solver;
|
|
#define For(i,a,b) for(int i=(a);i<=(b);i++)
|
|
int main()
|
|
{
|
|
#ifdef local
|
|
freopen("pro.in","r",stdin);
|
|
#endif
|
|
For(r,0,8) For(c,0,8) scanf("%d",&mp[r][c]);
|
|
solver.init(9*9*4);
|
|
For(r,0,8) For(c,0,8) For(v,0,8)
|
|
if(mp[r][c]==0||mp[r][c]==v+1)
|
|
{
|
|
vector<int> cols;
|
|
cols.push_back(encode(Slot,r,c));
|
|
cols.push_back(encode(Row,r,v));
|
|
cols.push_back(encode(Col,c,v));
|
|
cols.push_back(encode(Sub,(r/3)*3+c/3,v));
|
|
solver.addNodes(encode(r,c,v),cols);
|
|
}
|
|
solver.dfs(0);
|
|
printf("%d\n",res);
|
|
return 0;
|
|
}
|