Eat the Trees
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 5079 Accepted Submission(s):
2628
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))
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.
ways in one line. It is guaranteed, that it does not exceed 263 – 1.
Use the format in the sample.
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
Case 2: There are 2 ways to eat the trees.
如上图,每根红线代表的是插头DP中状态的每一位,特别的地方就在最后一位,那条横线,表示的是当前格子被更新时左边是否有向右的插头,而当前格子上面的竖线表示上面是否有向下的插头...
这样,插头DP的状态就比普通轮廓线多一位。
这两个位置就是转移的重点。
在当前格子上面有插头或者左边有插头,那么可以转移到当前向右或者向下;如果两个状态同时存在,那么将两个状态合并,不能新增插头;如果两个状态都不存在,并且没有障碍格,那么同时增加两个新插头。
第一列、最后一排、最后一列需要特判。
空间要开足!!!
Code
#include<bits/stdc++.h>
#define LL long long
using namespace std; int n, m, G[][], ti;
LL dp[][( << )]; int main() {
int T;
scanf("%d", &T);
while(T --) {
scanf("%d%d", &n, &m);
for(int i = ; i <= n; i ++)
for(int j = ; j <= m; j ++)
scanf("%d", &G[i][j]);
memset(dp, , sizeof(dp));
int now = ;
dp[now][] = ;
for(int i = ; i <= n; i ++) {
for(int j = ; j <= m; j ++) {
now ^= ;
memset(dp[now], , sizeof(dp[now]));
for(int s = ; s < ( << m + ); s ++) {
int pre = s & ( << m);
int las = s & ;
if(!G[i][j]) {
int ss = (s << );
if(!pre && !las) dp[now][ss] += dp[now ^ ][s];
} else {
if(j != ) {
if(pre && las) {
int ss = (s ^ pre ^ las) << ;
dp[now][ss] += dp[now ^ ][s];
}
if(pre && !las) {
int ss = (s ^ pre) << | ;
if(j != m) dp[now][ss] += dp[now ^ ][s];
ss = (s ^ pre) << | ;
if(i != n) dp[now][ss] += dp[now ^ ][s];
}
if(!pre && las) {
int ss = (s ^ las) << | ;
if(j != m) dp[now][ss] += dp[now ^ ][s];
ss = (s ^ las) << | ;
if(i != n) dp[now][ss] += dp[now ^ ][s];
}
if(!pre && !las) {
int ss = s << | ;
dp[now][ss] += dp[now ^ ][s];
}
} else if(j == && !las) {
if(pre) {
int ss = (s ^ pre) << | ;
if(i != n) dp[now][ss] += dp[now ^ ][s];
ss = (s ^ pre) << | ;
if(j != m) dp[now][ss] += dp[now ^ ][s];
} else {
if(i != n && j != m) {
int ss = s << | ;
dp[now][ss] += dp[now ^ ][s];
}
}
}
}
}
}
}
printf("Case %d: There are %lld ways to eat the trees.\n", ++ti, dp[now][]);
}
}