# HDU 1693 Eat the Trees — 插头dp

# Eat the Trees

**Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
**

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.

^{63}– 1. Use the format in the sample.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
#include<map> #include<cmath> #include<queue> #include<cstdio> #include<string> #include<cstring> #include<iostream> #include<algorithm> using namespace std; #define inf 1000000007 #define ll long long #define N 13 inline int rd() { int x=0,f=1;char ch=getchar(); while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();} while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();} return x*f; } int T,n,m; bool mp[N][N]; ll f[N][N][1<<N]; void sol(int T) { n=rd();m=rd(); for(int i=1;i<=n;i++) for(int j=1;j<=m;j++) mp[i][j]=rd(); memset(f,0,sizeof(f)); f[0][m][0]=1; for(int i=1;i<=n;i++) { for(int k=0;k<=(1<<m);k++) f[i][0][k<<1]=f[i-1][m][k]; for(int j=1;j<=m;j++) { int up=1<<j,lf=1<<j-1; bool a1,a2; for(int k=0;k<=(1<<m+1);k++) { a1=up&k;a2=lf&k; if(mp[i][j]) { f[i][j][k^up^lf]+=f[i][j-1][k]; if(a1^a2) f[i][j][k]+=f[i][j-1][k]; } else { if(!a1&&!a2) f[i][j][k]=f[i][j-1][k]; else f[i][j][k]=0; } } } } printf("Case %d: There are %lld ways to eat the trees.\n",T,f[n][m][0]); } int main() { T=rd(); for(int i=1;i<=T;i++) sol(i); return 0; } |

## 发表评论