[ZOJ2669]Lattice Animals


Time Limit: 5 Seconds      Memory Limit: 32768 KB

Lattice animal is a set of connected sites on a lattice. Lattice animals on a square lattice are especially popular subject of study and are also known as polyminoes. Polymino is usually represented as a set of sidewise connected squares. Polymino with n squares is called n-polymino.

In this problem you are to find a number of distinct free n-polyminoes that fit into rectangle w * h. Free polyminoes can be rotated and flipped over, so that their rotations and mirror images are considered to be the same.

For example, there are 5 different pentaminoes (5-polyminoes) that fit into 2 * 4 rectangle and 3 different octominoes (8-polyminoes) that fit into 3 * 3 rectangle.

Input

There are several test cases in the input. Each case consists of a single line with 3 integer numbers n, w, and h (n ≤ 10, 1 ≤ w, h ≤ n).

Output

Write to the output file a single integer number --- the number of distinct free n-polyminoes that fit into rectangle w * h.

Sample Input

5 1 4
5 2 4
5 3 4
5 5 5
8 3 3

Sample Output

0
5
11
12
3


Source: Northeastern Europe 2004

这是NEERC2004的题目,好像有两个人A……

ZOJ的测评机跑的貌似比较快,交其它OJ全都是TLE,网上部分标程也是。我叫UVA上RE不知道为何……

题目大意就是生成N连块,N<=10,然后有一堆询问,每次询问N连块中,有W*H的棋盘可以放下多少个N连块。

先将N连块求出来,然后再将每个N连块的贡献记入答案。时间限制虽然是5秒,但是应该要掌握预处理在2秒以内才可以(反正2.1秒的我T了)

ZOJ上690MS,orz 0ms秒过的dalao……

众多标程都是与刘汝佳一样用的Set,跑起来貌似能比一样思想的程序快1秒(亲测)

试题分析:大暴力:直接枚举下一个块可以放在哪里,拓展即可。

         很容易想到一个优化:用N-1连块的答案来更新N连块的答案(显而易见不会漏掉答案)

         当然,加上旋转翻转等一类Set的简易操作,貌似就可以了。

         但我并没有写Set(不会很无奈啊,从来不用),然后就引来了众多莫名其妙的优化。

         Part1(优化3秒左右):Hash

            Hash优化是最先想到的一类优化,两个块一样必定Hash值相等,因此搞了两个Hash,其实一个Hash足矣。实测时间差不多。

         Part2(Hash优化后优化200ms左右):W,H

            注意到从N-1连块到N连块的长度或者高度最多其中一项+1.

            这时我们就可以只翻转+旋转W,H这一块,其余并不用翻转,Hash、判断相等也只用这一块就好了。

            现在就自然而然就多了一个剪枝:当两个块的W,H其一不等时,这两个块一定不相等。

         Part3(上两项优化完后优化1s左右):Del

            Del是去0操作,旨在去掉上面和左边的0,保证图形在10*10的棋盘的左上角。

               发现Del多了,删去后就优化1s左右。

      还有一些优化想出来了但没有用,可能不会优化太多:

         将每个N连块每行每列的有几个块都求出来,然后比完Hash与W,H后比这两个信息是否一样,不一样则退出。

         但这并不能完全确定一个联通块,比如:

         110  101

         111  111

         001  010

         这样列是:2 2 2      2 2 2

           行是:2 3 1      2 3 1

         一开始就是写完了这个发现不行,然后删了想了想又写了一天上面的东西。。。

     个人认为题还是挺不错的,值得一做,但要做好心理准备……

