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))
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.
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <iostream>
using namespace std;
typedef long long LL; const int MAXN = ; int mat[MAXN][MAXN];
LL dp[MAXN][MAXN][ << MAXN];
int n, m, T; LL solve() {
memset(dp, , sizeof(dp));
dp[][m][] = ;
for(int i = ; i <= n; ++i) {
for(int j = ; j < ( << m); ++j) dp[i][][j << ] = dp[i - ][m][j];
for(int k = ; k <= m; ++k) {
for(int state = ; state < ( << (m + )); ++state) {
int y = << k, x = y >> ;
if(mat[i][k]) {
if((state & x) && (state & y)) {
dp[i][k][state] = dp[i][k - ][state - x - y];
} else if((state & x) == && (state & y) == ) {
dp[i][k][state] = dp[i][k - ][state + x + y];
} else dp[i][k][state] = dp[i][k - ][state ^ x ^ y] + dp[i][k - ][state];
} else {
if((state & x) == && (state & y) == ) {
dp[i][k][state] = dp[i][k - ][state];
} else dp[i][k][state] = ;
}
}
}
}
return dp[n][m][];
} int main() {
scanf("%d", &T);
for(int t = ; t <= T; ++t) {
scanf("%d%d", &n, &m);
for(int i = ; i <= n; ++i)
for(int j = ; j <= m; ++j) scanf("%d", &mat[i][j]);
printf("Case %d: There are %I64d ways to eat the trees.\n", t, solve());
}
}
代码(0MS)(hash)(下面代码是lld的……):
#include <iostream>
#include <algorithm>
#include <cstring>
#include <cstdio>
using namespace std;
typedef long long LL; const int MAXH = ;
const int SIZEH = ; struct hash_map {
int head[SIZEH];
int next[MAXH], state[MAXH];
LL val[MAXH];
int size; void init() {
memset(head, -, sizeof(head));
size = ;
} void insert(int st, LL sv) {
int h = st % SIZEH;
for(int p = head[h]; ~p; p = next[p]) {
if(state[p] == st) {
val[p] += sv;
return ;
}
}
state[size] = st; val[size] = sv; next[size] = head[h]; head[h] = size++;
}
} hashmap[]; int getB(int state, int i) {
return (state >> i) & ;
} void setB(int &state, int i, int val) {
state = (state & ~( << i)) | (val << i);
} int mat[][];
int n, m, T;
hash_map *cur, *last; void update(int state, LL val, int x, int y) {
int left = getB(state, y);
int up = getB(state, y + );
if(mat[x][y] == ) {
if(left == && up == ) cur->insert(state, val);
return ;
}
if(left == && up == ) {
if(x < n - && y < m - ) {
int newState = state;
setB(newState, y, );
setB(newState, y + , );
cur->insert(newState, val);
}
} else if(left == || up == ) {
if(x < n - ) {
int newState = state;
setB(newState, y, );
setB(newState, y + , );
cur->insert(newState, val);
}
if(y < m - ) {
int newState = state;
setB(newState, y, );
setB(newState, y + , );
cur->insert(newState, val);
}
} else {
int newState = state;
setB(newState, y, );
setB(newState, y + , );
cur->insert(newState, val);
}
} LL solve() {
cur = hashmap, last = hashmap + ;
last->init();
last->insert(, );
for(int i = ; i < n; ++i) {
int sz = last->size;
for(int k = ; k < sz; ++k) last->state[k] <<= ;
for(int j = ; j < m; ++j) {
cur->init();
sz = last->size;
for(int k = ; k < sz; ++k)
update(last->state[k], last->val[k], i, j);
swap(cur, last);
}
}
for(int k = ; k < last->size; ++k)
if(last->state[k] == ) return last->val[k];
return ;
} int main() {
scanf("%d", &T);
for(int t = ; t <= T; ++t) {
scanf("%d%d", &n, &m);
for(int i = ; i < n; ++i)
for(int j = ; j < m; ++j) scanf("%d", &mat[i][j]);
printf("Case %d: There are %lld ways to eat the trees.\n", t, solve());
}
}