POJ 3074 :

Description

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

. 2 7 3 8 . . 1 .
. 1 . . . 6 7 3 5
. . . . . . . 2 9
3 . 5 6 9 2 . 8 .
. . . . . . . . .
. 6 . 1 7 4 5 . 3
6 4 . . . . . . .
9 5 1 8 . . . 7 .
. 8 . . 6 5 3 4 .

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=;
const int maxn=;
const int maxm=;
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=;i<=m;i++)
{
L[i]=i-;R[i]=i+;
U[i]=D[i]=i;C[i]=;
}
cnt=m;L[]=m;R[m]=; for(int i=;i<=n;i++)
H[i]=,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[])return true;
int p=R[];
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=;i<=;i++)
for(int j=(i-)*+;j<=i*;j++)
if(used[j]){
int Color=j-(i-)*;
printf("%d",Color);
}
printf("\n");
}
}DLX; int Area(int x,int y)
{
if(x<=&&y<=)return ;
if(x<=&&y<=)return ;
if(x<=)return ;
if(x<=&&y<=)return ;
if(x<=&&y<=)return ;
if(x<=)return ;
if(y<=)return ;
if(y<=)return ;
return ;
} char str[];
int main()
{
int x,y;
while(~scanf("%s",str+))
{
if(!strcmp(str+,"end"))break;
DLX.Init(,);x=;y=;
for(int i=;i<=;i++)
{
for(int j=(i-)*+;j<=i*;j++)
{
int Color=j-(i-)*;
if(str[i]!='.'&&str[i]-''!=Color)
continue; DLX.Link(j,(x-)*+Color); //行中对应颜色
DLX.Link(j,+(y-)*+Color); //列中对应颜色
DLX.Link(j,+Area(x,y)*+Color);//块中对应颜色
DLX.Link(j,+i); //矩阵中对应位置
}
y++;x+=y/;y=(y-)%+;
}
DLX.Solve();
DLX.Print();
}
return ;
}

POJ 3076:

 #include <iostream>
#include <cstring>
#include <cstdio>
using namespace std;
const int maxnode=;
const int maxn=;
const int maxm=;
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=;i<=m;i++)
{
L[i]=i-;R[i]=i+;
U[i]=D[i]=i;C[i]=;
}
cnt=m;L[]=m;R[m]=; for(int i=;i<=n;i++)
H[i]=,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[])return true;
int p=R[];
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=;i<=;i++){
for(int j=(i-)*+;j<=i*;j++)
if(used[j]){
int Color=j-(i-)*;
printf("%c",'A'+Color-);
break;
}
if(i%==)
printf("\n");
}
printf("\n");
}
}DLX; int Area(int x,int y)
{
if(x<=&&y<=)return ;
if(x<=&&y<=)return ;
if(x<=&&y<=)return ;
if(x<=)return ; if(x<=&&y<=)return ;
if(x<=&&y<=)return ;
if(x<=&&y<=)return ;
if(x<=)return ; if(x<=&&y<=)return ;
if(x<=&&y<=)return ;
if(x<=&&y<=)return ;
if(x<=)return ; if(y<=)return ;
if(y<=)return ;
if(y<=)return ;
return ;
} char str[],s[];
int main()
{
while(true){
int x=,y=;
DLX.Init(,);
for(int i=;i<;i+=){
if(not~scanf("%s",s))return ;
for(int j=i;j<i+;j++)
str[j]=s[j-i];
}
for(int i=;i<=;i++)
{
for(int j=(i-)*+;j<=i*;j++)
{
int Color=j-(i-)*;
if(str[i]!='-'&&str[i]-'A'+!=Color)
continue; DLX.Link(j,(x-)*+Color); //行中对应颜色
DLX.Link(j,+(y-)*+Color); //列中对应颜色
DLX.Link(j,+Area(x,y)*+Color);//块中对应颜色
DLX.Link(j,+i); //矩阵中对应位置
}
y++;x+=y/;y=(y-)%+;
}
DLX.Solve();
DLX.Print();
}
return ;
}