代码:

  1. #include<iostream>
  2. #include<cstring>
  3. #include<vector>
  4. #include<queue>
  5. #include<algorithm>
  6. using namespace std;
  7.  
  8. #define LL long long
  9.  
  10. inline int read(){
  11. int x=0,f=1;char c=getchar();
  12. for(;!isdigit(c);c=getchar()) if(c=='-') f=-1;
  13. for(;isdigit(c);c=getchar()) x=x*10+c-'0';
  14. return x*f;
  15. }
  16. const int INF=9999999;
  17. const int MAXN=100000;
  18. const int T=10;
  19. int N,M,K;
  20. int Maxd;
  21. bool vis[21][21];
  22. bool vist[21][21];
  23. int W1,H1;
  24. int dis[5][2]={{0,1},{1,0},{0,-1},{-1,0}};
  25. bool txt[21][21];
  26.  
  27. struct data{
  28. int w,h;
  29. bool mp[21][21];
  30. int dit[21];long long dit2[21];
  31. long long Hash1,Hash2;
  32. };
  33. vector<data> vec[21];
  34.  
  35. void del0(){
  36. int move=INF;
  37. for(int i=0;i<T;i++){
  38. int cnt=-1;
  39. while(!txt[i][cnt]) cnt++;
  40. move=min(cnt-1,move);
  41. }
  42. if(move>=0){
  43. for(int i=0;i<T;i++){
  44. for(int j=move+1;j<T;j++){
  45. txt[i][j-move-1]=txt[i][j];
  46. txt[i][j]=0;
  47. }
  48. }
  49. }
  50. move=INF;
  51. for(int j=0;j<T;j++){
  52. int cnt=-1;
  53. while(!txt[cnt][j]) cnt++;
  54. move=min(cnt-1,move);
  55. }
  56. if(move>=0){
  57. for(int i=move+1;i<T;i++){
  58. for(int j=0;j<T;j++){
  59. txt[i-move-1][j]=txt[i][j];
  60. txt[i][j]=0;
  61. }
  62. }
  63. }
  64. return ;
  65. }
  66. bool vis2[21][21];
  67. int ans[21][21][21];
  68. int W,H;
  69. int dig[21];long long dig2[21];
  70. void rota(){
  71. memset(vis2,0,sizeof(vis));
  72. for(int i=0;i<H1;i++)
  73. for(int j=0;j<W1;j++) vis2[i][j]=txt[i][j];
  74. memset(txt,0,sizeof(txt));
  75. for(int i=0;i<H1;i++){
  76. for(int j=0;j<W1;j++)
  77. txt[j][i]=vis2[i][W1-j-1];
  78. }
  79. swap(W1,H1);
  80. return ;
  81. }
  82. long long Has;
  83. bool judge(){
  84. int t=vec[Maxd].size();
  85. if(!t) return true;
  86. for(int i=0;i<5;i++){
  87. if(i) rota();
  88. Has=0;
  89. for(int a=0;a<H1;a++){
  90. long long cnt=0;
  91. for(int b=0;b<W1;b++){
  92. if(txt[a][b]) cnt+=((1<<b)*a);
  93. }
  94. Has+=cnt;
  95. dig[a]=cnt;
  96. }
  97. long long Has2=0;
  98. for(int a=0;a<H1;a++){
  99. long long cnt=0;
  100. for(int b=0;b<W1;b++){
  101. if(txt[a][b]) cnt+=(1<<(a*b)+a*b)%999997;
  102. }
  103. dig2[a]=cnt;
  104. Has2+=cnt;
  105. Has2%=999999997;
  106. }
  107. for(int j=0;j<t;j++){
  108. if(Has!=vec[Maxd][j].Hash1) continue;
  109. if(Has2!=vec[Maxd][j].Hash2) continue;
  110. if(W1!=vec[Maxd][j].w||H1!=vec[Maxd][j].h) continue;
  111. bool flag=true;
  112. for(int a=0;a<H1;a++)
  113. if(dig[a]!=vec[Maxd][j].dit[a]) {
  114. flag=false;break;
  115. }
  116. if(!flag) continue;
  117. for(int a=0;a<H1;a++)
  118. if(dig2[a]!=vec[Maxd][j].dit2[a]) {
  119. flag=false;break;
  120. }
  121. if(!flag) continue;
  122. for(int a=0;a<H1;a++){
  123. for(int b=0;b<W1;b++){
  124. if(vec[Maxd][j].mp[a][b]!=txt[a][b]){
  125. flag=false;
  126. break;
  127. }
  128. }
  129. if(!flag) break;
  130. }
  131. if(flag) return false;
  132. }
  133. }
  134. return true;
  135. }
  136. void pushin(){
  137. data tk;
  138. long long Has2=0;
  139. for(int i=0;i<T;i++)
  140. for(int j=0;j<T;j++)
  141. tk.mp[i][j]=txt[i][j];
  142. for(int a=0;a<H1;a++){
  143. long long cnt=0;
  144. for(int b=0;b<W1;b++){
  145. if(txt[a][b]) cnt+=((1<<b)*a);
  146. }
  147. tk.dit[a]=cnt;
  148. Has2+=cnt;
  149. }
  150. tk.Hash1=Has2;
  151. Has2=0;
  152. for(int a=0;a<H1;a++){
  153. long long cnt=0;
  154. for(int b=0;b<W1;b++){
  155. if(txt[a][b]) cnt+=(1<<(a*b)+a*b)%999997;
  156. }
  157. tk.dit2[a]=cnt;
  158. Has2+=cnt;
  159. Has2%=999999997;
  160. }
  161. tk.Hash2=Has2;
  162. tk.w=W1;
  163. tk.h=H1;
  164. vec[Maxd].push_back(tk);
  165. return ;
  166. }
  167. bool ti[21][21];
  168.  
  169. void GA(int d){
  170. for(int a=1;a<=H+1;a++)
  171. for(int b=1;b<=W+1;b++){
  172. if(!vis[a][b]) continue;
  173. for(int k=0;k<4;k++){
  174. int xx=dis[k][0]+a;
  175. int yy=dis[k][1]+b;
  176. if(xx>=T||yy>=T) continue;
  177. if(ti[xx][yy]) continue;
  178. if(vis[xx][yy]) continue;
  179. ti[xx][yy]=true;
  180. vis[xx][yy]=1;
  181. for(int i=0;i<T;i++)
  182. for(int j=0;j<T;j++) txt[i][j]=vis[i][j];
  183. del0();
  184. int h=0;
  185. for(int i=0;i<T;i++){
  186. int p=T-1;
  187. while(!txt[i][p]) p--;
  188. h=max(h,p+1);
  189. }
  190. W1=h;
  191. int w=0;
  192. for(int j=0;j<T;j++){
  193. int p=T-1;
  194. while(!txt[p][j]) p--;
  195. w=max(w,p+1);
  196. }
  197. H1=w;
  198. vis[xx][yy]=0;
  199. if(!judge()) continue;
  200. rota();
  201. for(int i=0;i<H1/2;i++)
  202. for(int j=0;j<W1;j++)
  203. swap(txt[i][j],txt[H1-i-1][j]);
  204. if(!judge()) continue;
  205. pushin();
  206. }
  207. }
  208. return ;
  209. }
  210. void pre(){
  211. txt[0][0]=1;
  212. Maxd=1;
  213. pushin();
  214. txt[0][0]=0;
  215. for(Maxd=2;Maxd<=T;Maxd++) {
  216. for(int i=0;i<vec[Maxd-1].size();i++){
  217. W=vec[Maxd-1][i].w;H=vec[Maxd-1][i].h;
  218. memset(vis,0,sizeof(vis));
  219. for(int j=0;j<T;j++)
  220. for(int k=0;k<T;k++)
  221. vis[j][k]=vec[Maxd-1][i].mp[j][k];
  222. for(int k=T-1;k>0;k--)
  223. for(int j=0;j<T;j++)
  224. vis[k][j]=vis[k-1][j],vis[k-1][j]=0;
  225. for(int k=0;k<T;k++)
  226. for(int j=T-1;j>0;j--)
  227. vis[k][j]=vis[k][j-1],vis[k][j-1]=0;
  228. memset(ti,0,sizeof(ti));
  229. GA(1);
  230. }
  231. for(int i=0;i<vec[Maxd].size();i++){
  232. for(int j=1;j<=10;j++)
  233. for(int k=1;k<=10;k++){
  234. if((vec[Maxd][i].w<=k&&vec[Maxd][i].h<=j)||(vec[Maxd][i].w<=j&&vec[Maxd][i].h<=k)) ans[Maxd][j][k]++;
  235. }
  236. }
  237. }
  238. }
  239.  
  240. int main(){
  241. pre();
  242. while(scanf("%d%d%d",&N,&M,&K)!=EOF){
  243. if(N==1) puts("1");
  244. else printf("%d\n",ans[N][M][K]);
  245. }
  246. return 0;
  247. }

