搜索(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 ...
随机推荐
- 如何学习一门新技术-iOS开发
如何快速学习一门新技术 以CoreBluetooth 蓝牙开发为例.我们可以从官方获得的资源有:SampleCode, Documentation,API Reference. 先从Documenta ...
- oracle 字符串切割成结果集方法
oracle字符串切割几种方式 方法一: SELECT COLUMN_VALUE FROM TABLE(SYS.ODCIVARCHAR2LIST('1','2','3','4','5')); 方法二: ...
- 关于HTTP请求报文和响应报文学习笔记
超文本传输协议(Hypertext Transfer Protocol,简称HTTP)是应用层的一种通信协议.它是一种请求/响应式的协议,即一个客户端与服务器建立连接后,向服务器发送一个请求;服务器接 ...
- C# ashx与html的联合使用
本文将介绍ashx和html的联合使用方法,尽管目前流行mvc,但handler一般处理程序还是ASP.NET的基础知识,结合html页面,做出来的网页绝对比WebForm的简洁和效率高. 首先,概要 ...
- 10.4 noip模拟试题
题目名称 PA 青春 三部曲 名称 huakai taritari truetears 输入 huakai.in taritari.in truetears.in 输出 huakai.out tari ...
- webGIS(离线版)研究路线归总
特注:不做详解,说明网上资源很多,找一篇,照着走一遍即可. 1.数据源要满足开源.Free且地理信息要完善 几经周折,选择了OSM,具体信息可以去其官方查看(它竟然把中国一分为二,大陆.台湾,坑爹!! ...
- Android开发--二维码开发应用(转载!)
android项目开发 二维码扫描 基于android平台的二维码扫描项目,可以查看结果并且链接网址 工具/原料 zxing eclipse 方法/步骤 首先需要用到google提供的zxin ...
- java异步上传图片
第一步:引入需要的js <script src="/res/common/js/jquery.js" type="text/javascript"> ...
- 关于ASP.NET控件方面的学习(恢复版)
前段时间没有把学习中的遇到的问题和解决方法详细总结,今天整理整理.. 鉴于我们这个研究生论文管理系统是小组形式,所以说虽然我只负责数据库,但是其它部分也多少有些工作方面的涉及,最后感谢各位同学和老师的 ...
- C# div布局
本文讲解使用DIV+CSS布局最基本的内容,读完本文你讲会使用DIV+CSS进行简单的页面布局. 转载请标明:http://www.kwstu.com/ArticleView/divcss_20139 ...