hdu1693
Eat the Trees
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2542 Accepted Submission(s): 1238
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))
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.
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.
最简单的插头DP,多条回路
不知道hdu的编绎器哪里鬼畜了。用long long一直都WA,一改成__int64就过了。。。TAT。亏我还调了半天。。。。
#include<cstdio> #include<cstdlib> #include<algorithm> #include<cmath> #include<cstring> using namespace std; typedef long long ll; ll xzq; int n,m,i,j,k,l,t,x,y,tl,tt; int a[15][15]; __int64 f[12][12][8193]; int main() { scanf("%d",&tl); for(tt=1;tt<=tl;tt++){ xzq=0; memset(f,0,sizeof(f)); memset(a,0,sizeof(a)); scanf("%d%d",&n,&m); for(i=1;i<=n;i++){ for(j=1;j<=m;j++){ scanf("%d",&a[i][j]); } } f[0][m][0]=1; for(i=0;i<=n;i++){ for(j=1;j<=m;j++){ if(i==0&&j<m)continue; for(k=0;k<=(1<<(m+1))-1;k++)if(f[i][j][k]){ if(j==m){ if(i!=n){ t=k*2; if(t>=(1<<(m+1)))t=t-(1<<(m+1)); t=t-(t&1)-(t&2); if((k&1)==0){ if(a[i+1][1]==0)f[i+1][1][t]+=f[i][j][k]; else{ if(i+1!=n&&m>1)f[i+1][1][t+3]+=f[i][j][k]; } } else{ if(a[i+1][1]!=0){ if(m>1)f[i+1][1][t+2]+=f[i][j][k]; if(i+1!=n)f[i+1][1][t+1]+=f[i][j][k]; } } } } else{ x=1<<j; y=1<<(j+1); t=k; t=t-(t&x)-(t&y); l=k&(x+y); if(l==0){ if(a[i][j+1]==0)f[i][j+1][t]+=f[i][j][k]; else{ if(i!=n&&j+1!=m)f[i][j+1][t+x+y]+=f[i][j][k]; } } if(l==x||l==y){ if(a[i][j+1]!=0){ if(i!=n)f[i][j+1][t+x]+=f[i][j][k]; if(j+1!=m)f[i][j+1][t+y]+=f[i][j][k]; } } if(l==x+y){ if(a[i][j+1]!=0)f[i][j+1][t]+=f[i][j][k]; } } } } } xzq=f[n][m][0]; printf("Case %d: There are %I64d ways to eat the trees.\n",tt,xzq); } }