您的位置:首页 > 其它

poj 3074(DLX)

2013-04-05 15:28 281 查看
和之前那道数独一样。。。 搜索很强大。只用了47ms

Sudoku

Time Limit: 1000MSMemory Limit: 65536K
Total Submissions: 7108Accepted: 2498
Description

In the game of Sudoku, you are given a large 9 × 9 grid divided into smaller 3 × 3 subgrids. For example,

.2738..1.
.1...6735
.......29
3.5692.8.
.........
.6.1745.3
64.......
9518...7.
.8..6534.
Given some of the numbers in the grid, your goal is to determine the remaining numbers such that the numbers 1 through 9 appear exactly once in (1) each of nine 3 × 3 subgrids, (2) each of the nine rows, and (3) each of the nine columns.

Input

The input test file will contain multiple cases. Each test case consists of a single line containing 81 characters, which represent the 81 squares of the Sudoku grid, given one row at a time. Each character is either a digit (from 1 to 9) or a period (used to indicate an unfilled square). You may assume that each puzzle in the input will have exactly one solution. The end-of-file is denoted by a single line containing the word “end”.

Output

For each test case, print a line representing the completed Sudoku puzzle.

Sample Input

.2738..1..1...6735.......293.5692.8...........6.1745.364.......9518...7..8..6534.
......52..8.4......3...9...5.1...6..2..7........3.....6...1..........7.4.......3.
end

Sample Output

527389416819426735436751829375692184194538267268174593643217958951843672782965341
416837529982465371735129468571298643293746185864351297647913852359682714128574936

Source

Stanford Local 2006
#include <stdio.h>
#include <string.h>
#include <iostream>
using namespace std;
#define N 300000
#define INF 0x3fffffff

char g[10][10];
int ans[1000];
int u[5000],d[5000],r[5000],l[5000],num[5000],H[1000],save[5000],save1[5000];
int flag,head;
const int n=729;
const int m=324;
int id;

void prepare()
{
for(int i=0;i<=m;i++)
{
num[i]=0;
d[i]=i;
u[i]=i;
r[i]=i+1;
l[i+1]=i;
}
r[m]=0;
memset(H,-1,sizeof(H)); // 记录每一行的第一个点
}

void link(int tn,int tm)
{
id++;
save1[id]=tn; // 记录行
++num[save[id]=tm]; // 记录列
d[id]=d[tm];
u[ d[tm] ]=id;
u[id]=tm;
d[tm]=id;
if(H[tn]<0) H[tn]=l[id]=r[id]=id;
else
{
r[id]=r[H[tn]];
l[ r[H[tn]] ]=id;
r[ H[tn] ]=id;
l[id]=H[tn];
}
}

void build()
{
id=m;
int sum;
prepare();
int tn=0;
for(int i=1;i<=81;i++)
{
for(int j=1;j<=9;j++)
{
++tn;
link(tn,i);
}
}
sum=81;
/////////////////
for(int i=1;i<=9;i++) // 每一行
{
tn=(i-1)*81;
for(int k=1;k<=9;k++)
{
int tk=tn+k;
for(int j=1;j<=9;j++)
{
link(tk,sum+(i-1)*9+k);
tk+=9;
}
}
}
sum+=81;
///////////////////////
for(int i=1;i<=9;i++)
{
tn=(i-1)*9;
for(int k=1;k<=9;k++)
{
int tk=tn+k;
for(int j=1;j<=9;j++)
{
link(tk,sum+(i-1)*9+k);
tk+=81;
}
}
}
sum+=81;
/////////////////////////
int tt=0;
for(int i1=1;i1<=3;i1++)
{
for(int j1=1;j1<=3;j1++)
{
tn=(i1-1)*81*3+9*3*(j1-1);
for(int k=1;k<=9;k++)
{
++tt;
int tk;
for(int i=1;i<=3;i++)
{
for(int j=1;j<=3;j++)
{
tk=tn+(i-1)*81+9*(j-1)+k;
link(tk,sum+tt);
}
}
}
}
}
}

void remove(int s)
{
l[ r[s] ]=l[s];
r[ l[s] ]=r[s];
for(int i=d[s];i!=s;i=d[i])
for(int j=r[i];j!=i;j=r[j])
{
u[d[j]]=u[j];
d[u[j]]=d[j];
num[save[j]]--;
}
}

void resume(int s)
{
r[l[s]]=s;
l[r[s]]=s;
for(int i=u[s];i!=s;i=u[i])
for(int j=l[i];j!=i;j=l[j])
{
u[d[j]]=j;
d[u[j]]=j;
num[save[j]]++;
}
}

void dfs(int s)
{
if(flag) return ;
if(r[head]==head)
{
flag=1;
for(int i=0;i<s;i++)
{
int ti,tj,tk;
int tans=save1[ans[i]]-1;
ti= (tans)/81+1;
tj= (tans%81)/9+1;;
tk= (tans%81)%9+1;
//printf("<%d %d> ",ti,tj);
g[ti][tj]=tk+'0';
}
return ;
}
int mi=INF,tu;
for(int i=r[head];i!=head;i=r[i])
if(mi>num[i])
{
mi=num[i];
tu=i;
}
remove(tu);
for(int i=d[tu];i!=tu;i=d[i])
{
for(int j=r[i];j!=i;j=r[j])
remove(save[j]);
ans[s]=i;
dfs(s+1);
for(int j=l[i];j!=i;j=l[j])
resume(save[j]);
}
resume(tu);
}

int main()
{
char T[100];
while(scanf("%s",T))
{
if(T[0]=='e') break;
build();
int tu=0;
int tcnt=0;
for(int i=1;i<=9;i++)
{
for(int j=1;j<=9;j++)
{
g[i][j]=T[tcnt++];
if(g[i][j]!='.')
{
int kk=g[i][j]-'0';
remove( save[ H[tu+kk] ] );
for(int i1=r[ H[tu+kk] ];i1 != H[tu+kk];i1=r[i1])
{
remove( save[i1] );
}
}
tu+=9;
}
}
flag=0;
dfs(0);
for(int i=1;i<=9;i++)
{
for(int j=1;j<=9;j++)
printf("%c",g[i][j]);
}
printf("\n");
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: