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+精确覆盖。的更多相关文章

  1. POJ 3074 Sudoku DLX精确覆盖

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

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

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

  3. POJ 3076 Sudoku DLX精确覆盖

    DLX精确覆盖模具称号..... Sudoku Time Limit: 10000MS   Memory Limit: 65536K Total Submissions: 4416   Accepte ...

  4. POJ 3074 Sudoku (DLX)

    Sudoku Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Submit Statu ...

  5. (中等) 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 ...

  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. 【转】DLX 精确覆盖 重复覆盖

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

  8. poj3074 DLX精确覆盖

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

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

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

随机推荐

  1. 转 Oracle 12c 使用scott等普通用户的方法

    一.前言 最近电脑上安装了oracle 12c数据库,想体验下新特性.安装完后,便像11g一样在dos窗口进行下面的操作: SQL*Plus: Release 12.1.0.2.0 Productio ...

  2. HTML day03表格与表单

    1.表格 一般格式: <table> <thead><!--表格头--> <tr> <th></th> </tr>& ...

  3. EditText的一点深入的了解

    最近在开发android下的记事本程序时,频繁的使用EditText控件,折腾来折腾去,算是对其的了解更深入了一些.特将这些收获记录如下: 一.几个属性的介绍 android:gravity=&quo ...

  4. 为Android硬件抽象层(HAL)模块编写JNI方法提供Java访问硬件服务接口

    在上两篇文章中,我们介绍了如何为Android系统的硬件编写驱动程序,包括如何在Linux内核空间实现内核驱动程序和在用户空间实现硬件抽象层接 口.实现这两者的目的是为了向更上一层提供硬件访问接口,即 ...

  5. 新建aix实例

    http://www.cnblogs.com/kfarvid/archive/2010/12/21/1912553.html   DB2数据库 http://wenku.baidu.com/view/ ...

  6. linux shell 远程执行命令

    经常要部署多台服务器上面的应用,如果一个个机器的登录太麻烦. 所有就想到编写一个脚本来部署不同的服务器 前提条件: 配置ssh免登陆 如果不会的请参加我的另外一篇文章 http://blog.csdn ...

  7. Log4j NDC MDC

    NDC(Nested Diagnostic Context)和MDC(Mapped Diagnostic Context)是log4j种非常有用的两个类,它们用于存储应用程序的上下文信息(contex ...

  8. 学习笔记——门面模式Facade

    门面模式,其实在我们不经意间已经使用了此设计模式.当我们需要将两个子系统,合并对外提供一个大的接口时,我们使用的就是门面模式.对外,子系统的接口是不可见的,只有我们的门面在.

  9. hadoop yarn

    简介: 本文介绍了 Hadoop 自 0.23.0 版本后新的 map-reduce 框架(Yarn) 原理,优势,运作机制和配置方法等:着重介绍新的 yarn 框架相对于原框架的差异及改进:并通过 ...

  10. 转:使用WebDriver过程中遇到的那些问题

    在做web项目的自动化端到端测试时主要使用的是Selenium WebDriver来驱动浏览器.Selenium WebDriver的优点是支持的语言多,支持的浏览器多.主流的浏览器Chrome.Fi ...