【DFS】【打表】Lattice Animals的更多相关文章

  1. DFS+打表

    N皇后问题 Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Submit Status ...

  2. HDU 2586 How far away(dfs+邻接表)

    How far away [题目链接]How far away [题目类型]dfs+邻接表 &题意: 题目大意:一个村子里有n个房子,这n个房子用n-1条路连接起来,接下了有m次询问,每次询问 ...

  3. HDU 2563 统计问题 (DFS + 打表)

    统计问题 Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submi ...

  4. UVA-1602 Lattice Animals 搜索问题(打表+set)

    题目链接 https://vjudge.net/problem/UVA-1602 紫书的一道例题,跟之前的很多题目有很多不同. 本题不像是一般的dfs或bfs这样的搜索套路,而是另一种枚举思路. 题意 ...

  5. ACM: HDU 2563 统计问题-DFS+打表

    HDU 2563 统计问题 Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u HDU 2 ...

  6. codeforces 285 D. Permutation Sum 状压 dfs打表

    题意: 如果有2个排列a,b,定义序列c为: c[i] = (a[i] + b[i] - 2) % n + 1 但是,明显c不一定是一个排列 现在,给出排列的长度n (1 <= n <= ...

  7. hdu 2510 符号三角形 (DFS+打表)

    符号三角形 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submi ...

  8. hdu2510-符号三角形(dfs+打表)

    n只有24 可以写个暴力搜索,然后打表,不然这个很难通过剪枝直接优化到1s以内. #include<bits/stdc++.h> #define inf 0x3f3f3f3f ; usin ...

  9. hdu 1848 sg——dfs&&打表双实现

    Fibonacci again and again Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Jav ...

随机推荐

  1. 2009 Round2 A Crazy Rows (模拟)

    Problem You are given an N x N matrix with 0 and 1 values. You can swap any two adjacent rows of the ...

  2. C++获取系统时间的方法

    //首先是了解这个结构体,_SYSTEMTIME ,然后通过系统函数GetLocalTime往这个结构体的变量中写入当前系统时间typedef struct _SYSTEMTIME { WORD wY ...

  3. Angular2.0 基础: User Input

    1.Angular 2.0 中的变量 对输入值的获取,我们可以通过$event 来获取,也可以通过变量来获取. template: ` <input (keyup)="onKey($e ...

  4. 【Python学习笔记】有关包的基本知识

    python的包(package)是一个有层次的文件目录结构.它定义了一个由模块和子包组成的Python应用程序执行环境. AAA/ __init__.py bbb.py CCC/ __init__. ...

  5. 自动化测试===Macaca环境搭建,自我总结

    安装jdk 安装安卓sdk(打开sdk的时候出现问题linux===启动sdk manager下载配置sdk的时候报错的解决办法) 安装gradle,配置环境变量(MACACA===gradle下载和 ...

  6. selenium.webdriver.common.keys 模块中常用的变量

    表11-5 selenium.webdriver.common.keys 模块中常用的变量属性 含义Keys.DOWN, Keys.UP, Keys.LEFT,Keys.RIGHT 键盘箭头键Keys ...

  7. python按比例随机切分数据

    在机器学习或者深度学习中,我们常常碰到一个问题是数据集的切分.比如在一个比赛中,举办方给我们的只是一个带标注的训练集和不带标注的测试集.其中训练集是用于训练,而测试集用于已训练模型上跑出一个结果,然后 ...

  8. canvas制作柱形图/折线图/饼状图,Konva写动态饼状图

    制作饼状图 <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF ...

  9. MYSQL5.5源码安装 linux下

    /* 首先安装必要的库 */ yum -y install gcc* ###### 安装 MYSQL ###### 首先安装camke 一.支持YUM,则 yum install -y cmake 二 ...

  10. DRF的@action装饰器

    # 转自:http://www.cnblogs.com/zhzhlong/p/9325180.html 视图集中附加action的声明 from rest_framework.decorators i ...