您的位置:首页 > 其它

POJ 2676 Sudoku 解题报告(Dancing Link)

2014-04-29 15:34 429 查看
Sudoku

Time Limit: 2000MS Memory Limit: 65536K
Total Submissions: 13019 Accepted: 6452 Special Judge
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
619275843
854396127


Source

Southeastern Europe 2005
   

    解题报告: 和POJ 3074是一样的,稍微改下代码即可。代码如下:

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;

const int M=1024*110;
const int N=1024;

int l[M], r[M], d[M], u[M], col[M], row[M], h
, control
;
int dcnt = 0;
char str[111];

inline void addnode(int &x)
{
++x;
r[x]=l[x]=u[x]=d[x]=x;
}

inline void insert_row(int rowx, int x)
{
r[l[rowx]]=x;
l[x]=l[rowx];
r[x]=rowx;
l[rowx]=x;
}

inline void insert_col(int colx, int x)
{
d[u[colx]]=x;
u[x]=u[colx];
d[x]=colx;
u[colx]=x;
}

void dlx_init(int cols)
{
memset(h, -1, sizeof(h));
memset(control, 0, sizeof(control));
dcnt=-1;
addnode(dcnt);

for(int i=1;i<=cols;++i)
{
addnode(dcnt);
insert_row(0, dcnt);
}
}

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];
control[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;
control[col[j]]++;
}
l[r[c]]=c;
r[l[c]]=c;
}

bool DLX(int deep)
{
if(r[0]==0)
{
for(int i=0;i<9;i++)
{
for(int j=0;j<9;j++)
printf("%c", str[i*9+j]);
puts("");
}
return true;
}

int min=M, tempc;
for(int i=r[0];i!=0;i=r[i]) if(control[i]<min)
{
min=control[i];
tempc=i;
}
remove(tempc);
for(int i=d[tempc];i!=tempc;i=d[i])
{
str[row[i]/9]=row[i]%9+'1';
for(int j=r[i];j!=i;j=r[j]) remove(col[j]);
if(DLX(deep+1)) return true;
for(int j=l[i];j!=i;j=l[j]) resume(col[j]);
}
resume(tempc);
return false;
}

inline void insert_node(int x, int y)
{
control[y]++;
addnode(dcnt);
row[dcnt]=x;
col[dcnt]=y;
insert_col(y, dcnt);
if(h[x]==-1)
h[x]=dcnt;
else
insert_row(h[x], dcnt);
}

int main()
{
#ifdef ACM
freopen("in.txt", "r", stdin);
#endif

int T;
scanf("%d", &T);

while(T--)
{
for(int i=0;i<9;i++) scanf("%s", str+9*i);

dlx_init(4*9*9);
for(int i=1;i<=9;i++) for(int j=1;j<=9;j++)
{
for(int k=1;k<=9;k++) if(str[(i-1)*9+(j-1)]=='0' || str[(i-1)*9+(j-1)]=='0'+k)
{
int rr=(i-1)*9*9+(j-1)*9+(k-1);
insert_node(rr, 81*0+(i-1)*9+k);
insert_node(rr, 81*1+(j-1)*9+k);
insert_node(rr, 81*2+((i-1)/3+(j-1)/3*3)*9+k);
insert_node(rr, 81*3+(i-1)*9+j);
}
}

DLX(0);
}
}

 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  DancingLink