hdu 4069 福州赛区网络赛I DLC ***

再遇到一个DLC就刷个专题

 #include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <string>
#include <math.h>
#include <stdlib.h>
#include <time.h>
using namespace std;
const int N = ; //3*3数独
const int MaxN = N*N*N + ;
const int MaxM = N*N* + ;
const int maxnode = MaxN* + MaxM + ;
char g[MaxN];
int cnt;
struct DLX
{
int n,m,size;
int U[maxnode],D[maxnode],R[maxnode],L[maxnode],Row[maxnode],Col[maxnode];
int H[MaxN],S[MaxM];
int ansd,ans[MaxN];
void init(int _n,int _m)
{
n = _n;
m = _m;
for(int i = ;i <= m;i++)
{
S[i] = ;
U[i] = D[i] = i;
L[i] = i-;
R[i] = i+;
}
R[m] = ; L[] = m;
size = m;
for(int i = ;i <= n;i++)H[i] = -;
}
void Link(int r,int c)
{
++S[Col[++size]=c];
Row[size] = r;
D[size] = D[c];
U[D[c]] = size;
U[size] = c;
D[c] = size;
if(H[r] < )H[r] = L[size] = R[size] = size;
else
{
R[size] = R[H[r]];
L[R[H[r]]] = size;
L[size] = H[r];
R[H[r]] = size;
}
}
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])
++S[Col[U[D[j]]=D[U[j]]=j]];
L[R[c]] = R[L[c]] = c;
}
void Dance(int d)
{
if(cnt > )return;
if(R[] == )
{
for(int i = ;i < d;i++)g[(ans[i]-)/] = (ans[i]-)% + '';
cnt++;
return;
}
int c = R[];
for(int i = R[];i != ;i = R[i])
if(S[i] < S[c])
c = i;
remove(c);
for(int i = D[c];i != c;i = D[i])
{
ans[d] = Row[i];
for(int j = R[i];j != i;j = R[j])remove(Col[j]);
Dance(d+);
if(cnt > )return;
for(int j = L[i];j != i;j = L[j])resume(Col[j]);
}
resume(c);
}
}; int id[][];
int a[][];
void bfs(int sx,int sy,int d)
{
queue<pair<int,int> >q;
q.push(make_pair(sx,sy));
id[sx][sy] = d;
while(!q.empty())
{
pair<int,int> tmp = q.front();
int x = tmp.first;
int y = tmp.second;
q.pop();
if(x > && ((a[x][y]%)/) == )
if(id[x-][y] == -)
{
id[x-][y] = d;
q.push(make_pair(x-,y));
}
if(x < N- && ((a[x][y]%)/) == )
if(id[x+][y] == -)
{
id[x+][y] = d;
q.push(make_pair(x+,y));
}
if(y > && ((a[x][y])/) == )
if(id[x][y-] == -)
{
id[x][y-] = d;
q.push(make_pair(x,y-));
}
if(y < N- && ((a[x][y]%)/) == )
if(id[x][y+] == -)
{
id[x][y+] = d;
q.push(make_pair(x,y+));
}
}
}
DLX dlx; int main()
{
//freopen("in.txt","r",stdin);
//freopen("out.txt","w",stdout);
int T;
scanf("%d",&T);
int iCase = ;
while(T--)
{
iCase++;
for(int i = ;i < N;i++)
for(int j = ;j < N;j++)
scanf("%d",&a[i][j]);
memset(id,-,sizeof(id));
int index = ;
for(int i = ;i < N;i++)
for(int j = ;j < N;j++)
if(id[i][j] == -)
bfs(i,j,++index);
dlx.init(N*N*N,N*N*);
for(int i = ;i < N;i++)
for(int j = ;j < N;j++)
for(int k = ;k <= N;k++)
{
if(a[i][j]% != && a[i][j]% != k)continue;
int r = (i*N+j)*N + k;
int c1 = i*N+j+;
int c2 = N*N+i*N+k;
int c3 = N*N*+j*N+k;
int c4 = N*N*+(id[i][j]-)*N+k;
dlx.Link(r,c1);
dlx.Link(r,c2);
dlx.Link(r,c3);
dlx.Link(r,c4);
}
cnt = ;
dlx.Dance();
printf("Case %d:\n",iCase);
if(cnt == )printf("No solution\n");
else if(cnt > )printf("Multiple Solutions\n");
else
{
for(int i = ;i < N*N;i++)
{
printf("%c",g[i]);
if(i % N == N - )
printf("\n");
}
}
}
return ;
}
上一篇:Photoshop为外景人物图片增加柔和的蓝黄阳光色


下一篇:Photoshop将草原上的人物调制出清爽的韩系蓝黄色