【插头DP】Eat the trees
2012-04-19 17:07
260 查看
Eat the Trees
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 1345 Accepted Submission(s): 634
[align=left]Problem Description[/align]
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))
[align=left]Input[/align]
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.
[align=left]Output[/align]
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.
[align=left]Sample Input[/align]
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
[align=left]Sample Output[/align]
Case 1: There are 3 ways to eat the trees.
Case 2: There are 2 ways to eat the trees.
[align=left]Source[/align]
2008 “Sunline Cup” National Invitational Contest
插头DP入门题目,虽然一次性AC了,但是仍然没有做到打的时候毫不犹豫,很多地方还是很迟疑。
比如:
q = m-j
p = q+1。
这样想就好了,p比q要多向左移动几位。
后面有几个地方q打成了p。
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <cmath>
#include <algorithm>
#include <map>
#include <set>
#include <string>
#include <cstring>
long n;long m;
const long maxs = 4200;
long long f[2][maxs];
long cnt[2];
long hash[maxs];
long map[13][13];
long Sta[2][maxs];
long ths = 0;
long pre = 1;
void clear()
{
cnt[ths] = 0;
memset(hash,0,sizeof hash);
}
long getID(long j)
{
if (!hash[j])
{
f[ths][++cnt[ths]] = 0;
Sta[ths][cnt[ths]] = j;
hash[j] = cnt[ths];
}
return hash[j];
}
void work()
{
memset(cnt,0,sizeof cnt);
memset(hash,0,sizeof hash);
ths = 0;pre = 1;
f[ths][getID(0)] = 1;
for (long i=1;i<n+1;i++)
{
for (long j=1;j<m+1;j++)
{
ths ^= pre;pre ^= ths;ths ^= pre;
clear();
for (long k=1;k<cnt[pre]+1;k++)
{
long last = Sta[pre][k];
if (j == 1)
{
if (last&1) continue;
else last >>= 1;
}
long q = m-j;
long p = q+1;
long pn = (last>>p)&1;
long qn = (last>>q)&1;
long now = last - (pn<<p) - (qn<<q);
if (!map[i][j])
{
if (!pn&&!qn)
f[ths][getID(now)]+=f[pre][k];
continue;
}
if (pn && qn)
{
f[ths][getID(now)] += f[pre][k];
continue;
}
if (!pn && !qn)
{
f[ths][getID(now|(1<<p)|(1<<q))] += f[pre][k];
continue;
}
if (!pn || !qn)
{
f[ths][getID(last)] += f[pre][k];
f[ths][getID(now|(pn<<q)|(qn<<p))] += f[pre][k];
continue;
}
}
}
}
std::cout << "There are "<< f[ths][getID(0)] <<" ways to eat the trees.\n";
return;
}
int main ()
{
freopen("eatthetrees.in","r",stdin);
freopen("eatthetrees.out","w",stdout);
long T;
scanf("%ld",&T);
long t = T;
while (t--)
{
scanf("%ld%ld",&n,&m);
std::cout << "Case " << T-t << ": ";
for (long i=1;i<n+1;i++)
for (long j=1;j<m+1;j++)
{
char tmp;
do tmp = getchar()-'0';
while (tmp && tmp-1);
map[i][j] = tmp;
}
work();
}
return 0;
}
相关文章推荐
- hdu 1693 Eat the Trees 插头dp
- HDU 1693 Eat the Trees(多回路插头DP)
- HDU_P1693 Eat the Trees(插头DP)
- HDU 1693 Eat the Trees (插头dp)
- HDU-1693 Eat the Trees 插头DP
- hdu1693 Eat the Trees (插头dp)
- HDU 1693 Eat the Trees (插头DP)
- HDU1693_Eat the Trees_插头DP
- HDU - 1693 Eat the Trees 插头dp
- hdu1693 Eat the Trees 插头DP
- 匹配注意HDU 1693(Eat the Trees-不用匹配的插头Dp)
- hdu 1697 Eat the Trees(插头dp)
- HDU1693 Eat the Trees(插头dp)
- hdu 1693 Eat the Trees 插头dp入门题
- HDU 1693 Eat the Trees(插头DP,入门题)
- hdu 1693 : Eat the Trees 【插头dp 入门】
- hdu1693 Eat the Trees 插头DP
- HDU 1693 Eat the Trees(插头DP)
- HDU 1693 Eat the Trees 插头DP
- hdu 1693 Eat the Trees (基础插头dp)