HDU1693 Eat the Trees —— 插头DP

题目链接:https://vjudge.net/problem/HDU-1693

 

Eat the Trees

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 4505    Accepted Submission(s): 2290


Problem Description
Most of us know that in the game called DotA(Defense of the Ancient), Pudge is a strong hero in the first period of the game. When the game goes to end however, Pudge is not a strong hero any more.
So Pudge’s teammates give him a new assignment—Eat the Trees!

The trees are in a rectangle N * M cells in size and each of the cells either has exactly one tree or has nothing at all. And what Pudge needs to do is to eat all trees that are in the cells.
There are several rules Pudge must follow:
I. Pudge must eat the trees by choosing a circuit and he then will eat all trees that are in the chosen circuit.
II. The cell that does not contain a tree is unreachable, e.g. each of the cells that is through the circuit which Pudge chooses must contain a tree and when the circuit is chosen, the trees which are in the cells on the circuit will disappear.
III. Pudge may choose one or more circuits to eat the trees.

Now Pudge has a question, how many ways are there to eat the trees?
At the picture below three samples are given for N = 6 and M = 3(gray square means no trees in the cell, and the bold black line means the chosen circuit(s))

 

 

Input
The input consists of several test cases. The first line of the input is the number of the cases. There are no more than 10 cases.
For each case, the first line contains the integer numbers N and M, 1<=N, M<=11. Each of the next N lines contains M numbers (either 0 or 1) separated by a space. Number 0 means a cell which has no trees and number 1 means a cell that has exactly one tree.
 

 

Output
For each case, you should print the desired number of ways in one line. It is guaranteed, that it does not exceed 263 – 1. Use the format in the sample.
 

 

Sample Input
2 6 3 1 1 1 1 0 1 1 1 1 1 1 1 1 0 1 1 1 1 2 4 1 1 1 1 1 1 1 1
 

 

Sample Output
Case 1: There are 3 ways to eat the trees. Case 2: There are 2 ways to eat the trees.
 

 

Source
 

 

Recommend
wangye

 

 

题意:

用一个或者多个回路去经过所有空格子,问总共有多少种情况?

 

题解:

有关路径的插头DP。

1.由于只需要记录轮廓线上m+1个位置的插头情况(0或1),且m<=11,2^11 = 2048,故可以用二进制对轮廓线的信息进行压缩。

2.接着就是人工枚举了。

 

对于如何维护轮廓线的理解:

1.对于棋盘。我们需要将其开始下标设为1(方便维护轮廓线)。

2.将要处理第i行第1列的时候,它的左插头的信息记录在第0位,它的上插头的信息刚好记录在第1位。

3.当处理完第i行第1列的时候,我们就把它的左插头改为下插头,上插头改为右插头。这样,对于第i行第2列来说,它的左插头记录在第1位,上插头记录在第2位。所以可以总结出:对于当前要转移的格子a[i][j],它的左插头的信息记录在第j-1位,它的上插头记录在第j位。

4.当转移完第i行最后一个格子的时候,左插头就位于轮廓线的最后位置,即其信息记录在最高位。而我们在转到下一行第一列的时候,由于a[i+1][1]的左插头是记录在第0位的,所以就需要把最高位的移到第0位,后面的位又往后移动一位,即左移。我们又知道:第一列的格子是没有左插头的,所以,在转行的时候,有效的轮廓线是没有左插头轮廓线,我们取这些有效的轮廓线,并对其压缩信息左移一位即可。

 

 

写法一:

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4 #include <algorithm>
 5 #include <vector>
 6 #include <cmath>
 7 #include <queue>
 8 #include <stack>
 9 #include <map>
