搜索(DLX): POJ 3074 3076 Sudoku
POJ 3074 :
DescriptionIn 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.
endSample Output
527389416819426735436751829375692184194538267268174593643217958951843672782965341
416837529982465371735129468571298643293746185864351297647913852359682714128574936
POJ 3076:
DescriptionA 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的更多相关文章
- DLX (poj 3074)
题目:Sudoku 匪夷所思的方法,匪夷所思的速度!!! https://github.com/ttlast/ACM/blob/master/Dancing%20Link%20DLX/poj%2030 ...
- 【POJ 3074】 Sudoku
[题目链接] http://poj.org/problem?id=3074 [算法] 将数独问题转化为精确覆盖问题,用Dancing Links求解 转化方法如下 : 我们知道,在一个数独中 : 1. ...
- 【POJ】3076 Sudoku
DLX第一题,模板留念. /* 3076 */ #include <iostream> #include <string> #include <map> #incl ...
- POJ 3074 Sudoku (DLX)
Sudoku Time Limit:1000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u Submit Statu ...
- 搜索+剪枝——POJ 1011 Sticks
搜索+剪枝--POJ 1011 Sticks 博客分类: 算法 非常经典的搜索题目,第一次做还是暑假集训的时候,前天又把它翻了出来 本来是想找点手感的,不想在原先思路的基础上,竟把它做出来了而且还是0 ...
- (简单) POJ 3076 Sudoku , DLX+精确覆盖。
Description A Sudoku grid is a 16x16 grid of cells grouped in sixteen 4x4 squares, where some cells ...
- POJ 3076 Sudoku DLX精确覆盖
DLX精确覆盖模具称号..... Sudoku Time Limit: 10000MS Memory Limit: 65536K Total Submissions: 4416 Accepte ...
- POJ 3074 Sudoku DLX精确覆盖
DLX精确覆盖.....模版题 Sudoku Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 8336 Accepted: ...
- (简单) POJ 3074 Sudoku, DLX+精确覆盖。
Description In the game of Sudoku, you are given a large 9 × 9 grid divided into smaller 3 × 3 subgr ...
随机推荐
- vsftpd的主配置文件详解
anonymous_enable=YES 允许匿名用户登录#local_enable=YES 允许本地用户登录#write_enable=YES 允许写权限#local_umask=022 ##ano ...
- HDFS的Java客户端操作代码(查看HDFS下的文件是否存在)
1.查看HDFS目录下得文件是否存在 package Hdfs; import java.io.IOException; import java.net.URI; import org.apache. ...
- 使用了hibernate时候乱码问题
在配置文件的url地址最后加上characterEncoding=utf-8
- 服务器证书安装配置指南(IIS7.5) 分类: ASP.NET 2014-11-05 12:39 105人阅读 评论(0) 收藏
1.启动IIS管理器,点击开始菜单->所有程序->管理工具->Internet信息服务(IIS)管理器: 2.选择"服务器证书": 3.在右边窗口,选择" ...
- javascript DOM小结
一:定义 dom:文档对象模型. dom是针对HTML和XML文档的一个API.dom描绘了一个层次化的节点树,允许开发人员添加.移除.修改页面的某一部分. 1:childNodes(返回当前节点的子 ...
- 百度ios 开发面试题
百度移动云可穿戴部门的面试经历,面试官都非常热情友好,一上来到弄的我挺不好意思的.下面记录一下自己的面试过程,因为我真的没啥面试经验,需要总结下. 1面 Objective C runtime lib ...
- hdoj 2601(判断N=i*j+i+j)
Problem E Time Limit : 6000/3000ms (Java/Other) Memory Limit : 32768/32768K (Java/Other) Total Sub ...
- java中jdk环境配置
配置java环境,俗称jdk环境 首先进入配置环境的目录下:右键鼠标我的电脑->属性->高级系统设置->环境变量,在对应的"系统变量"框下配置一下变量: 规范的配 ...
- C#编程连接数据库,通过更改配置文件切换数据库功能。
该实例主要应用情景:假如某公司用mysql当做数据库服务器,由于发现mysql数据库在运行中出现不稳定情况,针对这情况,厂家要求更换连接数据库方式,改用SQL server数据库,来满足 ...
- Apache:To Config The Vhost of Django Project
It is not a good idea to use dev server in Production Environment. Apache or Nginx are good choice.B ...