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. 关于RuntimException

    对于实现接口的类如果要抛出异常的话,那么接口也要抛出异常 所以RuntimeException只要对于实现接口的类就可以了 对于继承的类也可以这样运用 毕竟在实际开发中接口不一定是自己写的,而且团队可 ...

  2. js中对style中的多个属性进行设值

    js中对style中的多个属性进行设值: 看一下案例自然就明白: document.getElementById("my_wz1").style.cssText="bac ...

  3. 2.10 工具使用 after effects(图形视频处理软件)

    ................... ..................... 暂无 教程素材网 http://img.yipinsucai.com/

  4. st-Spanning Tree

    st-Spanning Tree time limit per test 4 seconds memory limit per test 256 megabytes input standard in ...

  5. HDOJ3743<分治>

    题意:求一个排列的逆序数. #include<cstdio> #include<iostream> #include<algorithm> const int ma ...

  6. php实现json

    <?PHP function __json_encode( $data ) { if( is_array($data) || is_object($data) ) { $islist = is_ ...

  7. websphere安装

    下午来再输 websphere 配置库中已存在应用程序,异常处理 出现此问题的原因之一:操作界面上没有卸载完成. 进行一下操作: 1.删除 $WAS_HOME/profiles/AppSrv01/co ...

  8. Android Studio相关的坑

    html,body,div,span,applet,object,iframe,h1,h2,h3,h4,h5,h6,p,blockquote,pre,a,abbr,acronym,address,bi ...

  9. InnoDB的数据页结构

    页是InnoDB存储引擎管理数据库的最小磁盘单位.页类型为B-tree node的页,存放的即是表中行的实际数据了. InnoDB数据页由以下七个部分组成,如图所示: File Header(文件头) ...

  10. html input密码显示为“*”

    1. 功能需求:HTML中,在input password输入框中输入字符将默认显示为“实体圆点”,但这里要求将实体圆点字符替换成“*”号显示. 2. 局限:鼠标光标非IE浏览器不一定显示,选择多个字 ...