题意:

给你n块碎片,这些碎片不能旋转、翻折。

问你能不能用当中的某些块拼出4*4的正方形。

思路:

精确覆盖裸题了

建图就是看看每一个碎片在4*4中能放哪些位置,这个就作为行。

列就是4*4=16个位置再加上n个碎片也就是16+n

然后注意下成立的判定就好了

代码:

  1. #include"stdio.h"
  2. #include"algorithm"
  3. #include"string.h"
  4. #include"iostream"
  5. #include"queue"
  6. #include"map"
  7. #include"vector"
  8. #include"string"
  9. using namespace std;
  10. #define N 1005*1005
  11. #define RN 1005
  12. #define M 1005
  13. struct DLX
  14. {
  15. int n,m,C;
  16. int U[N],D[N],L[N],R[N],Row[N],Col[N];
  17. int H[M],S[M],cnt,ans[M];
  18. void init(int _n,int _m)
  19. {
  20. n=_n;
  21. m=_m;
  22. for(int i=0; i<=m; i++)
  23. {
  24. U[i]=D[i]=i;
  25. L[i]=(i==0?m:i-1);
  26. R[i]=(i==m?0:i+1);
  27. S[i]=0;
  28. }
  29. C=m;
  30. for(int i=1; i<=n; i++) H[i]=-1;
  31. }
  32. void link(int x,int y)
  33. {
  34. C++;
  35. Row[C]=x;
  36. Col[C]=y;
  37. S[y]++;
  38. U[C]=U[y];
  39. D[C]=y;
  40. D[U[y]]=C;
  41. U[y]=C;
  42. if(H[x]==-1) H[x]=L[C]=R[C]=C;
  43. else
  44. {
  45. L[C]=L[H[x]];
  46. R[C]=H[x];
  47. R[L[H[x]]]=C;
  48. L[H[x]]=C;
  49. }
  50. }
  51. void del(int x)
  52. {
  53. R[L[x]]=R[x];
  54. L[R[x]]=L[x];
  55. for(int i=D[x]; i!=x; i=D[i])
  56. {
  57. for(int j=R[i]; j!=i; j=R[j])
  58. {
  59. U[D[j]]=U[j];
  60. D[U[j]]=D[j];
  61. S[Col[j]]--;
  62. }
  63. }
  64. }
  65. void rec(int x)
  66. {
  67. for(int i=U[x]; i!=x; i=U[i])
  68. {
  69. for(int j=L[i]; j!=i; j=L[j])
  70. {
  71. U[D[j]]=j;
  72. D[U[j]]=j;
  73. S[Col[j]]++;
  74. }
  75. }
  76. R[L[x]]=x;
  77. L[R[x]]=x;
  78. }
  79. int dance(int x)
  80. {
  81. if(R[0]==0 || R[0]>16)
  82. {
  83. cnt=x;
  84. return 1;
  85. }
  86. int now=R[0];
  87. for(int i=R[0]; i!=0 && i<=16; i=R[i])
  88. {
  89. if(S[i]<S[now]) now=i;
  90. }
  91. del(now);
  92. for(int i=D[now]; i!=now; i=D[i])
  93. {
  94. ans[x]=Row[i];
  95. for(int j=R[i]; j!=i; j=R[j]) del(Col[j]);
  96. if(dance(x+1)) return 1;
  97. for(int j=L[i]; j!=i; j=L[j]) rec(Col[j]);
  98. }
  99. rec(now);
  100. return 0;
  101. }
  102. } dlx;
  103. struct node
  104. {
  105. int m,id;
  106. int w[44];
  107. } kx[1234];
  108. int main()
  109. {
  110. int n;
  111. while(scanf("%d",&n),n)
  112. {
  113. int cnt=0;
  114. dlx.init(n*16,16+n);
  115. for(int i=1; i<=n; i++)
  116. {
  117. int a,b;
  118. scanf("%d%d",&a,&b);
  119. char v[12][12];
  120. for(int j=0; j<a; j++) scanf("%s",v[j]);
  121. for(int xx=1; xx+a<=5; xx++)
  122. {
  123. for(int yy=1; yy+b<=5; yy++)
  124. {
  125. cnt++;
  126. kx[cnt].m=0;
  127. kx[cnt].id=i;
  128. for(int x=0; x<a; x++)
  129. {
  130. for(int y=0; y<b; y++)
  131. {
  132. if(v[x][y]=='1')
  133. {
  134. int tep=(xx+x-1)*4+(yy+y);
  135. dlx.link(cnt,tep);
  136. kx[cnt].w[kx[cnt].m++]=tep;
  137. }
  138. }
  139. }
  140. dlx.link(cnt,16+i);
  141. }
  142. }
  143. }
  144. int f=dlx.dance(0);
  145. if(f==0) puts("No solution possible");
  146. else
  147. {
  148. char mp[10][10];
  149. for(int i=0;i<dlx.cnt;i++)
  150. {
  151. for(int j=0;j<kx[dlx.ans[i]].m;j++)
  152. {
  153. int x,y;
  154. x=(kx[dlx.ans[i]].w[j]-1)/4;
  155. y=kx[dlx.ans[i]].w[j]%4-1;
  156. if(y==-1) y=3;
  157. mp[x][y]=kx[dlx.ans[i]].id+'0';
  158. }
  159. }
  160. for(int i=0;i<4;i++)
  161. {
  162. mp[i][4]='\0';
  163. puts(mp[i]);
  164. }
  165. }
  166. puts("");
  167. }
  168. return 0;
  169. }