搜索(DLX): POJ 3074 3076 Sudoku的更多相关文章

  1. DLX (poj 3074)

    题目:Sudoku 匪夷所思的方法,匪夷所思的速度!!! https://github.com/ttlast/ACM/blob/master/Dancing%20Link%20DLX/poj%2030 ...

  2. 【POJ 3074】 Sudoku

    [题目链接] http://poj.org/problem?id=3074 [算法] 将数独问题转化为精确覆盖问题,用Dancing Links求解 转化方法如下 : 我们知道,在一个数独中 : 1. ...

  3. 【POJ】3076 Sudoku

    DLX第一题,模板留念. /* 3076 */ #include <iostream> #include <string> #include <map> #incl ...

  4. POJ 3074 Sudoku (DLX)

    Sudoku Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Submit Statu ...

  5. 搜索+剪枝——POJ 1011 Sticks

    搜索+剪枝--POJ 1011 Sticks 博客分类: 算法 非常经典的搜索题目,第一次做还是暑假集训的时候,前天又把它翻了出来 本来是想找点手感的,不想在原先思路的基础上,竟把它做出来了而且还是0 ...

  6. (简单) POJ 3076 Sudoku , DLX+精确覆盖。

    Description A Sudoku grid is a 16x16 grid of cells grouped in sixteen 4x4 squares, where some cells ...

  7. POJ 3076 Sudoku DLX精确覆盖

    DLX精确覆盖模具称号..... Sudoku Time Limit: 10000MS   Memory Limit: 65536K Total Submissions: 4416   Accepte ...

  8. POJ 3074 Sudoku DLX精确覆盖

    DLX精确覆盖.....模版题 Sudoku Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 8336   Accepted: ...

  9. (简单) POJ 3074 Sudoku, DLX+精确覆盖。

    Description In the game of Sudoku, you are given a large 9 × 9 grid divided into smaller 3 × 3 subgr ...

随机推荐

  1. Stream类

    为什么需要 Stream Stream 作为 Java 8 的一大亮点,它与 java.io 包里的 InputStream 和 OutputStream 是完全不同的概念.它也不同于 StAX 对 ...

  2. Android Studio创建工程时一直卡在下载Gradle

    一直提示这个进度条,查了不少资料,有的说FQ,有的说下载gradle后运行下bin里面的批处理,再在环境变量里Path中加入路径,我都试了,都不和... 按理说FQ了可以下载了吧,但是半天没下载完,一 ...

  3. oracle中drop、delete和truncate的区别

    oracle中drop.delete和truncate的区别 oracle中可以使用drop.delete和truncate三个命令来删除数据库中的表,网上有许多文章和教程专门讲解了它们之间的异同,我 ...

  4. HTML5 <Audio>标签API整理(一)

    简单实例: <audio id="myAudio"></audio> <script> var myAudio = document.getEl ...

  5. ASP.NET如何发布更新

    如果一个应用程序迭代开发后,我们如何来进行发布更新呢? 这里,我用Visual Studio 2008和SQL server来演示. 一.程序修改完毕后,我们要进行以下操作: 1.1 点击解决方案,重 ...

  6. Core Animation之CAKeyframeAnimation

    在上一篇专题文章中我们学习了iOS核心动画CoreAnimation中CABasicAnimation动画的使用方法.CABasicAnimation已经可以应付一些比较简单的应用场景了,比如view ...

  7. java 迭代器iterator

    对于如ArrayList<E>类的数据,常用iterator遍历. ArrayList<String> list = new ArrayList<String>() ...

  8. Proxy 模式

    在以下集中情况下可以用 Proxy模式解决问题: 1)创建开销大的对象时候,比如显示一幅大的图片,我们将这个创建的过程交给代理去完成,GoF 称之为虚代理(Virtual Proxy): 2)为网络上 ...

  9. 【ZOJ2112】【整体二分+树状数组】带修改区间第k大

    The Company Dynamic Rankings has developed a new kind of computer that is no longer satisfied with t ...

  10. javascript 函数声明问题

    (function(){ //运行正常 test1(); function test1() { console.log('123'); }; })() (function(){ //出错,test2未 ...