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. 利用jpedal进行pdf转换成jpeg,jpg,png,tiff,tif等格式的图片

    项目中运用到pdf文件转换成image图片,开始时使用pdfbox开源库进行图片转换,但是转换出来的文件中含有部分乱码的情况.下面是pdfBox 的pdf转换图片的代码示例. try{ String ...

  2. MySQL日志概述

    为了防止无良网站的爬虫抓取文章,特此标识,转载请注明文章出处.LaplaceDemon/SJQ. http://www.cnblogs.com/shijiaqi1066/p/3859866.html ...

  3. RPM工具

    一.RMP工具管理RPM包 rpm命令是RPM软件包的管理工具.rpm原本是Red Hat Linux发行版专门用来管理Linux各项套件的程序,由于它遵循GPL规则且功能强大方便,因而广受欢迎.逐渐 ...

  4. 解决win service 2003 IIS发布Gis网站后,访问地图服务出错,无法正常打开而且 事件查看器出现错误提示。

    错误详情: 应用程序-特定 权限设置未将 COM 服务器应用程序(CLSID 为{379376DB-AEA6-40D1-9491-9345E61EF6BE})的 本地 激活 权限授予用户 NT AUT ...

  5. 读取xml字符串

    string strXml = @"<MessageData><pm_id>10</pm_id><pm_title>这是公司或者产品的名称&l ...

  6. 完全卸载oracle

    今天在网上看到有位网友写的篇日志,感觉蛮好的,一般卸载oracle有4个地方需求注意:1)Services,2)software,3eventlog,4)path. 1.关闭 oracle 所有的服务 ...

  7. PS2251-07 海力士(金士顿U盘量产,成功!)

    U盘挂掉了,用芯片无忧测到是这个样子的,看到是PS2251-07 海力士的 网上找了很多方法都不成功,最后找到了两个可以量产成功的方法,建议使用第一种. 首先,附上三个检测工具 芯片无忧.GetInf ...

  8. 一个中型项目:本地校园App

    好暨: 这个项目的起源于课堂老师作业的要求.老师要求一年下来完成一个构想并实现Demo.思考良久,在要提交构想的那个晚上,想到了校园App,当时团队只有两个人,但我感觉到:就是它了!项目启动时间——2 ...

  9. 如何让input之间无空隙

    有如下两个input: <form action="http://www.example.com/index/search" method="get"&g ...

  10. JQuery zoom插件学习

    jquery zoom是一款图片放大插件,经常用在商城商品页面里. 使用JQuery zoom插件,除了需要引入JQuery.js外,还要引入JQuery.zoom.js文件及jqzoom.css文件 ...