POJ 2676 Sudoku 解题报告(Dancing Link)
2014-04-29 15:34
429 查看
Sudoku
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);
}
}
相关文章推荐
- POJ 3074 Sudoku 解题报告(Dancing Link)
- POJ 3076 Sudoku 解题报告(Dancing Link)
- POJ-2676 Sudoku 解题报告
- POJ - 2676 Sudoku解题报告(解数独)
- POJ_2676 数独解题报告
- SPOJ 1771 Yet Another N-Queen Problem 解题报告(Dancing Link)
- POJ 2676 解题报告
- HDU 3663 Power Stations 解题报告(Dancing Link)
- HUST 1017 Exact cover 解题报告(Dancing Link)
- ZOJ 3209 Treasure Map 解题报告(Dancing Link)
- POJ-3076 Sudoku(Dancing Link 算法)
- poj解题报告——2676
- poj 3304解题报告
- POJ 2965 玛雅历 解题报告
- POJ 2753 菲波那契数列 解题报告
- POJ 2806 公共子序列 解题报告
- 【原】 POJ 1141 Brackets Sequence 动态规划 解题报告
- 【原】 POJ 1159 Palindrome 回文 动态规划 解题报告
- POJ-3592-Instantaneous Transference 解题报告
- 【原】 POJ 2388 Who's in the Middle 中位数 解题报告