[DLX精确覆盖] hdu 1603 A Puzzling Problem
题意:
给你n块碎片,这些碎片不能旋转、翻折。
问你能不能用当中的某些块拼出4*4的正方形。
思路:
精确覆盖裸题了
建图就是看看每一个碎片在4*4中能放哪些位置,这个就作为行。
列就是4*4=16个位置再加上n个碎片也就是16+n
然后注意下成立的判定就好了
代码:
#include"stdio.h"
#include"algorithm"
#include"string.h"
#include"iostream"
#include"queue"
#include"map"
#include"vector"
#include"string"
using namespace std;
#define N 1005*1005
#define RN 1005
#define M 1005
struct DLX
{
int n,m,C;
int U[N],D[N],L[N],R[N],Row[N],Col[N];
int H[M],S[M],cnt,ans[M];
void init(int _n,int _m)
{
n=_n;
m=_m;
for(int i=0; i<=m; i++)
{
U[i]=D[i]=i;
L[i]=(i==0?m:i-1);
R[i]=(i==m?0:i+1);
S[i]=0;
}
C=m;
for(int i=1; i<=n; i++) H[i]=-1;
}
void link(int x,int y)
{
C++;
Row[C]=x;
Col[C]=y;
S[y]++;
U[C]=U[y];
D[C]=y;
D[U[y]]=C;
U[y]=C;
if(H[x]==-1) H[x]=L[C]=R[C]=C;
else
{
L[C]=L[H[x]];
R[C]=H[x];
R[L[H[x]]]=C;
L[H[x]]=C;
}
}
void del(int x)
{
R[L[x]]=R[x];
L[R[x]]=L[x];
for(int i=D[x]; i!=x; 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 rec(int x)
{
for(int i=U[x]; i!=x; i=U[i])
{
for(int j=L[i]; j!=i; j=L[j])
{
U[D[j]]=j;
D[U[j]]=j;
S[Col[j]]++;
}
}
R[L[x]]=x;
L[R[x]]=x;
}
int dance(int x)
{
if(R[0]==0 || R[0]>16)
{
cnt=x;
return 1;
}
int now=R[0];
for(int i=R[0]; i!=0 && i<=16; i=R[i])
{
if(S[i]<S[now]) now=i;
}
del(now);
for(int i=D[now]; i!=now; i=D[i])
{
ans[x]=Row[i];
for(int j=R[i]; j!=i; j=R[j]) del(Col[j]);
if(dance(x+1)) return 1;
for(int j=L[i]; j!=i; j=L[j]) rec(Col[j]);
}
rec(now);
return 0;
}
} dlx;
struct node
{
int m,id;
int w[44];
} kx[1234];
int main()
{
int n;
while(scanf("%d",&n),n)
{
int cnt=0;
dlx.init(n*16,16+n);
for(int i=1; i<=n; i++)
{
int a,b;
scanf("%d%d",&a,&b);
char v[12][12];
for(int j=0; j<a; j++) scanf("%s",v[j]);
for(int xx=1; xx+a<=5; xx++)
{
for(int yy=1; yy+b<=5; yy++)
{
cnt++;
kx[cnt].m=0;
kx[cnt].id=i;
for(int x=0; x<a; x++)
{
for(int y=0; y<b; y++)
{
if(v[x][y]=='1')
{
int tep=(xx+x-1)*4+(yy+y);
dlx.link(cnt,tep);
kx[cnt].w[kx[cnt].m++]=tep;
}
}
}
dlx.link(cnt,16+i);
}
}
}
int f=dlx.dance(0);
if(f==0) puts("No solution possible");
else
{
char mp[10][10];
for(int i=0;i<dlx.cnt;i++)
{
for(int j=0;j<kx[dlx.ans[i]].m;j++)
{
int x,y;
x=(kx[dlx.ans[i]].w[j]-1)/4;
y=kx[dlx.ans[i]].w[j]%4-1;
if(y==-1) y=3;
mp[x][y]=kx[dlx.ans[i]].id+'0';
}
}
for(int i=0;i<4;i++)
{
mp[i][4]='\0';
puts(mp[i]);
}
}
puts("");
}
return 0;
}
[DLX精确覆盖] hdu 1603 A Puzzling Problem的更多相关文章
- [DLX精确覆盖] hdu 3663 Power Stations
题意: 给你n.m.d,代表有n个城市.m条城市之间的关系,每一个城市要在日后d天内都有电. 对于每一个城市,都有一个发电站,每一个发电站能够在[a,b]的每一个连续子区间内发电. x城市发电了.他相 ...
- DLX精确覆盖与重复覆盖模板题
hihoCoder #1317 : 搜索四·跳舞链 原题地址:http://hihocoder.com/problemset/problem/1317 时间限制:10000ms 单点时限:1000ms ...
- 【转】DLX 精确覆盖 重复覆盖
问题描述: 给定一个n*m的矩阵,有些位置为1,有些位置为0.如果G[i][j]==1则说明i行可以覆盖j列. Problem: 1)选定最少的行,使得每列有且仅有一个1. 2)选定最少的行,使得每列 ...
- 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 ...
- (简单) 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 ...
- poj3074 DLX精确覆盖
题意:解数独 分析: 完整的数独有四个充要条件: 1.每个格子都有填数字 2.每列都有1~9中的每个数字 3.每行都有1~9中的每个数字 4.每个9宫格都有1~9中的每个数字 可以转化成精确覆盖问题. ...
- POJ 3074 Sudoku DLX精确覆盖
DLX精确覆盖.....模版题 Sudoku Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 8336 Accepted: ...
- [DLX精确覆盖+打表] hdu 2518 Dominoes
题意: 就是给12种图形,旋转,翻折.有多少种方法构成n*m=60的矩形 思路: 裸的精确覆盖.就是建图麻烦 个人太挫,直接手写每一个图形的各种形态 须要注意的是最后的答案须要除以4 代码: #inc ...
随机推荐
- 通过css实现小三角形
下面是用css做小三角形的demo, <!DOCTYPE html><html lang="en"><head> <meta charse ...
- spring源码分析系列 (3) spring拓展接口InstantiationAwareBeanPostProcessor
更多文章点击--spring源码分析系列 主要分析内容: 一.InstantiationAwareBeanPostProcessor简述与demo示例 二.InstantiationAwareBean ...
- 继承之super关键字的使用
一.super关键字: 在对象的内部使用,可代表父类对象. 1. 访问父类的属性:super.age 2. 访问父类的方法:super.eat() 例: package 关键字extends; pub ...
- 使用 IntraWeb (37) - TIWApplication
每个访问用户都会拥有一个它的实例(WebApplication), 它除了承载 Session(会话)数据, 还要记忆着用户的浏览器信息.登陆信息等等; 另外, 窗体的建立也都依附(Owner)于它, ...
- 在iOS端如何使用Charles用作http调试
转:http://blog.csdn.net/messageloop3/article/details/9966727 在iOS端如何使用Charles用作http调试 After noticing ...
- libnids关于计算校验和引起的抓不到包的现象的解决方法
libnids关于计算校验和引起的抓不到包的现象的解决方法: nids.h中有这么一段: struct nids_chksum_ctl { u_int netaddr; u_int mask; u_i ...
- Oracle 12c: RMAN restore/recover pluggable database
查看数据库状态 运行在归档模式,可拔插数据库name=pdborcl SQL> archive log list; Database log mode Archive Mode Automati ...
- void java.lang.System.gc()
void java.lang.System.gc() Runs the garbage collector. Calling the gc method suggests that the Java ...
- iOS 在tableview的侧滑事件里执行tableView.selectRow无效的解决办法
很奇怪的问题,在执行默认选中一个cell的时候,突然发现这句话不起作用了 (我的场景是:当前cell侧滑删除后,默认选中上一个cell) 搞了半天,终于发现罪魁祸首竟然是因为:这句话写在了侧滑事件的方 ...
- how to check the computer is 32 bit or 64bit in linux
just use cat /proc/cpuinfo in shell