[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 ...
随机推荐
- 喵哈哈村的魔法考试 Round #12 (Div.2) 题解
A 注意答案会超过int,考虑分l,r奇数和偶数来考虑即可. #include<bits/stdc++.h> using namespace std; long long l,r; int ...
- db2执行计划具体操作
explain 1.如果第一次执行,请先(在dbinst用户下) connect to dbname,执行db2 -tvf $HOME/sqllib/misc/EXPLAIN.DDL建立执行计划表 2 ...
- 解决wsl不能安装z.sh问题
z.sh是韦大很推崇的类似autojump的bash插件,能够很方便的寻找目录,然而wsl下不能直接使用,解决方法在其github仓库(z)的issue中找到: Reproduce it at Mic ...
- print2flash文档在线预览应用(java,.net)
一.背景 前段时间,LZ的boss突然给了出了这样一个需求:将原项目中的所有文章关联的附件TXT.PDF.office相关文件全部以flash的形式在网页上进行展示,便于预览.看似简单的需求,整个研发 ...
- 吴伯凡:VUCA时代的自我迭代
吴伯凡:VUCA时代的自我迭代 https://mp.weixin.qq.com/s?src=3×tamp=1506588223&ver=1&signature=nv ...
- C#编程(八十一)---------- 捕获异常
捕获异常 前面主要说了关于异常的一些基础和理论知识,没有进入到正真的异常案例,这一讲通过几个案例来描述一下异常的捕获和处理. 案例代码: using System; using System.Coll ...
- 这可能是最好的RxJava 2.x 入门教程(一)
这可能是最好的 RxJava 2.x 入门教程系列专栏 文章链接: 这可能是最好的 RxJava 2.x 入门教程(完结版)[重磅推出] 这可能是最好的RxJava 2.x 入门教程(一) 这可能是最 ...
- Apache Kafka学习 (二) - 多代理(broker)集群
1. 配置server.properties > cp config/server.properties config/server-1.properties> cp config/ser ...
- 微软BI 之SSAS 系列 - 多维数据集维度用法之三 多对多维度 Many to Many
开篇介绍 对于维度成员和事实数据直接的关系看到更多的可能还是一对一,一对多的关系.比方在事实维度(或退化维度)中一个订单和明细号组合而成的ID,对应的就是事实表中的一条数据,这就是一对一的关系.比方说 ...
- vim Google style format
https://github.com/google/vim-codefmt https://github.com/rhysd/vim-clang-format http://pre.tir.tw/00 ...