插头dp里面最简单的任意多回路问题,因为任意连通分量都可以合并,所以不用对插头标号,以前写过几道轮廓线dp,插头dp还是第一次写(QAQ,不会的东西好多啊)
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <queue>
#include <algorithm>
#include <vector>
#include <cstring>
#include <stack>
#include <cctype>
#include <utility>
#include <map>
#include <string>
#include <climits>
#include <set>
#include <string>
#include <sstream>
#include <utility>
#include <ctime>
using std::priority_queue;
using std::vector;
using std::swap;
using std::stack;
using std::sort;
using std::max;
using std::min;
using std::pair;
using std::map;
using std::string;
using std::cin;
using std::cout;
using std::set;
using std::queue;
using std::string;
using std::istringstream;
using std::make_pair;
using std::getline;
using std::greater;
using std::endl;
using std::multimap;
using std::deque;
typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int, int> PAIR;
typedef multimap<int, int> MMAP;
const int MAXN(10010);
const int MAXM(10010);
const int MAXE(10010);
const int HSIZE(13131);
const int SIGMA_SIZE(26);
const int MAXH(19);
const int INFI(2000000000);
const int MOD(100000000);
const ULL BASE(31);
const LL LIM(10000000);
const int INV(-10000);
inline void checkmax(int &op1, int op2){if(op1 < op2) op1 = op2;}
struct HASH_MAP
{
int first[HSIZE], state[MAXN], next[MAXN];
LL value[MAXN];
int size;
void init()
{
size = 0;
memset(first, -1, sizeof(first));
}
void insert(int ts, LL tv)
{
int h = ts%HSIZE;
for(int i = first[h]; ~i; i = next[i])
if(state[i] == ts)
{
value[i] += tv;
return;
}
state[size] = ts;
value[size] = tv;
next[size] = first[h];
first[h] = size++;
}
} hm[2];
int mp[15][15];
int n_case;
void solve(int n, int m)
{
int cur = 0, last = 1;
int lim = (1 << m)-1;
hm[last].init();
hm[last].insert(0, 1);
for(int i = 0; i < n; ++i)
for(int j = 0; j < m; ++j)
{
hm[cur].init();
int sz = hm[last].size;
for(int k = 0; k < sz; ++k)
{
int ts = hm[last].state[k];
if(mp[i][j])
{
if(ts&(1 << m))
{
if(ts&1)
hm[cur].insert((ts&lim) >> 1, hm[last].value[k]);
else
{
if(j < m-1)
hm[cur].insert(((ts&lim) >> 1)|(1 << m), hm[last].value[k]);
hm[cur].insert(((ts&lim) >> 1)|(1 << (m-1)), hm[last].value[k]);
}
}
else if(ts&1)
{
if(j < m-1)
hm[cur].insert(((ts&lim) >> 1)|(1 << m), hm[last].value[k]);
hm[cur].insert(((ts&lim) >> 1)|(1 << (m-1)), hm[last].value[k]);
}
else if(j < m-1)
hm[cur].insert(((ts&lim) >> 1)|(3 << (m-1)), hm[last].value[k]);
}
else if((ts&1) == 0 && (ts&(1 << m)) == 0)
hm[cur].insert((ts&lim) >> 1, hm[last].value[k]);
}
cur ^= 1;
last ^= 1;
}
LL ans = 0;
for(int i = 0; i < hm[last].size; ++i)
if(hm[last].state[i] == 0)
{
ans = hm[last].value[i];
break;
}
printf("Case %d: There are %I64d ways to eat the trees.\n", ++n_case, ans);
}
int main()
{
int TC;
int n, m;
scanf("%d", &TC);
while(TC--)
{
scanf("%d%d", &n, &m);
for(int i = 0; i < n; ++i)
for(int j = 0; j < m; ++j)
scanf("%d", mp[i]+j);
solve(n, m);
}
return 0;
}