您的位置:首页 > 其它

搜索(DLX): POJ 3074 3076 Sudoku

2016-03-04 21:03 399 查看
POJ 3074 :


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


POJ 3076:


Description
A Sudoku grid is a 16x16 grid of cells grouped in sixteen 4x4 squares, where some cells are filled with letters from A to P (the first 16 capital letters of the English alphabet), as shown in figure 1a. The game is to fill all the empty grid cells with letters from A to P such that each letter from the grid occurs once only in the line, the column, and the 4x4 square it occupies. The initial content of the grid satisfies the constraints mentioned above and guarantees a unique solution.



Write a Sudoku playing program that reads data sets from a text file.
Input

Each
data set encodes a grid and contains 16 strings on 16 consecutive lines
as shown in figure 2. The i-th string stands for the i-th line of the
grid, is 16 characters long, and starts from the first position of the
line. String characters are from the set {A,B,…,P,-}, where – (minus)
designates empty grid cells. The data sets are separated by single empty
lines and terminate with an end of file.
Output

The program prints the solution of the input encoded grids in the same format and order as used for input.
Sample Input

--A----C-----O-I
-J--A-B-P-CGF-H-
--D--F-I-E----P-
-G-EL-H----M-J--
----E----C--G---
-I--K-GA-B---E-J
D-GP--J-F----A--
-E---C-B--DP--O-
E--F-M--D--L-K-A
-C--------O-I-L-
H-P-C--F-A--B---
---G-OD---J----H
K---J----H-A-P-L
--B--P--E--K--A-
-H--B--K--FI-C--
--F---C--D--H-N-

Sample Output

FPAHMJECNLBDKOGI
OJMIANBDPKCGFLHE
LNDKGFOIJEAHMBPC
BGCELKHPOFIMAJDN
MFHBELPOACKJGNID
CILNKDGAHBMOPEFJ
DOGPIHJMFNLECAKB
JEKAFCNBGIDPLHOM
EBOFPMIJDGHLNKCA
NCJDHBAEKMOFIGLP
HMPLCGKFIAENBDJO
AKIGNODLBPJCEFMH
KDEMJIFNCHGAOPBL
GLBCDPMHEONKJIAF
PHNOBALKMJFIDCEG
IAFJOECGLDPBHMNK



  这两道题几乎一样的,就是要你求一个数独矩阵。

  难得有这样一道接近生活的信息题啊~~~

POJ 3074:

#include <iostream>
#include <cstring>
#include <cstdio>
using namespace std;
const int maxnode=100010;
const int maxn=755;
const int maxm=345;
struct DLX
{
int L[maxnode],R[maxnode],U[maxnode],D[maxnode],Row[maxnode],Col[maxnode],C[maxm],H[maxn],cnt;
bool used[maxn];
void Init(int n,int m)
{
for(int i=0;i<=m;i++)
{
L[i]=i-1;R[i]=i+1;
U[i]=D[i]=i;C[i]=0;
}
cnt=m;L[0]=m;R[m]=0;

for(int i=1;i<=n;i++)
H[i]=0,used[i]=false;
}
void Link(int x,int y)
{
C[Col[++cnt]=y]++;
Row[cnt]=x;

U[cnt]=y;
U[D[y]]=cnt;
D[cnt]=D[y];
D[y]=cnt;

if(H[x])
L[R[H[x]]]=cnt,R[cnt]=R[H[x]],R[H[x]]=cnt,L[cnt]=H[x];
else
H[x]=L[cnt]=R[cnt]=cnt;
}

void Delete(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])
--C[Col[j]],U[D[j]]=U[j],D[U[j]]=D[j];
}

void Resume(int c)
{
L[R[c]]=c;R[L[c]]=c;
for(int i=U[c];i!=c;i=U[i])
for(int j=L[i];j!=i;j=L[j])
++C[Col[j]],U[D[j]]=j,D[U[j]]=j;
}

bool Solve()
{
if(!R[0])return true;
int p=R[0];
for(int i=R[p];i;i=R[i])
if(C[p]>C[i])
p=i;
Delete(p);
for(int i=D[p];i!=p;i=D[i]){
used[Row[i]]=true;
for(int j=R[i];j!=i;j=R[j])
Delete(Col[j]);
if(Solve())
return true;
used[Row[i]]=false;
for(int j=L[i];j!=i;j=L[j])
Resume(Col[j]);
}
Resume(p);
return false;
}
void Print()
{
for(int i=1;i<=81;i++)
for(int j=(i-1)*9+1;j<=i*9;j++)
if(used[j]){
int Color=j-(i-1)*9;
printf("%d",Color);
}
printf("\n");
}
}DLX;

int Area(int x,int y)
{
if(x<=3&&y<=3)return 0;
if(x<=3&&y<=6)return 1;
if(x<=3)return 2;
if(x<=6&&y<=3)return 3;
if(x<=6&&y<=6)return 4;
if(x<=6)return 5;
if(y<=3)return 6;
if(y<=6)return 7;
return 8;
}

