目测结构体跑的比驻足慢一点,但又有时候结构体快,看个人喜好吧。
结构体是我一开始写的。
里面同时包含了多重覆盖和精确覆盖两种。
结构体版本:
struct P {
int right,left,down,up;
int size;
int x,y;
};
struct DLX {
const static int maxn=10000; // 节点总数
P p[maxn];
int size; // 节点标记
int head[100]; // 行标指针
int vis[100];
stack<int> Ans;
int ans;
void init(int m) {
for (int i=0;i<=m;i++) {
p[i].up=p[i].down=i;
p[i].left=i-1;p[i].right=i+1;
p[i].size=0;
}
p[0].left=m;p[m].right=0;
memset(head,-1,sizeof(head));
size=m+1;
ans=-1;
while (Ans.size()) Ans.pop();
}
void link(int row,int col) {
p[size].x=row;p[size].y=col;p[col].size++;
p[size].up=p[col].up;p[size].down=col;
p[p[col].up].down=size;p[col].up=size;
if (head[row]<0) head[row]=p[size].left=p[size].right=size;
else {
p[size].right=head[row];p[size].left=p[head[row]].left;
p[p[size].left].right=size;p[p[size].right].left=size;
}
size++;
}
// exact cover
void remove(int c) {
p[p[c].right].left=p[c].left;
p[p[c].left].right=p[c].right;
for (int i=p[c].down;i!=c;i=p[i].down) {
for (int j=p[i].right;j!=i;j=p[j].right) {
p[p[j].down].up=p[j].up;
p[p[j].up].down=p[j].down;
p[p[j].y].size--;
}
}
}
void resume(int c) {
p[p[c].left].right=c;
p[p[c].right].left=c;
for (int i=p[c].up;i!=c;i=p[i].up) {
for (int j=p[i].left;j!=i;j=p[j].left) {
p[p[j].down].up=j;
p[p[j].up].down=j;
p[p[j].y].size++;
}
}
}
bool dfs(int cnt) {
if (ans!=-1) return true;
if (p[0].right==0) {
ans=cnt;
return true;
}
int c=0;
int Min=INF_INT;
for (int i=p[0].right;i!=0;i=p[i].right)
if (Min>p[i].size) Min=p[i].size,c=i;
remove(c);
for (int i=p[c].down;i!=c;i=p[i].down) {
Ans.push(p[i].x);
for (int j=p[i].right;j!=i;j=p[j].right) {
remove(p[j].y);
}
if (dfs(cnt+1)) return true;
for (int j=p[i].left;j!=i;j=p[j].left) {
resume(p[j].y);
}
Ans.pop();
}
resume(c);
return false;
}
// exact cover ends;
// multiple cover
void del(int c) {
for (int i=p[c].down;i!=c;i=p[i].down) {
p[p[i].right].left=p[i].left;
p[p[i].left].right=p[i].right;
}
}
void rec(int c) {
for (int i=p[c].up;i!=c;i=p[i].up) {
p[p[i].right].left=i;
p[p[i].left].right=i;
}
}
int h() {
int ret=0;
memset(vis,0,sizeof(vis));
for (int i=p[0].right;i!=0;i=p[i].right) if (!vis[i]) {
ret++;vis[i]=1;
for (int j=p[i].down;j!=i;j=p[j].down)
for (int k=p[j].right;k!=j;k=p[k].right)
vis[p[k].y]=1;
}
return ret;
}
void DFS(int cnt) {
if (p[0].right==0) ans=min(ans,cnt);
else if (h()+cntint c=0;
int Min=INF_INT;
for (int i=p[0].right;i!=0;i=p[i].right){
if (p[i].sizefor (int i=p[c].down;i!=c;i=p[i].down) {
del(i);
for (int j=p[i].right;j!=i;j=p[j].right) del(j);
DFS(cnt+1);
for (int j=p[i].left;j!=i;j=p[j].left) rec(j);
rec(i);
}
}
return;
}
// multiple cover ends;
}dlx;
数组版本:
const int Maxm = 15*15+10;
const int Maxn = 15*15+10;
const int maxnode = MaxN * MaxM;
struct DLX {
int n,m;
int L[maxnode],R[maxnode],U[maxnode],D[maxmaxnode];
int Row[maxnode],Col[maxnode];
int H[maxnode],S[maxnode];
int vis[maxn];
int ans; // need update
int size;
void init(int _n,int _m) {
n=_n,m=_m;
for (int i=0;i<=m;i++) {
U[i]=D[i]=i;
L[i]=i-1;R[i]=i+1;
S[i]=0;
}
L[0]=m;R[m]=0;
for (int i=1;i<=n;i++) H[i]=-1;
size=m;
}
void link(int row,int col) {
size++;
Col[size]=col;Row[size]=row;
S[col]++;
U[size]=U[col];D[size]=col;
D[U[col]]=size;
U[col]=size;
if (H[row]!=-1) {
R[size]=H[row];
L[size]=L[H[row]];
R[L[size]]=size;
L[R[size]]=size;
}
else
H[row]=L[size]=R[size]=size;
}
// exact cover
void remove(int c) {
L[R[c]]=L[c];
R[L[c]]=R[c];
for (int i=D[c];i!=c;i=D[i]) {
for (int j=R[i];j!=i;j=R[j]) {
U[D[j]]=U[j];
D[U[j]]=D[j];
S[Col[j]]--;
}
}
}
void resume(int c) {
for (int i=U[c];i!=c;i=U[i]) {
for (int j=L[i];j!=i;j=L[j]) {
U[D[j]]=j;
D[U[j]]=j;
S[Col[j]]++;
}
}
R[L[c]]=c;
L[R[c]]=c;
}
bool dfs(int cnt) {
if (ans!=-1) return true;
if (R[0]==0) {
ans=cnt;
return true;
}
int c=R[0];
for (int i=R[0];i!=0;i=R[i]){
if (S[c]>S[i]) c=i;
}
remove(c);
for (int i=D[c];i!=c;i=D[i]) {
for (int j=R[i];j!=i;j=R[j]) {
remove(Col[j]);
}
if (dfs(cnt+1)) return true;
for (int j=L[i];j!=i;j=L[j]) {
resume(Col[j]);
}
}
resume(c);
return false;
}
// exact cover ends;
// multiple cover
void del(int c) {
for (int i=D[c];i!=c;i=D[i]) {
L[R[i]]=L[i];
R[L[i]]=R[i];
}
}
void rec(int c) {
for (int i=U[c];i!=c;i=U[i]) {
L[R[i]]=R[L[i]]=i;
}
}
int h() {
int ret=0;
for (int i=0;i<=m;i++) vis[i]=0;
for (int i=R[0];i!=0;i=R[i]) if (!vis[i]) {
ret++;
vis[i]=1;
for (int j=D[i];j!=i;j=D[j])
for (int k=R[j];k!=j;k=R[k])
vis[Col[k]]=1;
}
return ret;
}
void DFS(int cnt) {
if (cnt+h()>=ans) return;
if (R[0]==0) {
ans=min(ans,cnt);
return;
}
int c=R[0];
for (int i=R[0];i!=0;i=R[i])
if (S[c]>S[i]) c=i;
for (int i=D[c];i!=c;i=D[i]) {
del(i);
for (int j=R[i];j!=i;j=R[j]) del(j);
DFS(cnt+1);
for (int j=L[i];j!=i;j=L[j]) rec(j);
rec(i);
}
return;
}
// multiple cover ends;
}dlx;