[DLX精确覆盖] hdu 1603 A Puzzling Problem的更多相关文章

  1. [DLX精确覆盖] hdu 3663 Power Stations

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

  2. DLX精确覆盖与重复覆盖模板题

    hihoCoder #1317 : 搜索四·跳舞链 原题地址:http://hihocoder.com/problemset/problem/1317 时间限制:10000ms 单点时限:1000ms ...

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

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

  4. POJ 3076 Sudoku DLX精确覆盖

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

  5. (简单) POJ 3074 Sudoku, DLX+精确覆盖。

    Description In the game of Sudoku, you are given a large 9 × 9 grid divided into smaller 3 × 3 subgr ...

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

  7. poj3074 DLX精确覆盖

    题意:解数独 分析: 完整的数独有四个充要条件: 1.每个格子都有填数字 2.每列都有1~9中的每个数字 3.每行都有1~9中的每个数字 4.每个9宫格都有1~9中的每个数字 可以转化成精确覆盖问题. ...

  8. POJ 3074 Sudoku DLX精确覆盖

    DLX精确覆盖.....模版题 Sudoku Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 8336   Accepted: ...

  9. [DLX精确覆盖+打表] hdu 2518 Dominoes

    题意: 就是给12种图形,旋转,翻折.有多少种方法构成n*m=60的矩形 思路: 裸的精确覆盖.就是建图麻烦 个人太挫,直接手写每一个图形的各种形态 须要注意的是最后的答案须要除以4 代码: #inc ...

随机推荐

  1. 喵哈哈村的魔法考试 Round #12 (Div.2) 题解

    A 注意答案会超过int,考虑分l,r奇数和偶数来考虑即可. #include<bits/stdc++.h> using namespace std; long long l,r; int ...

  2. db2执行计划具体操作

    explain 1.如果第一次执行,请先(在dbinst用户下) connect to dbname,执行db2 -tvf $HOME/sqllib/misc/EXPLAIN.DDL建立执行计划表 2 ...

  3. 解决wsl不能安装z.sh问题

    z.sh是韦大很推崇的类似autojump的bash插件,能够很方便的寻找目录,然而wsl下不能直接使用,解决方法在其github仓库(z)的issue中找到: Reproduce it at Mic ...

  4. print2flash文档在线预览应用(java,.net)

    一.背景 前段时间,LZ的boss突然给了出了这样一个需求:将原项目中的所有文章关联的附件TXT.PDF.office相关文件全部以flash的形式在网页上进行展示,便于预览.看似简单的需求,整个研发 ...

  5. 吴伯凡:VUCA时代的自我迭代

    吴伯凡:VUCA时代的自我迭代 https://mp.weixin.qq.com/s?src=3&timestamp=1506588223&ver=1&signature=nv ...

  6. C#编程(八十一)---------- 捕获异常

    捕获异常 前面主要说了关于异常的一些基础和理论知识,没有进入到正真的异常案例,这一讲通过几个案例来描述一下异常的捕获和处理. 案例代码: using System; using System.Coll ...

  7. 这可能是最好的RxJava 2.x 入门教程(一)

    这可能是最好的 RxJava 2.x 入门教程系列专栏 文章链接: 这可能是最好的 RxJava 2.x 入门教程(完结版)[重磅推出] 这可能是最好的RxJava 2.x 入门教程(一) 这可能是最 ...

  8. Apache Kafka学习 (二) - 多代理(broker)集群

    1. 配置server.properties > cp config/server.properties config/server-1.properties> cp config/ser ...

  9. 微软BI 之SSAS 系列 - 多维数据集维度用法之三 多对多维度 Many to Many

    开篇介绍 对于维度成员和事实数据直接的关系看到更多的可能还是一对一,一对多的关系.比方在事实维度(或退化维度)中一个订单和明细号组合而成的ID,对应的就是事实表中的一条数据,这就是一对一的关系.比方说 ...

  10. vim Google style format

    https://github.com/google/vim-codefmt https://github.com/rhysd/vim-clang-format http://pre.tir.tw/00 ...