10 #include <string>
11 #include <set>
12 using namespace std;
13 typedef long long LL;
14 const int INF = 2e9;
15 const LL LNF = 9e18;
16 const int MOD = 1e9+7;
17 const int MAXN = 1e5;
18 
19 int n, m, Map[12][12];
20 LL dp[12][12][1<<12];
21 
22 int main()
23 {
24     int T, kase = 0;
25     scanf("%d", &T);
26     while(T--)
27     {
28         scanf("%d%d", &n, &m);
29         for(int i = 1; i<=n; i++)
30         for(int j = 1; j<=m; j++)
31             scanf("%d", &Map[i][j]);
32 
33         memset(dp, 0, sizeof(dp));
34         dp[0][m][0] = 1;      //初始状态
35         for(int i = 1; i<=n; i++)
36         {
37             //可知转移完行末的格子后,左插头在轮廓线上最后一个位置,即其信息记录在最后一个位
38             //由于第一列的格子肯定没有左插头,所以只需枚举到(1<<m)-1,以去除掉含有左插头的情况。
39             for(int s = 0; s<(1<<m); s++)
40                 dp[i][0][s<<1] = dp[i-1][m][s];
41 
42             for(int j = 1; j<=m; j++)
43             {
44                 for(int s = 0; s<(1<<m+1); s++) //枚举上一个格子的所以状态,即当前格子的轮廓线
45                 {
46                     int up = 1<<j;  //上插头
47                     int left = 1<<(j-1);    //左插头
48                     bool have_up = s&up;
49                     bool have_left = s&left;
50 
51                     if(!Map[i][j])  //当前格子不可行
52                     {
53                         if( !have_up && !have_left )  //当没有插头时,才能转移
54                             dp[i][j][s] += dp[i][j-1][s];
55                     }
56                     else        //当前格子可行
57                     {
58                         if( !have_up && !have_left )    //两个格子都不存在,新建分量
59                         {
60                             dp[i][j][s^up^left] += dp[i][j-1][s];
61                         }
62                         else if( have_up && !have_left )    //有上插头无左插头
63                         {
64                             dp[i][j][s] += dp[i][j-1][s];   //往右延伸
65                             dp[i][j][s^up^left] += dp[i][j-1][s];   //往下延伸
66                         }
67                         else if( !have_up && have_left ) //无上插头有左插头
68                         {
69                             dp[i][j][s] += dp[i][j-1][s];   //往下延伸
70                             dp[i][j][s^up^left] += dp[i][j-1][s]; //往右延伸
71                         }
72                         else    //两个插头都存在,只能合并分量
73                         {
74                             dp[i][j][s^up^left] += dp[i][j-1][s];
75                         }
76                     }
77                 }
78             }
79         }
80         printf("Case %d: There are %lld ways to eat the trees.\n", ++kase, dp[n][m][0]);
81     }
82 }
View Code

 

写法二(写法一的简写):

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4 #include <algorithm>
 5 #include <vector>
 6 #include <cmath>
 7 #include <queue>
 8 #include <stack>
 9 #include <map>
10 #include <string>
11 #include <set>
12 using namespace std;
13 typedef long long LL;
14 const int INF = 2e9;
15 const LL LNF = 9e18;
16 const int MOD = 1e9+7;
17 const int MAXN = 1e5;
18 
19 int n, m, Map[12][12];
20 LL dp[12][12][1<<12];
21 
22 int main()
23 {
24     int T, kase = 0;
25     scanf("%d", &T);
26     while(T--)
27     {
28         scanf("%d%d", &n, &m);
29         for(int i = 1; i<=n; i++)
30         for(int j = 1; j<=m; j++)
31             scanf("%d", &Map[i][j]);
32 
33         memset(dp, 0, sizeof(dp));
34         dp[0][m][0] = 1;      //初始状态
35         for(int i = 1; i<=n; i++)
36         {
37             //可知转移完行末的格子后,左插头在轮廓线上最后一个位置,即其信息记录在最后一个位
38             //由于第一列的格子肯定没有左插头,所以只需枚举到(1<<m)-1,以去除掉含有左插头的情况。
39             for(int s = 0; s<(1<<m); s++)
40                 dp[i][0][s<<1] = dp[i-1][m][s];
41 
42             for(int j = 1; j<=m; j++)
43             {
44                 for(int s = 0; s<(1<<m+1); s++) //枚举上一个格子的所以状态,即当前格子的轮廓线
45                 {
46                     int up = 1<<j;  //上插头
47                     int left = 1<<(j-1);    //左插头
48                     bool have_up = s&up;
49                     bool have_left = s&left;
50 
51                     if(!Map[i][j])  //当前格子不可行
52                     {
53                         if( !have_up && !have_left )  //当没有插头时,才能转移
54                             dp[i][j][s] += dp[i][j-1][s];
55                     }
56                     else        //当前格子可行
57                     {
58                         dp[i][j][s^up^left] += dp[i][j-1][s];
59                         if( (have_up&&!have_left) || (!have_up&&have_left) )
60                             dp[i][j][s] += dp[i][j-1][s];
61                     }
62                 }
63             }
64         }
65         printf("Case %d: There are %lld ways to eat the trees.\n", ++kase, dp[n][m][0]);
66     }
67 }
View Code

 

