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

Write a Sudoku playing program that reads data sets from a text file.
#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+精确覆盖。的更多相关文章
- POJ 3076 Sudoku DLX精确覆盖
DLX精确覆盖模具称号..... Sudoku Time Limit: 10000MS Memory Limit: 65536K Total Submissions: 4416 Accepte ...
- (简单) POJ 3074 Sudoku, DLX+精确覆盖。
Description In the game of Sudoku, you are given a large 9 × 9 grid divided into smaller 3 × 3 subgr ...
- POJ 3074 Sudoku DLX精确覆盖
DLX精确覆盖.....模版题 Sudoku Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 8336 Accepted: ...
- (中等) 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 ...
- [DLX精确覆盖] hdu 3663 Power Stations
题意: 给你n.m.d,代表有n个城市.m条城市之间的关系,每一个城市要在日后d天内都有电. 对于每一个城市,都有一个发电站,每一个发电站能够在[a,b]的每一个连续子区间内发电. x城市发电了.他相 ...
随机推荐
- Ztree当节点没有下级时不显示下拉图标
select o.*,(select count(*) from sys_org t where t.orgsupid=o.orgid) isLeaf from sys_org o where 1=1
- 后台运行之BackgroundWorker
BackgroundWorker 类允许您在单独的专用线程上运行操作. 耗时的操作(如下载和数据库事务)在长时间运行时可能会导致用户界面 (UI) 似乎处于停止响应状态. 如果您需要能进行响应的用户界 ...
- Spring Boot 系列教程2-Data JPA
Spring Data JPA 用来简化创建 JPA 数据访问层和跨存储的持久层功能. 官网文档连接 http://docs.spring.io/spring-data/jpa/docs/curren ...
- vps安装wordpress遇到的问题(lnmp)
1.要执行请求的操作,WordPress 需要访问您网页服务器的权限. 请输入您的 FTP 登录XXXX完美解决方法 因为在wordpress中新上传的插件的权限都是www用户的,而不是root或其他 ...
- Category / Extention / 属性 / 成员变量 /
转载自:http://blog.csdn.net/itianyi/article/details/8618128 在ios第一版中,我们为输出口同时声明了属性和底层实例变量,那时,属性是oc语言的一个 ...
- easyui实现权限管理
在js中: function makeEasyTree(data){ if(!data) return []; var _newData = []; //最终返回结果 var _treeArray = ...
- ubuntu下安装nagios
第一步安装apache root@root01-virtual-machine:/etc/apache2/conf-available# vi charset.conf 可修改apache服务器的编码 ...
- 扫盲: JAVA基本常识
http://java-mzd.iteye.com/blog/838514
- plat模板修改记录
每个栏目显示文章数 homepage.hph 94行 $loop = new WP_Query('post_type=post&showposts=6&category_name=xi ...
- web.xml中常用元素的解读
前言 针对一个项目而言,通常会有几类XML文件需要书写. web.xml spring-context.xml spring-mvc.xml other.xml ... 不管有多少配置文件,可以肯定的 ...