DLX精确覆盖模具称号.....

Sudoku
Time Limit: 10000MS   Memory Limit: 65536K
Total Submissions: 4416   Accepted: 2143

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

Source

[Submit]   [Go Back]   [

problem_id=3076" style="text-decoration:none">Status]  
[Discuss]

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm> using namespace std; const int N=16;
const int maxn=N*N*N+10;
const int maxm=N*N*4+10;
const int maxnode=maxn*4+maxm+10; char sudoku[maxn]; struct DLX
{
int n,m,size;
int U[maxnode],D[maxnode],L[maxnode],R[maxnode],Row[maxnode],Col[maxnode];
int H[maxnode],S[maxnode];
int ansd,ans[maxn];
void init(int _n,int _m)
{
n=_n; m=_m;
for(int i=0;i<=m;i++)
{
S[i]=0;
U[i]=D[i]=i;
L[i]=i-1;
R[i]=i+1;
}
R[m]=0; L[0]=m;
size=m;
for(int i=1;i<=n;i++) H[i]=-1;
}
void Link(int r,int c)
{
++S[Col[++size]=c];
Row[size]=r;
D[size]=D[c];
U[D[c]]=size;
U[size]=c;
D[c]=size;
if(H[r]<0) H[r]=L[size]=R[size]=size;
else
{
R[size]=R[H[r]];
L[R[H[r]]]=size;
L[size]=H[r];
R[H[r]]=size;
}
}
void remove(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])
{
U[D[j]]=U[j];
D[U[j]]=D[j];
--S[Col[j]];
}
}
void resume(int c)
{
for(int i=U[c];i!=c;i=U[i])
for(int j=L[i];j!=i;j=L[j])
++S[Col[U[D[j]]=D[U[j]]=j]];
L[R[c]]=R[L[c]]=c;
}
bool Dance(int d)
{
if(R[0]==0)
{
for(int i=0;i<d;i++) sudoku[(ans[i]-1)/16]=(ans[i]-1)%16+'A';
//printf("%s\n",sudoku);
for(int i=0,sz=N*N;i<sz;i++)
{
putchar(sudoku[i]);
if((i+1)%16==0) putchar(10);
}
return true;
}
int c=R[0];
for(int i=R[0];i!=0;i=R[i])
if(S[i]<S[c]) c=i;
remove(c);
for(int i=D[c];i!=c;i=D[i])
{
ans[d]=Row[i];
for(int j=R[i];j!=i;j=R[j]) remove(Col[j]);
if(Dance(d+1)) return true;
for(int j=L[i];j!=i;j=L[j]) resume(Col[j]);
}
resume(c);
return false;
}
}; DLX dlx; void place(int& r,int& c1,int& c2,int& c3,int& c4,int i,int j,int k)
{
r=(i*N+j)*N+k;
c1=i*N+j+1;
c2=N*N+N*i+k;
c3=N*N*2+N*j+k;
c4=N*N*3+((i/4)*4+(j/4))*N+k;
} int main()
{
while(scanf("%s",sudoku)!=EOF)
{
for(int i=1;i<16;i++)
scanf("%s",sudoku+i*16);
dlx.init(N*N*N,N*N*4);
for(int i=0;i<N;i++)
{
for(int j=0;j<N;j++)
{
for(int k=1;k<=N;k++)
{
if(sudoku[i*N+j]=='-'||sudoku[i*N+j]==k+'A'-1)
{
int r,c1,c2,c3,c4;
place(r,c1,c2,c3,c4,i,j,k);
dlx.Link(r,c1);
dlx.Link(r,c2);
dlx.Link(r,c3);
dlx.Link(r,c4);
}
}
}
}
dlx.Dance(0);
putchar(10);
}
return 0;
}

版权声明:来自: 代码代码猿猿AC路 http://blog.csdn.net/ck_boss

POJ 3076 Sudoku DLX精确覆盖的更多相关文章

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

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

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

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

  3. POJ 3074 Sudoku DLX精确覆盖

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

  4. (中等) HDU 4069 Squiggly Sudoku , DLX+精确覆盖。

    Description Today we play a squiggly sudoku, The objective is to fill a 9*9 grid with digits so that ...

  5. 【转】DLX 精确覆盖 重复覆盖

    问题描述: 给定一个n*m的矩阵,有些位置为1,有些位置为0.如果G[i][j]==1则说明i行可以覆盖j列. Problem: 1)选定最少的行,使得每列有且仅有一个1. 2)选定最少的行,使得每列 ...

  6. (简单) HUST 1017 Exact cover , DLX+精确覆盖。

    Description There is an N*M matrix with only 0s and 1s, (1 <= N,M <= 1000). An exact cover is ...

  7. poj3074 DLX精确覆盖

    题意:解数独 分析: 完整的数独有四个充要条件: 1.每个格子都有填数字 2.每列都有1~9中的每个数字 3.每行都有1~9中的每个数字 4.每个9宫格都有1~9中的每个数字 可以转化成精确覆盖问题. ...

  8. DLX精确覆盖与重复覆盖模板题

    hihoCoder #1317 : 搜索四·跳舞链 原题地址:http://hihocoder.com/problemset/problem/1317 时间限制:10000ms 单点时限:1000ms ...

  9. zoj 3209.Treasure Map(DLX精确覆盖)

    直接精确覆盖 开始逐行添加超时了,换成了单点添加 #include <iostream> #include <cstring> #include <cstdio> ...

随机推荐

  1. ERROR: Error in Log_event::read_log_event(): 'read error', data_len: 438, event_type: 2

    分析从库1062问题,解析从库binlog日志,报错如下 [root@xxxdb0402 tmp]# mysqlbinlog mysql-bin.004271 > 4.log ERROR: Er ...

  2. 什么样的企业造什么样的软件最easy成功?

    事件1: 一般软件企业按功能分,大体分业务应用型软件和系统工具型软件. 按市场分,应用型软件企业较多,直接贴近生活:系统工具类较少,间接贴近大众较少. 事件2: 软件企业中,当中中小型企业老板存在非常 ...

  3. DotNet基础

    DotNet基础 URL特殊字符转义 摘要: URL中一些字符的特殊含义,基本编码规则如下: 1.空格换成加号(+) 2.正斜杠(/)分隔目录和子目录 3.问号(?)分隔URL和查询 4.百分号(%) ...

  4. Jetty 9.3庆祝20周年生日快乐,并添加HTTP/2支持

    本文来源于我在InfoQ中文站翻译的文章,原文地址是:http://www.infoq.com/cn/news/2015/06/Building-Distributed-Systems 今年6月12日 ...

  5. poj Budget

    Budget 建图好题.不知道为什么提交一直TLE. 然后.该了几次,看了别人的普通网络流都过了. 我觉得可能是卡DINIC的某些部分吧.这题就是一道普通的上下界最小流. 建图麻烦,所以说一下建图吧. ...

  6. wamp在win7下64位系统memcache/memcached安装教程

    折腾了1个多小时,终于搞定.操作系统时64位的,php5.3.13 类似于上一篇的xdebug安装教程~~ memcache和memcached的区别  在自己的新程序中打算全面应用memcached ...

  7. ecshop 调用其他数据库中的商品

    ecshop中修改includes/cls_ecshop.php中第53行 function table($str) { /* if($str=='goods'){ return '`ecshop3' ...

  8. Web监听器导图详解(转)

    阅读目录 Web监听器 监听器的分类 Servlet版本与Tomcat版本 getAttribute与getParameter的区别 参考 监听器是JAVA Web开发中很重要的内容,其中涉及到的知识 ...

  9. 《深入浅出 Java Concurrency》—锁紧机构(一)Lock与ReentrantLock

    转会:http://www.blogjava.net/xylz/archive/2010/07/05/325274.html 前面的章节主要谈谈原子操作,至于与原子操作一些相关的问题或者说陷阱就放到最 ...

  10. cocos2d-x截图功能clippingnode它也可用于——白费

    许多其他精彩分享:http://blog.csdn.net/u010229677 3.1版本号: 在Director数: bool Director::saveScreenshot(const std ...