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.
 
  这个题和POJ 3076没有什么区别,不过就是要注意一个问题,就是MaxNode不要太大,不然的话会MLE,对于数组不用开到MaxN*MaxM这么大,因为数独的很多方格都已经填上了,根本用不了那么大。。。。。。
 
  然后还要注意格式的问题,答案是要空行的。。。。。。
 
代码如下:
#include<iostream>
#include<cstring>
#include<cstdio> using namespace std; const int MaxN=**+;
const int MaxM=**+;
const int MaxNode=MaxN*MaxM/; int cas=; 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)
{
if(cas!=)
cout<<endl; for(int i=;i<d;++i)
ans1[(ans[i]-)/+]=(ans[i]-)%+; for(int i=;i<=;++i)
{
cout<<char(ans1[i]-+'A'); if(i%==)
cout<<endl;
} ++cas;
} 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 getchange(int &r,int &c1,int &c2,int &c3,int &c4,int i,int j,int k)
{
r=(i*+j)*+k;
c1=i*+j+;
c2=+i*+k;
c3=+j*+k;
c4=+((i/)*+(j/))*+k;
} void slove()
{
int r,c1,c2,c3,c4; dlx.init(**,**); for(int i=;i<;++i)
for(int j=;j<;++j)
for(int k=;k<=;++k)
if(s[i*+j]=='-' || s[i*+j]-'A'+==k)
{
getchange(r,c1,c2,c3,c4,i,j,k); dlx.Link(r,c1);
dlx.Link(r,c2);
dlx.Link(r,c3);
dlx.Link(r,c4);
} /* for(int i=1;i<=256;++i)
for(int j=1;j<=16;++j)
if(s[i-1]=='-' || (j+(i-1)*16-1)%16+1==s[i-1]-'A'+1)
dlx.Link(j+(i-1)*16,i); for(int i=1;i<=256;++i)
for(int j=1;j<=16;++j)
if(s[i-1]=='-' || (16*(j-1)+(i-1)%16+1+256*((i-1)/16)-1)%16+1==s[i-1]-'A'+1)
dlx.Link(16*(j-1)+(i-1)%16+1+256*((i-1)/16),i+256); for(int i=1;i<=256;++i)
for(int j=1;j<=16;++j)
if(s[i-1]=='-' || ((j-1)*256+i-1)%16+1==s[i-1]-'A'+1)
dlx.Link((j-1)*256+i,i+512); for(int i=1;i<=4;++i)
for(int j=1;j<=4;++j)
for(int k=1;k<=16;++k)
for(int l=1;l<=4;++l)
for(int m=1;m<=4;++m)
if(s[(i-1)*64+(j-1)*16+k-1]=='-' || ((i-1)*1024+(j-1)*64+k+(l-1)*256+(m-1)*16-1)%16+1==s[(i-1)*64+(j-1)*16+k-1]-'A'+1)
dlx.Link((i-1)*1024+(j-1)*64+k+(l-1)*256+(m-1)*16,(i-1)*64+(j-1)*16+k+768); for(int i=0;i<256;++i)
if(s[i]!='-')
{
dlx.ans1[i+1]=s[i]-'A'+1; dlx.remove(i+1); for(int j=dlx.D[i+1];j!=i+1;j=dlx.D[j])
{
if((dlx.row[j]-1)%16+1==s[i]-'A'+1)
{
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); char st[]; while(cin>>s)
{
for(int i=;i<;++i)
{
cin>>st;
strcat(s,st);
} slove();
} return ;
}

(简单) POJ 3076 Sudoku , DLX+精确覆盖。的更多相关文章

  1. POJ 3076 Sudoku DLX精确覆盖

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

  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. (简单) 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 ...

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

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

  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. [DLX精确覆盖] hdu 3663 Power Stations

    题意: 给你n.m.d,代表有n个城市.m条城市之间的关系,每一个城市要在日后d天内都有电. 对于每一个城市,都有一个发电站,每一个发电站能够在[a,b]的每一个连续子区间内发电. x城市发电了.他相 ...

随机推荐

  1. 1.2 sikuli API

    sikuli API网站:http://nightly.sikuli.de/docs/index.html eclipse中如果要用到相应的 sikuli 功能,可以查看API ,然后import相应 ...

  2. Properties集合

    Map |--Hashtable |--Properties Properties集合特点: 1.该集合中的键和值都是字符串类型 2.集合中的数据可以保存在IO流中或者从IO流中获取数据. 通常该集合 ...

  3. final关键字修饰的变量

    final意义:最终的,不可改变的. 1.修饰变量,为常量,值不可变: 2.修饰对象,值可变,引用不变: 3.修饰方法,方法不可重写: 4.修饰类,无子类,不可以被继承,更不可能被重写. 1.fina ...

  4. Lucene + Hadoop 分布式搜索运行框架 Nut 1.0a9转自http://www.linuxidc.com/Linux/2012-02/53113.htm

    1.概述 不管程序性能有多高,机器处理能力有多强,都会有其极限.能够快速方便的横向与纵向扩展是Nut设计最重要的原则,以此原则形成以分布式并行计算为核心的架构设计.以分布式并行计算为核心的架构设计是N ...

  5. 最简单的epoll的使用范例 : 监听 标准输入 ,并将数据回显到终端

    #include<stdio.h>#include<stdlib.h>#include<unistd.h>#include<sys/epoll.h> # ...

  6. AngularJS vs. jQuery,看看谁更胜一筹

    http://www.apjs.net/ http://docs.angularjs.cn/api/ng/function 本文由PHP100中文网编译,转载请看文末的转载要求,谢谢合作!除非特别声明 ...

  7. drawable文件夹详解

    QVGA使用ldpi,虽然有不同尺寸,但都是120dpi左右:HVGA同理:如下图: -finger    用于触摸屏的设备 -hdpi    近似于240dpi的高级显示密度的屏幕 -mdpi    ...

  8. MySQL 建表

    SET NAMES utf8; SET FOREIGN_KEY_CHECKS = 0; -- ---------------------------- -- Table structure for ` ...

  9. hdu_3549_Flow Problem(最大流)

    题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=3549 题意:求1到n的最大流 题解:模版题,直接上Claris的ISAP,效率是一般dfs的十倍,OR ...

  10. css初始化值

    body,div,dl,dt,dd,ul,ol,li,h1,h2,h3,h4,h5,h6,form,textarea,input,p,th,td,tr,table,tbody,thead,tfoot, ...