题意:给定一个图,X表示不能走,O表示必须要走,*表示可走可不走,问你多少种走的法,使得形成一个回路。

析:

代码如下:

  1. #pragma comment(linker, "/STACK:1024000000,1024000000")
  2. #include <cstdio>
  3. #include <string>
  4. #include <cstdlib>
  5. #include <cmath>
  6. #include <iostream>
  7. #include <cstring>
  8. #include <set>
  9. #include <queue>
  10. #include <algorithm>
  11. #include <vector>
  12. #include <map>
  13. #include <cctype>
  14. #include <cmath>
  15. #include <stack>
  16. #include <sstream>
  17. #include <list>
  18. #include <assert.h>
  19. #include <bitset>
  20. #define debug() puts("++++");
  21. #define gcd(a, b) __gcd(a, b)
  22. #define lson l,m,rt<<1
  23. #define rson m+1,r,rt<<1|1
  24. #define fi first
  25. #define se second
  26. #define pb push_back
  27. #define sqr(x) ((x)*(x))
  28. #define ms(a,b) memset(a, b, sizeof a)
  29. //#define sz size()
  30. #define pu push_up
  31. #define pd push_down
  32. #define cl clear()
  33. #define all 1,n,1
  34. #define FOR(i,x,n) for(int i = (x); i < (n); ++i)
  35. #define freopenr freopen("in.txt", "r", stdin)
  36. #define freopenw freopen("out.txt", "w", stdout)
  37. using namespace std;
  38.  
  39. typedef long long LL;
  40. typedef unsigned long long ULL;
  41. typedef pair<int, int> P;
  42. const int INF = 0x3f3f3f3f;
  43. const double inf = 1e20;
  44. const double PI = acos(-1.0);
  45. const double eps = 1e-8;
  46. const int maxn = 10 + 10;
  47. const int maxm = 1e5 + 10;
  48. const int mod = 50007;
  49. const int dr[] = {-1, 0, 1, 0};
  50. const int dc[] = {0, -1, 0, 1};
  51. const char *de[] = {"0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111", "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111"};
  52. int n, m;
  53. const int mon[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
  54. const int monn[] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
  55. inline bool is_in(int r, int c) {
  56. return r >= 0 && r < n && c >= 0 && c < m;
  57. }
  58.  
  59. struct Hash{
  60. int head[mod], next[maxm], sz;
  61. LL state[maxm], f[maxm];
  62. void clear(){ sz = 0; ms(head, -1); }
  63. void push(LL st, LL ans){
  64. int h = st % mod;
  65. for(int i = head[h]; ~i; i = next[i])
  66. if(st == state[i]){
  67. f[i] += ans;
  68. return ;
  69. }
  70. f[sz] = ans;
  71. state[sz] = st;
  72. next[sz] = head[h];
  73. head[h] = sz++;
  74. }
  75. };
  76. Hash dp[2];
  77. int isend; // is or not form loop
  78. int code[maxn], ch[maxn];
  79. int a[maxn][maxn]; // 1 must go 2 go or not
  80. char s[maxn];
  81.  
  82. void decode(int m, LL st){
  83. for(int i = m; i >= 0; --i){
  84. code[i] = st&7;
  85. st >>= 3;
  86. }
  87. isend = st&1; // the highest bit
  88. }
  89.  
  90. LL encode(int m){
  91. LL st = isend;
  92. int cnt = 1; ms(ch, -1);
  93. ch[0] = 0;
  94. for(int i = 0; i <= m; ++i){
  95. if(ch[code[i]] == -1) ch[code[i]] = cnt++;
  96. st <<= 3;
  97. st |= ch[code[i]];
  98. }
  99. return st;
  100. }
  101.  
  102. void shift(int m){
  103. for(int i = m; i; --i) code[i] = code[i-1];
  104. code[0] = 0;
  105. }
  106.  
  107. void dpblank(int i, int j, int cur){
  108. for(int k = 0; k < dp[cur].sz; ++k){
  109. decode(m, dp[cur].state[k]);
  110. int left = code[j-1];
  111. int up = code[j];
  112. if(isend){ // already form loop
  113. if(up || left || a[i][j] == 1) continue;
  114. code[j] = code[j-1] = 0;
  115. if(j == m) shift(m);
  116. dp[cur^1].push(encode(m), dp[cur].f[k]);
  117. continue;
  118. }
  119. if(up && left){
  120. if(up == left){ // last grid
  121. code[j-1] = code[j] = 0;
  122. isend = 1;
  123. if(j == m) shift(m);
  124. dp[cur^1].push(encode(m), dp[cur].f[k]);
  125. }
  126. else{
  127. code[j] = code[j-1] = 0;
  128. for(int i = 0; i <= m; ++i)
  129. if(code[i] == up) code[i] = left;
  130. if(j == m) shift(m);
  131. dp[cur^1].push(encode(m), dp[cur].f[k]);
  132. }
  133. }
  134. else if(up || left){
  135. int t = max(up, left);
  136. if(a[i][j+1]){
  137. code[j] = t;
  138. code[j-1] = 0;
  139. dp[cur^1].push(encode(m), dp[cur].f[k]);
  140. }
  141. if(a[i+1][j]){
  142. code[j] = 0;
  143. code[j-1] = t;
  144. if(j == m) shift(m);
  145. dp[cur^1].push(encode(m), dp[cur].f[k]);
  146. }
  147. }
  148. else{
  149. if(a[i+1][j] && a[i][j+1]){
  150. code[j] = code[j-1] = 14;
  151. dp[cur^1].push(encode(m), dp[cur].f[k]);
  152. }
  153. if(a[i][j] == 2){
  154. code[j] = code[j-1] = 0;
  155. if(j == m) shift(m);
  156. dp[cur^1].push(encode(m), dp[cur].f[k]);
  157. }
  158. }
  159. }
  160. }
  161.  
  162. void dpblock(int i, int j, int cur){
  163. for(int k = 0; k < dp[cur].sz; ++k){
  164. decode(m, dp[cur].state[k]);
  165. code[j] = code[j-1] = 0;
  166. if(j == m) shift(m);
  167. dp[cur^1].push(encode(m), dp[cur].f[k]);
  168. }
  169. }
  170.  
  171. int main(){
  172. int T; cin >> T;
  173. for(int kase = 1; kase <= T; ++kase){
  174. scanf("%d %d", &n, &m);
  175. ms(a, 0);
  176. for(int i = 1; i <= n; ++i){
  177. scanf("%s", s + 1);
  178. for(int j = 1; j <= m; ++j){
  179. if(s[j] == 'O') a[i][j] = 1; // must go
  180. else if(s[j] == '*') a[i][j] = 2; // not need go
  181. }
  182. }
  183. int cur = 0;
  184. dp[cur].cl; dp[cur].push(0, 1);
  185. for(int i = 1; i <= n; ++i)
  186. for(int j = 1; j <= m; ++j){
  187. dp[cur^1].cl;
  188. if(a[i][j]) dpblank(i, j, cur);
  189. else dpblock(i, j, cur);
  190. cur ^= 1;
  191. }
  192. LL ans = 0;
  193. for(int i = 0; i < dp[cur].sz; ++i)
  194. ans += dp[cur].f[i];
  195. printf("Case %d: %I64d\n", kase, ans);
  196. }
  197. return 0;
  198. }

  

FZU 1977 Pandora adventure (DP)的更多相关文章

  1. FZU 1977 Pandora adventure (插头DP,常规)

    题意:有一个n*m矩阵,其中有些格子必走,有些格子不可走,其他格子是可走也可不走,问有多少条哈密顿回路? 思路: 本来是一道很简单的题,代码写多了连白痴bug都查不出了,竟然用i>=ex& ...

  2. 【FZU】1977 Pandora adventure

    http://acm.fzu.edu.cn/problem.php?pid=1977 题意:n×m的网格,有3种格子,'O'必须经过.'*'可以选择经过.'X'不能经过.现在要求路径经过所有'O'且是 ...

  3. FZU1977 Pandora adventure —— 插头DP

    题目链接:https://vjudge.net/problem/FZU-1977  Problem 1977 Pandora adventure Accept: 597    Submit: 2199 ...

  4. FZU - 2204 简单环形dp

    FZU - 2204 简单环形dp 题目链接 n个有标号的球围成一个圈.每个球有两种颜色可以选择黑或白染色.问有多少种方案使得没有出现连续白球7个或连续黑球7个. 输入 第一行有多组数据.第一行T表示 ...

  5. FZU 1025 状压dp 摆砖块

    云峰菌曾经提到过的黄老师过去讲课时的摆砖块 那时百度了一下题目 想了想并没有想好怎么dp 就扔了 这两天想补动态规划知识 就去FZU做专题 然后又碰到了 就认真的想并且去做了 dp思想都在代码注释里 ...

  6. FZU 2092 收集水晶 dp+bfs

    定义dp[t][x1][y1][x2][y2]为在t时刻,人走到x1,y1,影子走到x2,y2所获得最大价值 最终就是所有的dp[max][..][..][..][..]的最大值 然后递推也很自然,枚 ...

  7. FZU 2113(数位dp)

    题目连接:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=38054 题意:求区间[a,b]中包含'1'的个数. 分析:数位dp ...

  8. FZU 1502 Letter Deletion(DP)

    Description You are given two words (each word consists of upper-case English letters). Try to delet ...

  9. [FZU1977] Pandora adventure

    来学插头DP了= = GDKOI前觉得不会考数位DP,GDOI前觉得插头DP用不上.. 结果令人伤感>_< 这题并不用增加状态.. 只要在形成环的时候,让形成环的位置在最后一个必走点之后, ...

随机推荐

  1. CF gym101933 K King's Colors——二项式反演

    题目:http://codeforces.com/gym/101933/problem/K 每个点只要和父亲不同色就行.所以 “至多 i 种颜色” 的方案数就是 i * ( i-1 )n-1 . #i ...

  2. 360全景技术支持中心(KRPanoGUI三维全景制作软件)

    http://www.360pano.cn/ http://www.360pano.cn/88/ http://www.suse.edu.cn/qjmy/hd/index.html

  3. Android wm指令用法详解

    wm 是查看和设置显示信息的指令,此指令只能临时调试使用. wm:查看 wm 指令信息 $ adb shell root@rk3288:/ # wm wm usage: wm [subcommand] ...

  4. SpringCloud初体验:一、Eureka 服务的注册与发现

    Eureka :云端服务发现,一个基于 REST 的服务,用于定位服务,以实现云端中间层服务发现和故障转移. Eureka 可以大致理解为 房产中介 和 房东 的关系,房东想让租客租房子,首先要把房子 ...

  5. py基础3--函数,递归,内置函数

    本节内容 函数基本语法及特性 参数与局部变量 返回值 嵌套函数 递归 匿名函数 函数式编程介绍 高阶函数 内置函数 1. 函数基本语法及特性 背景提要 现在老板让你写一个监控程序,监控服务器的系统状况 ...

  6. CentOS下MySQL的彻底卸载

      #################CentOS7下MySQL的卸载#################### 1:查看MySQL是否安装: 方式1: [root@localhost usr]# yu ...

  7. Oracle11g 搭建单实例DataGuard (转载)

    原文:http://blog.itpub.net/29324876/viewspace-1246133/ 环境:主备库都为单实例并且数据库SID相同 OS:red hat 6.5 Oracle:11. ...

  8. springboot 2.0 自定义redis自动装配

    首先创建maven项目 pom.xml: <?xml version="1.0" encoding="UTF-8"?> <project xm ...

  9. 新手之:SpringBoot ——Reids主从哨兵整合(CentOS7)

    一.Redis主从搭建(一台服务器模拟多个端口) 结构图:) 1.确保安装了Redis,我装在了/opt/redis目录下.可通过"whereis redis-cli"命令查看是否 ...

  10. Python Web框架——Flask

    简介 Flask是一个基于Python开发并且依赖jinja2模板和Werkzeug WSGI服务的一个微型框架,对于Werkzeug本质是Socket服务端,其用于接收http请求并对请求进行预处理 ...