打模板的经验:

1.变量名取一样,换行也一样,不要宏定义

2.大小写,少写,大括号

  1. #include<algorithm>
  2. #include<iostream>
  3. #include<stdlib.h>
  4. #include<string.h>
  5. #include<math.h>
  6. #include<stdio.h>
  7. #include<vector>
  8. #include<queue>
  9. #include<string>
  10. #include<ctime>
  11. #include<stack>
  12. #include<map>
  13. #include<set>
  14. #include<list>
  15. using namespace std;
  16. #define rep(i,j,k) for(int i = (int)j;i <= (int)k;i ++)
  17. #define per(i,j,k) for(int i = (int)j;i >= (int)k;i --)
  18. #define debug(x) cerr<<#x<<" = "<<(x)<<endl
  19. #define mmm(a,b) memset(a,b,sizeof(a))
  20. #define pb push_back
  21. //#define x first
  22. //#define y second
  23.  
  24. typedef double db;
  25. typedef long long ll;
  26.  
  27. const int MAXD = 15;
  28. const int STATE = 1e6 + 5;
  29. const int HASH = 3e5 + 7;
  30. const int MOD = 1e9 + 7;
  31. int N, M, K;
  32. int maze[MAXD][MAXD];
  33. int code[MAXD];
  34. int ch[MAXD];
  35. int num;
  36. struct HASHMAP
  37. {
  38. int head[HASH], next[STATE], size;
  39. ll state[STATE];
  40. int f[STATE];
  41. void init()
  42. {
  43. size = 0;
  44. mmm(head, -1);
  45. }
  46. void push(ll st, int ans)
  47. {
  48. int i;
  49. int h = st % HASH;
  50. for (i = head[h]; i != -1; i = next[i])
  51. if (state[i] == st)
  52. {
  53. f[i] += ans;
  54. f[i] %= MOD;
  55. return;
  56. }
  57. state[size] = st;
  58. f[size] = ans;
  59. next[size] = head[h];
  60. head[h] = size++;
  61. }
  62. }hm[2];
  63. void decode(int *code, int m, long long st)
  64. {
  65. num = st & 63;
  66. st >>= 6;
  67. for (int i = m; i >= 0; i--)
  68. {
  69. code[i] = st & 7;
  70. st >>= 3;
  71. }
  72. }
  73. ll encode(int *code, int m)
  74. {
  75. int cnt = 1;
  76. mmm(ch, -1);
  77. ch[0] = 0;
  78. ll st = 0;
  79. rep(i, 0, m)
  80. {
  81. if (ch[code[i]] == -1)ch[code[i]] = cnt++;
  82. code[i] = ch[code[i]];
  83. st <<= 3;
  84. st |= code[i];
  85. }
  86. st <<= 6;
  87. st |= num;
  88. return st;
  89. }
  90. void shift(int *code, int m)
  91. {
  92. for (int i = m; i > 0; i--)code[i] = code[i - 1];
  93. code[0] = 0;
  94. }
  95. void dpblank(int i, int j, int cur)
  96. {
  97. int k, left, up;
  98. for (k = 0; k < hm[cur].size; k++)
  99. {
  100. decode(code, M, hm[cur].state[k]);
  101. left = code[j - 1];
  102. up = code[j];
  103. if (left&&up)
  104. {
  105. if (left == up)
  106. {
  107. if (num >= K)continue;
  108. int t = 0;
  109.  
  110. for (int p = 0; p < j - 1; p++)
  111. if (code[p])t++;
  112. if (t & 1)continue;
  113. if (num < K)
  114. {
  115. num++;
  116. code[j - 1] = code[j] = 0;
  117. hm[cur ^ 1].push(encode(code, j == M ? M - 1 : M), hm[cur].f[k]);
  118. }
  119. }
  120. else
  121. {
  122. code[j - 1] = code[j] = 0;
  123. for (int t = 0; t <= M; t++)
  124. if (code[t] == up)
  125. code[t] = left;
  126. hm[cur ^ 1].push(encode(code, j == M ? M - 1 : M), hm[cur].f[k]);
  127. }
  128. }
  129. else if (left || up)
  130. {
  131. int t;
  132. if (left) t = left;
  133. else t = up;
  134. if (maze[i][j + 1])
  135. {
  136. code[j - 1] = 0;
  137. code[j] = t;
  138. hm[cur ^ 1].push(encode(code, M), hm[cur].f[k]);
  139. }
  140. if (maze[i + 1][j])
  141. {
  142. code[j] = 0;
  143. code[j - 1] = t;
  144. hm[cur ^ 1].push(encode(code, j == M ? M - 1 : M), hm[cur].f[k]);
  145. }
  146. }
  147. else
  148. {
  149. if (maze[i][j + 1] && maze[i + 1][j])
  150. {
  151. code[j - 1] = code[j] = 13;
  152. hm[cur ^ 1].push(encode(code, j == M ? M - 1 : M), hm[cur].f[k]);
  153. }
  154. }
  155. }
  156. }
  157. void dplock(int i, int j, int cur)
  158. {
  159. int k;
  160. for (k = 0; k < hm[cur].size; k++)
  161. {
  162. decode(code, M, hm[cur].state[k]);
  163. code[j - 1] = code[j] = 0;
  164. hm[cur ^ 1].push(encode(code, j == M ? M - 1 : M), hm[cur].f[k]);
  165. }
  166. }
  167. char str[20];
  168. void init()
  169. {
  170. cin >> N >> M >> K;
  171. mmm(maze, 0);
  172. rep(i, 1, N)
  173. {
  174. scanf("%s", &str);
  175. for (int j = 1; j <= M; j++)
  176. if (str[j - 1] == '.')
  177. maze[i][j] = 1;
  178. }
  179. }
  180. void solve()
  181. {
  182. int cur = 0;
  183. hm[cur].init();
  184. hm[cur].push(0, 1);
  185. rep(i, 1, N)
  186. rep(j, 1, M)
  187. {
  188. hm[cur ^ 1].init();
  189. if (maze[i][j])dpblank(i, j, cur);
  190. else dplock(i, j, cur);
  191. cur ^= 1;
  192. }
  193. int ans = 0;
  194. for (int i = 0; i < hm[cur].size; i++)
  195. if (hm[cur].state[i] == K)
  196. {
  197. ans += hm[cur].f[i];
  198. ans %= MOD;
  199. }
  200. printf("%d\n", ans);
  201.  
  202. }
  203. int main()
  204. {
  205. int t;
  206. cin >> t;
  207. while (t--)
  208. {
  209. init();
  210. solve();
  211. }
  212. //cin >> t;
  213. }
  214. /*
  215. 2
  216. 4 4 1
  217. **..
  218. ....
  219. ....
  220. ....
  221. 4 1
  222. ....
  223. ....
  224. ....
  225. ....
  226. */
  1. /*
  2. HDU 4285
  3. 要形成刚好K条回路的方法数
  4. 要避免环套环的情况。
  5. 所以形成回路时,要保证两边的插头数是偶数
  6.  
  7. G++ 11265ms 11820K
  8. C++ 10656ms 11764K
  9.  
  10. */
  11.  
  12. #include<stdio.h>
  13. #include<iostream>
  14. #include<algorithm>
  15. #include<string.h>
  16. using namespace std;
  17.  
  18. const int MAXD=;
  19. const int STATE=;
  20. const int HASH=;//这个大一点可以防止TLE,但是容易MLE
  21. const int MOD=;
  22.  
  23. int N,M,K;
  24. int maze[MAXD][MAXD];
  25. int code[MAXD];
  26. int ch[MAXD];
  27. int num;//圈的个数
  28. struct HASHMAP
  29. {
  30. int head[HASH],next[STATE],size;
  31. long long state[STATE];
  32. int f[STATE];
  33. void init()
  34. {
  35. size=;
  36. memset(head,-,sizeof(head));
  37. }
  38. void push(long long st,int ans)
  39. {
  40. int i;
  41. int h=st%HASH;
  42. for(i=head[h];i!=-;i=next[i])
  43. if(state[i]==st)
  44. {
  45. f[i]+=ans;
  46. f[i]%=MOD;
  47. return;
  48. }
  49. state[size]=st;
  50. f[size]=ans;
  51. next[size]=head[h];
  52. head[h]=size++;
  53. }
  54. }hm[];
  55. void decode(int *code,int m,long long st)
  56. {
  57. num=st&;
  58. st>>=;
  59. for(int i=m;i>=;i--)
  60. {
  61. code[i]=st&;
  62. st>>=;
  63. }
  64. }
  65. long long encode(int *code,int m)//最小表示法
  66. {
  67. int cnt=;
  68. memset(ch,-,sizeof(ch));
  69. ch[]=;
  70. long long st=;
  71. for(int i=;i<=m;i++)
  72. {
  73. if(ch[code[i]]==-)ch[code[i]]=cnt++;
  74. code[i]=ch[code[i]];
  75. st<<=;
  76. st|=code[i];
  77. }
  78. st<<=;
  79. st|=num;
  80. return st;
  81. }
  82. void shift(int *code,int m)
  83. {
  84. for(int i=m;i>;i--)code[i]=code[i-];
  85. code[]=;
  86. }
  87. void dpblank(int i,int j,int cur)
  88. {
  89. int k,left,up;
  90. for(k=;k<hm[cur].size;k++)
  91. {
  92. decode(code,M,hm[cur].state[k]);
  93. left=code[j-];
  94. up=code[j];
  95. if(left&&up)
  96. {
  97. if(left==up)
  98. {
  99. if(num>=K)continue;
  100. int t=;
  101. //要避免环套环的情况,需要两边插头数为偶数
  102. for(int p=;p<j-;p++)
  103. if(code[p])t++;
  104. if(t&)continue;
  105. if(num<K)
  106. {
  107. num++;
  108. code[j-]=code[j]=;
  109. hm[cur^].push(encode(code,j==M?M-:M),hm[cur].f[k]);
  110. }
  111. }
  112. else
  113. {
  114. code[j-]=code[j]=;
  115. for(int t=;t<=M;t++)
  116. if(code[t]==up)
  117. code[t]=left;
  118. hm[cur^].push(encode(code,j==M?M-:M),hm[cur].f[k]);
  119. }
  120. }
  121. else if(left||up)
  122. {
  123. int t;
  124. if(left)t=left;
  125. else t=up;
  126. if(maze[i][j+])
  127. {
  128. code[j-]=;
  129. code[j]=t;
  130. hm[cur^].push(encode(code,M),hm[cur].f[k]);
  131. }
  132. if(maze[i+][j])
  133. {
  134. code[j]=;
  135. code[j-]=t;
  136. hm[cur^].push(encode(code,j==M?M-:M),hm[cur].f[k]);
  137. }
  138. }
  139. else
  140. {
  141. if(maze[i][j+]&&maze[i+][j])
  142. {
  143. code[j-]=code[j]=;
  144. hm[cur^].push(encode(code,j==M?M-:M),hm[cur].f[k]);
  145. }
  146. }
  147. }
  148. }
  149. void dpblock(int i,int j,int cur)
  150. {
  151. int k;
  152. for(k=;k<hm[cur].size;k++)
  153. {
  154. decode(code,M,hm[cur].state[k]);
  155. code[j-]=code[j]=;
  156. hm[cur^].push(encode(code,j==M?M-:M),hm[cur].f[k]);
  157. }
  158. }
  159. char str[];
  160. void init()
  161. {
  162. scanf("%d%d%d",&N,&M,&K);
  163. memset(maze,,sizeof(maze));
  164. for(int i=;i<=N;i++)
  165. {
  166. scanf("%s",&str);
  167. for(int j=;j<=M;j++)
  168. if(str[j-]=='.')
  169. maze[i][j]=;
  170. }
  171. }
  172. void solve()
  173. {
  174. int i,j,cur=;
  175. hm[cur].init();
  176. hm[cur].push(,);
  177. for(i=;i<=N;i++)
  178. for(j=;j<=M;j++)
  179. {
  180. hm[cur^].init();
  181. if(maze[i][j])dpblank(i,j,cur);
  182. else dpblock(i,j,cur);
  183. cur^=;
  184. }
  185. int ans=;
  186. for(i=;i<hm[cur].size;i++)
  187. if(hm[cur].state[i]==K)
  188. {
  189. ans+=hm[cur].f[i];
  190. ans%=MOD;
  191. }
  192. printf("%d\n",ans);
  193.  
  194. }
  195. int main()
  196. {
  197. //freopen("in.txt","r",stdin);
  198. //freopen("out.txt","w",stdout);
  199. int T;
  200. scanf("%d",&T);
  201. while(T--)
  202. {
  203. init();
  204. solve();
  205. }
  206. return ;
  207. }
  208.  
  209. /*
  210. Sample Input
  211. 4 4 1
  212. **..
  213. ....
  214. ....
  215. ....
  216. 4 1
  217. ....
  218. ....
  219. ....
  220. ....
  221.  
  222. Sample Output
  223. 6
  224.  
  225. */

#include<algorithm>#include<iostream>#include<stdlib.h>#include<string.h>#include<math.h>#include<stdio.h>#include<vector>#include<queue>#include<string>#include<ctime>#include<stack>#include<map>#include<set>#include<list>using namespace std;#define rep(i,j,k) for(int i = (int)j;i <= (int)k;i ++)#define per(i,j,k) for(int i = (int)j;i >= (int)k;i --)#define debug(x) cerr<<#x<<" = "<<(x)<<endl#define mmm(a,b) memset(a,b,sizeof(a))#define pb push_back//#define x first//#define y second
typedef double db;typedef long long ll;

const int MAXD = 15;const int STATE = 1e6 + 5;const int HASH = 3e5 + 7;const int MOD = 1e9 + 7;int N, M, K;int maze[MAXD][MAXD];int code[MAXD];int ch[MAXD];int num;struct HASHMAP{int head[HASH], next[STATE], size;ll state[STATE];int f[STATE];void init(){size = 0;mmm(head, -1);}void push(ll st, int ans){int i;int h = st % HASH;for (i = head[h]; i != -1; i = next[i])if (state[i] == st){f[i] += ans;f[i] %= MOD;return;}state[size] = st;f[size] = ans;next[size] = head[h];head[h] = size++;}}hm[2];void decode(int *code, int m, long long st){num = st & 63;st >>= 6;for (int i = m; i >= 0; i--){code[i] = st & 7;st >>= 3;}}ll encode(int *code, int m){int cnt = 1;mmm(ch, -1);ch[0] = 0;ll st = 0;rep(i, 0, m){if (ch[code[i]] == -1)ch[code[i]] = cnt++;code[i] = ch[code[i]];st <<= 3;st |= code[i];}st <<= 6;st |= num;return st;}void shift(int *code, int m){for (int i = m; i > 0; i--)code[i] = code[i - 1];code[0] = 0;}void dpblank(int i, int j, int cur){int k, left, up;for (k = 0; k < hm[cur].size; k++){decode(code, M, hm[cur].state[k]);left = code[j - 1];up = code[j];if (left&&up){if (left == up){if (num >= K)continue;int t = 0;
for (int p = 0; p < j - 1; p++)if (code[p])t++;if (t & 1)continue;if (num < K){num++;code[j - 1] = code[j] = 0;hm[cur ^ 1].push(encode(code, j == M ? M - 1 : M), hm[cur].f[k]);}}else{code[j - 1] = code[j] = 0;for (int t = 0; t <= M; t++)if (code[t] == up)code[t] = left;hm[cur ^ 1].push(encode(code, j == M ? M - 1 : M), hm[cur].f[k]);}}else if (left || up){int t;if (left) t = left;else t = up;if (maze[i][j + 1]){code[j - 1] = 0;code[j] = t;hm[cur ^ 1].push(encode(code, M), hm[cur].f[k]);}if (maze[i + 1][j]){code[j] = 0;code[j - 1] = t;hm[cur ^ 1].push(encode(code, j == M ? M - 1 : M), hm[cur].f[k]);}}else{if (maze[i][j + 1] && maze[i + 1][j]){code[j - 1] = code[j] = 13;hm[cur ^ 1].push(encode(code, j == M ? M - 1 : M), hm[cur].f[k]);}}}}void dplock(int i, int j, int cur){int k;for (k = 0; k < hm[cur].size; k++){decode(code, M, hm[cur].state[k]);code[j - 1] = code[j] = 0;hm[cur ^ 1].push(encode(code, j == M ? M - 1 : M), hm[cur].f[k]);}}char str[20];void init(){cin >> N >> M >> K;mmm(maze, 0);rep(i, 1, N){scanf("%s", &str);for (int j = 1; j <= M; j++)if (str[j - 1] == '.')maze[i][j] = 1;}}void solve(){int  cur = 0;hm[cur].init();hm[cur].push(0, 1);rep(i, 1, N)rep(j, 1, M){hm[cur ^ 1].init();if (maze[i][j])dpblank(i, j, cur);else dplock(i, j, cur);cur ^= 1;}int ans = 0;for (int i = 0; i < hm[cur].size; i++)if (hm[cur].state[i] == K){ans += hm[cur].f[i];ans %= MOD;}printf("%d\n", ans);
}int main(){int t;cin >> t;while (t--){init();solve();}//cin >> t;}/*24 4 1**..............4 1................*/

【插头dp】 hdu4285 找bug的更多相关文章

  1. bzoj 1187: [HNOI2007]神奇游乐园 插头dp

    1187: [HNOI2007]神奇游乐园 Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 668  Solved: 337[Submit][Statu ...

  2. 动态规划之插头DP入门

    基于联通性的状态压缩动态规划是一类非常典型的状态压缩动态规划问题,由于其压缩的本质并不像是普通的状态压缩动态规划那样用0或者1来表示未使用.使用两种状态,而是使用数字来表示类似插头的状态,因此.它又被 ...

  3. 初探 插头DP

    因为这题,气得我火冒三丈! 这数据是不是有问题啊!我用cin代替scanf后居然就AC了(本来一直卡在Test 18)!导致我调(对)试(排)了一个小时!! UPD:后来细细想想,会不会是因为scan ...

  4. [入门向选讲] 插头DP:从零概念到入门 (例题:HDU1693 COGS1283 BZOJ2310 BZOJ2331)

    转载请注明原文地址:http://www.cnblogs.com/LadyLex/p/7326874.html 最近搞了一下插头DP的基础知识……这真的是一种很锻炼人的题型…… 每一道题的状态都不一样 ...

  5. bzoj 1210 [HNOI2004] 邮递员 插头dp

    插头dp板子题?? 搞了我一晚上,还tm全是抄的标程.. 还有高精,哈希混入,还是我比较弱,orz各种dalao 有不明白的可以去看原论文.. #include<cstdio> #incl ...

  6. [HNOI2007]神奇游乐园(插头DP)

    题意:n*m的矩阵内值有正有负,找一个四连通的简单环(长度>=4),使得环上值的和最大. 题解:看到2<=m<=6和简单环,很容易想到插头DP,设f[i][j][k]表示轮廓线为第i ...

  7. BZOJ.1210.[HNOI2004]邮递员(插头DP Hash 高精)

    BZOJ 洛谷 http://www.cnblogs.com/LadyLex/p/7326874.html 插头DP.\(m+1\)个插头的状态需要用三进制表示:\(0\)表示无插头,\(1\)表示是 ...

  8. bzoj 1814 Ural 1519 Formula 1 ——插头DP

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=1814 普通的插头 DP .但是调了很久.注意如果合并两个 1 的话,不是 “把向右第一个 2 ...

  9. LG5056 【模板】插头dp

    题意 题目背景 ural 1519 陈丹琦<基于连通性状态压缩的动态规划问题>中的例题 题目描述 给出n*m的方格,有些格子不能铺线,其它格子必须铺,形成一个闭合回路.问有多少种铺法? 输 ...

随机推荐

  1. 【SqlServer】解析SqlServer中的事务

    目录结构: contents structure [+] 事务是什么 控制事务 数据并发访问产生的影响 事务的隔离级别 锁 NOLOCK.HOLDLOCK.UPDLOCK 死锁分析 在这篇Blog中, ...

  2. ES 插入十万条数据耗时1573秒

  3. JAVA代码实现多级树结构封装对象

    树结构在开发中经常遇到.例如:部门.菜单.员工架构等等.下面用部门作为例子构造部门结构树 1.部门表:dept -- ---------------------------- -- Table str ...

  4. 【Tomcat】Tomcat 系统架构与设计模式,第 2 部分: 设计模式分析

    这个分为两个部分的系列文章研究了 Apache Tomcat 服务器的系统架构以及其运用的很多经典设计模式.第 1 部分 分析了 Tomcat 的工作原理,第 2 部分将分析 Tomcat 中运用的许 ...

  5. (5) 电商场景下的常见业务SQL处理

    1. 如何对评论进行分页展示 一般情况下都是这样写 SELECT customer_id,title,content FROM product_comment WHERE audit_status = ...

  6. Asp.Net 隐藏手机号中间四位为*方法

    需求:15088881234 > 150****1234 方法1: "; , ) + , ); 方法2: "; string p2= Regex.Replace(phone ...

  7. 【iCore4 双核心板_ARM】例程十九:USBD_MSC实验——虚拟U盘

    实验步骤: 1.将SD卡插在SD卡槽中. 2.将跳线冒跳至USB_OTG,将USB_OTG通过Micor USB线与USB主机(电脑)相连. 3.烧写程序,我的电脑中将出现一个磁盘. 实验现象: 核心 ...

  8. android 监听动画对象后不能播放动画

    采用监听  AnimationListener 发现不能播放动画了. 解决办法: 将动画的启动方式:animation.startnow去掉,改为如下即可 view.startAnimation(an ...

  9. Qt动态库静态库的创建、使用、多级库依赖、动态库改成静态库等详细说明

    本文描述的是windows系统下,通过qtcreator在pro文件中添加动态库与静态库的方法: 1.添加动态库(直接添加动态库文件.dll,非子项目) 通过qtcreator创建动态库的方法就不在此 ...

  10. c# 根据字段名,得到对象中的属性值

    public string GetModelValue(string FieldName, object obj) { try { Type Ts = obj.GetType(); object o ...