对写法一用哈希表优化了一下,但是过不了,差不出错误。先放着:

  1 #include <iostream>
  2 #include <cstdio>
  3 #include <cstring>
  4 #include <algorithm>
  5 #include <vector>
  6 #include <cmath>
  7 #include <queue>
  8 #include <stack>
  9 #include <map>
 10 #include <string>
 11 #include <set>
 12 using namespace std;
 13 typedef long long LL;
 14 const int INF = 2e9;
 15 const LL LNF = 9e18;
 16 const int MOD = 1e9+7;
 17 const int MAXN = 1e5;
 18 const int HASH = 1e4;
 19 
 20 int n, m, Map[15][15];
 21 
 22 struct
 23 {
 24     int size, head[HASH], next[MAXN];
 25     int state[MAXN];
 26     LL sum[MAXN];
 27 
 28     void init()
 29     {
 30         size = 0;
 31         memset(head, -1, sizeof(head));
 32     }
 33 
 34     void insert(int status, LL Sum)
 35     {
 36         int u = status%HASH;
 37         for(int i = head[u]; i!=-1; i = next[i])
 38         {
 39             if(state[i]==status)
 40             {
 41                 sum[i] += Sum;
 42                 return;
 43             }
 44         }
 45         state[size] = status;   //头插法
 46         sum[size] = Sum;
 47         next[size] = head[u];
 48         head[u] = size++;
 49     }
 50 
 51 }Hash_map[2];
 52 
 53 void transfer(int i, int j, int cur)
 54 {
 55     for(int k = 0; k<Hash_map[cur].size; k++)
 56     {
 57         int s = Hash_map[cur].state[k];
 58         LL Sum = Hash_map[cur].sum[k];
 59 
 60         int up = 1<<j;
 61         int left = 1<<(j-1);
 62         bool have_up = s&up;
 63         bool have_left = s&left;
 64 
 65         if(!Map[i][j])
 66         {
 67             Hash_map[cur^1].insert(s<<(j==m), Sum);
 68         }
 69         else
 70         {
 71             if( !have_up && !have_left )
 72             {
 73                 if(Map[i+1][j] && Map[i][j+1])
 74                     Hash_map[cur^1].insert((s^up^left)<<(j==m), Sum);
 75             }
 76             else if( have_up && !have_left )    //有上插头无左插头
 77             {
 78                 if(Map[i][j+1])
 79                     Hash_map[cur^1].insert(s<<(j==m), Sum);
 80                 if(Map[i+1][j])
 81                     Hash_map[cur^1].insert((s^up^left)<<(j==m), Sum);
 82             }
 83             else if( !have_up && have_left ) //无上插头有左插头
 84             {
 85                 if(Map[i][j+1])
 86                     Hash_map[cur^1].insert((s^up^left)<<(j==m), Sum);
 87                 if(Map[i+1][j])
 88                     Hash_map[cur^1].insert(s<<(j==m), Sum);
 89             }
 90             else
 91             {
 92                 Hash_map[cur^1].insert((s^up^left)<<(j==m), Sum);
 93             }
 94         }
 95     }
 96 }
 97 
 98 int main()
 99 {
100     int T, kase = 0;
101     scanf("%d", &T);
102     while(T--)
103     {
104         scanf("%d%d", &n, &m);
105         memset(Map, 0, sizeof(Map));
106         for(int i = 1; i<=n; i++)
107         for(int j = 1; j<=m; j++)
108             scanf("%d", &Map[i][j]);
109 
110         int cur = 0;
111         Hash_map[cur].init();
112         Hash_map[cur].insert(0, 1);
113         for(int i = 1; i<=n; i++)
114         {
115             for(int j = 1; j<=m; j++)
116             {
117                 Hash_map[cur^1].init();
118                 transfer(i, j, cur);
119                 cur ^= 1;
120             }
121         }
122         printf("Case %d: There are %lld ways to eat the trees.\n", ++kase, Hash_map[cur].sum[0]);
123     }
124 }
View Code

 

posted on 2017-12-05 20:29  h_z_cong  阅读(288)  评论(0编辑  收藏  举报

导航