Online Judge | Problem Set | Authors | Online Contests | User | ||||||
---|---|---|---|---|---|---|---|---|---|---|
Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest |
DLX 900MS过了#include<cstring> #include<cstdio> #include<map> using namespace std; struct DLX{ const static int maxn=7500; #define FF(i,A,s) for(int i = A[s];i != s;i = A[i]) int L[maxn],R[maxn],U[maxn],D[maxn]; int size,col[maxn],row[maxn],s[maxn],H[900]; bool vis[900]; int ans[maxn],cnt; void init(int m){ for(int i=0;i<=m;i++){ L[i]=i-1;R[i]=i+1;U[i]=D[i]=i;s[i]=0; } memset(H,-1,sizeof(H)); L[0]=m;R[m]=0;size=m+1; } void link(int r,int c){ U[size]=c;D[size]=D[c];U[D[c]]=size;D[c]=size; if(H[r]<0)H[r]=L[size]=R[size]=size; else { L[size]=H[r];R[size]=R[H[r]]; L[R[H[r]]]=size;R[H[r]]=size; } s[c]++;col[size]=c;row[size]=r;size++; } void del(int c){//精确覆盖 L[R[c]]=L[c];R[L[c]]=R[c]; FF(i,D,c)FF(j,R,i)U[D[j]]=U[j],D[U[j]]=D[j],--s[col[j]]; } void add(int c){ //精确覆盖 R[L[c]]=L[R[c]]=c; FF(i,U,c)FF(j,L,i)++s[col[U[D[j]]=D[U[j]]=j]]; } bool dfs(int k){//精确覆盖 if(!R[0]){ cnt=k;return 1; } int c=R[0];FF(i,R,0)if(s[c]>s[i])c=i; del(c); FF(i,D,c){ FF(j,R,i)del(col[j]); ans[k]=row[i];if(dfs(k+1))return true; FF(j,L,i)add(col[j]); } add(c); return 0; } }dlx; const int maxn=(int)1e3; char s[maxn]; int calg(int x,int y) { if(x<4) { if(y<4) return 1; else if(y<7) return 2; else return 3; } else if(x<7) { if(y<4) return 4; else if(y<7) return 5; else return 6; } else { if(y<4) return 7; else if(y<7) return 8; else return 9; } } void addedge(int line,int x,int y,int now) { dlx.link(line,(x-1)*9+y); dlx.link(line,81+(x-1)*9+now); dlx.link(line,162+(y-1)*9+now); dlx.link(line,243+(calg(x,y)-1)*9+now); } int a[100][100]; struct node { int x; int y; int now; }; int main() { while(scanf("%s",s)!=EOF) { if(s[0]=='e') { return 0; } dlx.init(81*4); int cnt=0; int line=1; map<int,node>mp; for(int i=1;i<=9;i++) { for(int j=1;j<=9;j++) { if(s[cnt]=='.') { for(int k=1;k<=9;k++) { addedge(line,i,j,k); mp[line]=node{i,j,k}; line++; } } else { int now=s[cnt]-'0'; addedge(line,i,j,now); mp[line]=node{i,j,now}; line++; } cnt++; } } dlx.dfs(0); for(int i=0;i<dlx.cnt;i++) { node use=mp[dlx.ans[i]]; a[use.x][use.y]=use.now; } for(int i=1;i<=9;i++) { for(int j=1;j<=9;j++) { printf("%d",a[i][j]); } } putchar('\n'); } return 0; } Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator