(简单) POJ 3074 Sudoku, DLX+精确覆盖。
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.
经典的数独问题,DLX精确覆盖。。。。。。
构造01矩阵的话,行是9*9*9行,列是4*9*9列;
其中行的话代表的是对于9*9个格子,每一个都有9种可能。。。。。。
然后列的话,4是代表9*9个格子每一个填一个,9*9的格子中的行,列,每个3*3的块,这四种都要保证正确。
然后代码如下:
#include<iostream>
#include<cstring> using namespace std; const int MaxN=;
const int MaxM=;
const int MaxNode=MaxN*MaxM; struct DLX
{
int U[MaxNode],D[MaxNode],L[MaxNode],R[MaxNode],col[MaxNode],row[MaxNode];
int size,n,m;
int H[MaxN],S[MaxM];
int ans[],ans1[]; void init(int _n,int _m)
{
n=_n;
m=_m; for(int i=;i<=m;++i)
{
U[i]=D[i]=i;
L[i]=i-;
R[i]=i+;
row[i]=; S[i]=;
}
L[]=m;
R[m]=; size=m; for(int i=;i<=n;++i)
H[i]=-;
} void Link(int r,int c)
{
col[++size]=c;
row[size]=r;
++S[c]; U[size]=U[c];
D[size]=c;
D[U[c]]=size;
U[c]=size; if(H[r]==-)
H[r]=L[size]=R[size]=size;
else
{
L[size]=L[H[r]];
R[size]=H[r];
R[L[H[r]]]=size;
L[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])
{
U[D[j]]=j;
D[U[j]]=j;
++S[col[j]];
} L[R[c]]=R[L[c]]=c;
} void showans(int d)
{
for(int i=;i<d;++i)
ans1[(ans[i]-)/+]=(ans[i]-)%+; for(int i=;i<=;++i)
cout<<ans1[i]; cout<<endl;
} bool Dance(int d)
{
if(R[]==)
{
showans(d);
return ;
} int c=R[]; for(int i=R[];i!=;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+))
return ; for(int j=L[i];j!=i;j=L[j])
resume(col[j]);
} resume(c); return ;
} void display()
{
for(int i=R[];i!=;i=R[i])
{
cout<<i<<' ';
for(int j=D[i];j!=i;j=D[j])
cout<<'('<<j<<','<<(row[j]-)%+<<')'<<' '; cout<<endl;
}
}
}; DLX dlx;
char s[]; void slove()
{
dlx.init(,); for(int i=;i<=;++i)
for(int j=;j<=;++j)
dlx.Link(j+(i-)*,i); for(int i=;i<=;++i)
for(int j=;j<=;++j)
dlx.Link(*(j-)+(i-)%++*((i-)/),i+); for(int i=;i<=;++i)
for(int j=;j<=;++j)
dlx.Link((j-)*+i,i+); for(int i=;i<=;++i)
for(int j=;j<=;++j)
for(int k=;k<=;++k)
for(int l=;l<=;++l)
for(int m=;m<=;++m)
dlx.Link((i-)*+(j-)*+k+(l-)*+(m-)*,(i-)*+(j-)*+k+); for(int i=;i<;++i)
if(s[i]!='.')
{
dlx.ans1[i+]=s[i]-''; dlx.remove(i+); for(int j=dlx.D[i+];j!=i+;j=dlx.D[j])
{
if((dlx.row[j]-)%+==s[i]-'')
{
for(int k=dlx.R[j];k!=j;k=dlx.R[k])
dlx.remove(dlx.col[k]); break;
}
}
} dlx.Dance();
} int main()
{
ios::sync_with_stdio(false); for(cin>>s;s[]!='e';cin>>s)
slove(); return ;
}
(简单) POJ 3074 Sudoku, DLX+精确覆盖。的更多相关文章
- POJ 3074 Sudoku DLX精确覆盖
DLX精确覆盖.....模版题 Sudoku Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 8336 Accepted: ...
- (简单) 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)
Sudoku Time Limit:1000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u Submit Statu ...
- (中等) 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 ...
- (简单) 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 ...
- 【转】DLX 精确覆盖 重复覆盖
问题描述: 给定一个n*m的矩阵,有些位置为1,有些位置为0.如果G[i][j]==1则说明i行可以覆盖j列. Problem: 1)选定最少的行,使得每列有且仅有一个1. 2)选定最少的行,使得每列 ...
- poj3074 DLX精确覆盖
题意:解数独 分析: 完整的数独有四个充要条件: 1.每个格子都有填数字 2.每列都有1~9中的每个数字 3.每行都有1~9中的每个数字 4.每个9宫格都有1~9中的每个数字 可以转化成精确覆盖问题. ...
- DLX精确覆盖与重复覆盖模板题
hihoCoder #1317 : 搜索四·跳舞链 原题地址:http://hihocoder.com/problemset/problem/1317 时间限制:10000ms 单点时限:1000ms ...
随机推荐
- C++中的函数指针和函数对象总结
篇一.函数指针函数指针:是指向函数的指针变量,在C编译时,每一个函数都有一个入口地址,那么这个指向这个函数的函数指针便指向这个地址.函数指针的用途是很大的,主要有两个作用:用作调用函数和做函数的参数. ...
- C#中partial关键字
1. 什么是局部类型? C# 2.0 引入了局部类型的概念.局部类型允许我们将一个类.结构或接口分成几个部分,分别实现在几个不同的.cs文件中. 局部类型适用于以下情况: (1) 类型特别大,不宜放在 ...
- Dominating Patterns
Dominating Patterns Time Limit:3000MS Memory Limit:Unknown 64bit IO Format:%lld & %llu Descr ...
- 【转】关于C execlp函数的理解
转自:http://bachue.is-programmer.com/posts/21611.html execlp(从PATH 环境变量中查找文件并执行) 相关函数 fork,execl,execl ...
- stdafx文件介绍
MSDN介绍: These files are used to build a precompiled header file Projname.pch and a precompiled types ...
- Android自定义XML属性
<?xml version="1.0" encoding="utf-8"?> <resources> <declare-style ...
- [转]SSL协议与数字证书原理
1 SSL(Secure Socket Lclientyer)是netscclientpe公司设计的主要用于weserver的安全传输协议.这种协议在WESERVER上获得了广泛的应用. SSL在TC ...
- hadoop yarn
简介: 本文介绍了 Hadoop 自 0.23.0 版本后新的 map-reduce 框架(Yarn) 原理,优势,运作机制和配置方法等:着重介绍新的 yarn 框架相对于原框架的差异及改进:并通过 ...
- C4.5算法总结
C4.5是一系列用在机器学习和数据挖掘的分类问题中的算法.它的目标是监督学习:给定一个数据集,其中的每一个元组都能用一组属性值来描述,每一个元组属于一个互斥的类别中的某一类.C4.5的目标是通过学习, ...
- android:contentDescription的作用是什么
在写Android的XML布局文件时,在ImageView或ImageButton中经常会碰到一个提示: Missing contentDescription attribute on image. ...