Description
Sudoku is a very simple task. A square table with 9 rows and 9 columns is divided to 9 smaller squares 3x3 as shown on the Figure. In some of the cells are written decimal digits from 1 to 9. The other cells are empty. The goal is to fill the empty cells with decimal digits from 1 to 9, one digit per cell, in such way that in each row, in each column and in each marked 3x3 subsquare, all the digits from 1 to 9 to appear. Write a program to solve a given Sudoku-task.
Input
The input data will start with the number of the test cases. For each test case, 9 lines follow, corresponding to the rows of the table. On each line a string of exactly 9 decimal digits is given, corresponding to the cells in this line. If a cell is empty it is represented by 0.
Output
For each test case your program should print the solution in the same format as the input data. The empty cells have to be filled according to the rules. If solutions is not unique, then the program may print any one of them.
Sample Input
1 103000509 002109400 000704000 300502006 060000050 700803004 000401000 009205800 804000107
Sample Output
143628579 572139468 986754231 391542786 468917352 725863914 237481695 619275843854396127
dfs+剪枝
#include<cstdio> #include<iostream> #include<cstring> #include<map> #include<queue> #include<stack> #include<algorithm> #include<vector> #include<cmath> #include<string> using namespace std; const int maxn=10; int T,a[maxn][maxn],tot; int c[maxn][maxn],r[maxn][maxn],u[maxn][maxn]; struct point { int x,y; point(int x=0,int y=0):x(x),y(y){} }p[maxn*maxn]; string s; bool flag; int check(int x,int y) { x=(x-1)/3; y=(y-1)/3; return 3*x+y+1; } void dfs(int now) { if (flag) return ; if (now==tot) { flag=true; return;} int x=p[now].x,y=p[now].y; int z=check(p[now].x,p[now].y); for (int i=1;i<=9;i++) if (c[x][i]+r[y][i]+u[z][i]<1) { c[x][i]=r[y][i]=u[z][i]=1; a[x][y]=i; dfs(now+1); if (flag) return ; c[x][i]=r[y][i]=u[z][i]=0; } } int main() { cin>>T; while (T--) { memset(c,0,sizeof(c)); memset(r,0,sizeof(r)); memset(u,0,sizeof(u)); tot=0; for (int i=1;i<=9;i++) { cin>>s; for (int j=1;j<=s.size();j++) { a[i][j]=s[j-1]-'0'; if (a[i][j]) c[i][a[i][j]]=r[j][a[i][j]]=u[check(i,j)][a[i][j]]=1; else p[tot++]=point(i,j); } } flag=false; dfs(0); for (int i=1;i<=9;i++) { for (int j=1;j<=9;j++) printf("%d",a[i][j]); printf("\n"); } } return 0; }
建图+dlx
#include<cstdio> #include<cstring> #include<algorithm> using namespace std; typedef long long ll; const ll maxn=300005; int n,m,x,y,T,a[20][20],tot,p[1000][3]; char s[20][20]; inline void read(int &ret) { char c; do { c = getchar(); } while(c < '0' || c > '9'); ret = c - '0'; while((c=getchar()) >= '0' && c <= '9') ret = ret * 10 + ( c - '0' ); } struct DLX { int L[maxn],R[maxn],U[maxn],D[maxn]; int row[maxn],col[maxn],ans[maxn],cnt[maxn]; int n,m,num,sz; void add(int now,int l,int r,int u,int d,int x,int y) { L[now]=l; R[now]=r; U[now]=u; D[now]=d; row[now]=x; col[now]=y; } void reset(int n,int m) { this->n=n; this->m=m; for (int i=0;i<=m;i++) { add(i,i-1,i+1,i,i,0,i); cnt[i]=0; } L[0]=m; R[m]=0; sz=m+1; } void insert(int x,int y) { int ft=sz-1; if (row[ft]!=x) { add(sz,sz,sz,U[y],y,x,y); U[D[sz]]=sz; D[U[sz]]=sz; } else { add(sz,ft,R[ft],U[y],y,x,y); R[L[sz]]=sz; L[R[sz]]=sz; U[D[sz]]=sz; D[U[sz]]=sz; } ++cnt[y]; ++sz; } void remove(int now) { R[L[now]]=R[now]; L[R[now]]=L[now]; for (int i=D[now];i!=now;i=D[i]) for (int j=R[i];j!=i;j=R[j]) { D[U[j]]=D[j]; U[D[j]]=U[j]; --cnt[col[j]]; } } void resume(int now) { R[L[now]]=now; L[R[now]]=now; for (int i=D[now];i!=now;i=D[i]) for (int j=R[i];j!=i;j=R[j]) { D[U[j]]=j; U[D[j]]=j; ++cnt[col[j]]; } } bool dfs(int x) { if (!R[0]) {num=x; return true;} int now=R[0]; for (int i=now;i!=0;i=R[i]) if (cnt[now]>cnt[i]) now=i; remove(now); for (int i=D[now];i!=now;i=D[i]) { ans[x]=row[i]; for (int j=R[i];j!=i;j=R[j]) remove(col[j]); if (dfs(x+1)) return true; for (int j=L[i];j!=i;j=L[j]) resume(col[j]); } resume(now); return false; } void display() { for (int i=0;i<num;++i) { a[p[ans[i]][0]][p[ans[i]][1]]=p[ans[i]][2]; } for (int i=1;i<=9;i++) for (int j=1;j<=9;j++) printf("%d%s",a[i][j],j==9?"\n":""); } }dlx; void insert(int x,int y,int z) { p[++tot][0]=x; p[tot][1]=y; p[tot][2]=z; dlx.insert(tot,9*(x-1)+y); dlx.insert(tot,81+9*(x-1)+z); dlx.insert(tot,162+9*(y-1)+z); x=(x-1)/3*3+(y-1)/3+1; dlx.insert(tot,243+9*(x-1)+z); } int main() { read(T); while (T--) { tot=0; dlx.reset(729,324); for (int i=1;i<=9;++i) { scanf("%s",s[i]+1); for (int j=1;j<=9;++j) { a[i][j]=s[i][j]-'0'; if (a[i][j]) insert(i,j,a[i][j]); else for (int k=1;k<=9;++k) insert(i,j,k); } } if (dlx.dfs(0)) dlx.display(); } return 0; }