1#include <cstdio>2#include <cstring>3 using namespacestd;4 BOOLv[Ten][Ten],row[Ten][Ten],col[Ten][Ten],used[4][4][Ten];5 inta[Ten][Ten];6 BOOLFlag;7 voidDfsintXinty)8 {9 if(flag)return ;Ten if(x>9) One { A for(intI=1; i<=9; i++) - { - for(intj=1; j<=9; j + +) printf ("%d", A[i][j]); theprintf"\ n"); - } -flag=true; - return; + } - + if(v[x][y]==1) A { at if(y==9) DFS (x+1,1); - ElseDFS (x,y+1); - } - Else - { -v[x][y]=1; in for(intI=1; i<=9; i++) - if(row[x][i]==0&& col[y][i]==0&& used[(x1)/3+1[Y-1)/3+1][i]==0) to { +a[x][y]=i; -row[x][i]=1; thecol[y][i]=1; *used[(x1)/3+1[Y-1)/3+1][i]=1; $ if(y==9) DFS (x+1,1);ElseDFS (x,y+1);Panax Notoginsenga[x][y]=0; -row[x][i]=0; thecol[y][i]=0; +used[(x1)/3+1[Y-1)/3+1][i]=0; A } thev[x][y]=0; + } - } $ $ intMain () - { - intN; thescanf"%d",&n); - for(intt=1; t<=n;t++)Wuyi { theMemset (A,0,sizeof(a)); -memset (V,0,sizeof(v)); Wumemset (Row,0,sizeof(Row)); -memset (Col,0,sizeof(col)); Aboutmemset (Used,0,sizeof(used)); $ for(intI=1; i<=9; i++) - { - Charstr[Ten]; -scanf"%s", str); A for(intj=0;j<9; j + +) + { thea[i][j+1]=str[j]-'0'; - if(a[i][j+1]!=0) $ { thev[i][j+1]=1; the intx=a[i][j+1]; therow[i][x]=1; thecol[j+1][x]=1; -used[(I-1)/3+1[(j+1-1)/3+1][x]=1; in } the } the } About /* the for (int i=1;i<=9;i++) the { the for (int j=1;j<=9;j++) printf ("%d", row[i][j]); + printf ("\ n"); - } the */Bayiflag=false; theDfs1,1); the } - return 0; -}
Dfs/poj 2676 Sudoku