char str[110];
int main()
{
int x,y;
while(~scanf("%s",str+1))
{
if(!strcmp(str+1,"end"))break;
DLX.Init(729,324);x=1;y=1;
for(int i=1;i<=81;i++)
{
for(int j=(i-1)*9+1;j<=i*9;j++)
{
int Color=j-(i-1)*9;
if(str[i]!='.'&&str[i]-'0'!=Color)
continue;

DLX.Link(j,(x-1)*9+Color); //行中对应颜色
DLX.Link(j,81+(y-1)*9+Color); //列中对应颜色
DLX.Link(j,162+Area(x,y)*9+Color);//块中对应颜色
DLX.Link(j,243+i); //矩阵中对应位置
}
y++;x+=y/10;y=(y-1)%9+1;
}
DLX.Solve();
DLX.Print();
}
return 0;
}


POJ 3076:


#include <iostream>
#include <cstring>
#include <cstdio>
using namespace std;
const int maxnode=500010;
const int maxn=4100;
const int maxm=1030;
struct DLX
{
int L[maxnode],R[maxnode],U[maxnode],D[maxnode],Row[maxnode],Col[maxnode],C[maxm],H[maxn],cnt;
bool used[maxn];
void Init(int n,int m)
{
for(int i=0;i<=m;i++)
{
L[i]=i-1;R[i]=i+1;
U[i]=D[i]=i;C[i]=0;
}
cnt=m;L[0]=m;R[m]=0;

for(int i=1;i<=n;i++)
H[i]=0,used[i]=false;
}
void Link(int x,int y)
{
C[Col[++cnt]=y]++;
Row[cnt]=x;

U[cnt]=y;
U[D[y]]=cnt;
D[cnt]=D[y];
D[y]=cnt;

if(H[x])
L[R[H[x]]]=cnt,R[cnt]=R[H[x]],R[H[x]]=cnt,L[cnt]=H[x];
else
H[x]=L[cnt]=R[cnt]=cnt;
}

void Delete(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])
--C[Col[j]],U[D[j]]=U[j],D[U[j]]=D[j];
}

void Resume(int c)
{
L[R[c]]=c;R[L[c]]=c;
for(int i=U[c];i!=c;i=U[i])
for(int j=L[i];j!=i;j=L[j])
++C[Col[j]],U[D[j]]=j,D[U[j]]=j;
}

bool Solve()
{
if(!R[0])return true;
int p=R[0];
for(int i=R[p];i;i=R[i])
if(C[p]>C[i])
p=i;
Delete(p);
for(int i=D[p];i!=p;i=D[i]){
used[Row[i]]=true;
for(int j=R[i];j!=i;j=R[j])
Delete(Col[j]);
if(Solve())
return true;
used[Row[i]]=false;
for(int j=L[i];j!=i;j=L[j])
Resume(Col[j]);
}
Resume(p);
return false;
}
void Print()
{
for(int i=1;i<=256;i++){
for(int j=(i-1)*16+1;j<=i*16;j++)
if(used[j]){
int Color=j-(i-1)*16;
printf("%c",'A'+Color-1);
break;
}
if(i%16==0)
printf("\n");
}
printf("\n");
}
}DLX;

int Area(int x,int y)
{
if(x<=4&&y<=4)return 0;
if(x<=4&&y<=8)return 1;
if(x<=4&&y<=12)return 2;
if(x<=4)return 3;

if(x<=8&&y<=4)return 4;
if(x<=8&&y<=8)return 5;
if(x<=8&&y<=12)return 6;
if(x<=8)return 7;

if(x<=12&&y<=4)return 8;
if(x<=12&&y<=8)return 9;
if(x<=12&&y<=12)return 10;
if(x<=12)return 11;

if(y<=4)return 12;
if(y<=8)return 13;
if(y<=12)return 14;
return 15;
}

char str[260],s[17];
int main()
{
while(true){
int x=1,y=1;
DLX.Init(4096,1024);
for(int i=1;i<256;i+=16){
if(not~scanf("%s",s))return 0;
for(int j=i;j<i+16;j++)
str[j]=s[j-i];
}
for(int i=1;i<=256;i++)
{
for(int j=(i-1)*16+1;j<=i*16;j++)
{
int Color=j-(i-1)*16;
if(str[i]!='-'&&str[i]-'A'+1!=Color)
continue;

DLX.Link(j,(x-1)*16+Color); //行中对应颜色
DLX.Link(j,256+(y-1)*16+Color); //列中对应颜色
DLX.Link(j,512+Area(x,y)*16+Color);//块中对应颜色
DLX.Link(j,768+i); //矩阵中对应位置
}
y++;x+=y/17;y=(y-1)%16+1;
}
DLX.Solve();
DLX.Print();
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: