【HDU 1693】Eat the Trees

Eat the Trees

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

Total Submission(s): 3004    Accepted Submission(s): 1448

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))

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.

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.

Source

2008 “Sunline Cup” National Invitational Contest

Recommend

wangye

插头dp入门题。

最简单的插头dp题(其实本题的思路是简化的插头dp思路,并不广泛适用于插头dp的题目)

题解在这里

行与行之间的转移就是______|转移到|______,横线都是一样的,那么直接把上一行的左移一位就是这一行的第一个了。

#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <cmath>
using namespace std;
int n,m,a[20][20];
long long f[20][20][1<<15];
void Plugdp()
{
	f[0][m][0]=1;
	for (int i=1;i<=n;i++)
	{
		for (int j=0;j<(1<<m);j++)
			f[i][0][j<<1]=f[i-1][m][j];
		for (int j=1;j<=m;j++)
			for (int k=0;k<(1<<(m+1));k++)
			{
				int y=1<<j,x=1<<(j-1);
				if (a[i][j])
				{
					if ((k&y)&&(k&x))
						f[i][j][k]=f[i][j-1][k-x-y];
					else if (!(k&x)&&!(k&y))
						f[i][j][k]=f[i][j-1][k+x+y];
					else f[i][j][k]=f[i][j-1][k^x^y]+f[i][j-1][k];
				}
				else
				{
					if (!(k&x)&&!(k&y))
						f[i][j][k]=f[i][j-1][k];
					else f[i][j][k]=0;
				}
			}
	}
	printf("There are %lld ways to eat the trees.\n",f[n][m][0]);
}
int main()
{
	int T;
    scanf("%d",&T);
	for (int i=1;i<=T;i++)
	{
		scanf("%d%d",&n,&m);
		for (int i=1;i<=n;i++)
			for (int j=1;j<=m;j++)
				scanf("%d",&a[i][j]);
		printf("Case %d: ",i);
		Plugdp();
	}
	return 0;
}

时间: 2024-11-02 23:28:51

【HDU 1693】Eat the Trees的相关文章

【HDU】【1693】Eat The Trees

插头DP 插头dp模板题…… 这题比CDQ论文上的例题还要简单……因为不用区分左右插头(这题可以多回路,并不是一条哈密尔顿路) 硬枚举当前位置的状态就好了>_< 题解:http://blog.csdn.net/xymscau/article/details/6756351 1 //HDU 1693 2 #include<cstdio> 3 #include<cstring> 4 #include<cstdlib> 5 #include<iostream

【HDU1693】Eat the Trees(插头dp)

[HDU1693]Eat the Trees(插头dp) 题面 HDU Vjudge 大概就是网格图上有些点不能走,现在要找到若干条不相交的哈密顿回路使得所有格子都恰好被走过一遍. 题解 这题的弱化版本吧... 因为可以任意分配哈密顿回路的数量,因此根本不需要再考虑插头的配对问题了,那么直接分情况转移就好啦. #include<iostream> #include<cstdio> #include<cstdlib> #include<cstring> #in

【HDU 4940】Destroy Transportation system(数据水/无源无汇带上下界可行流)

Description Tom is a commander, his task is destroying his enemy’s transportation system. Let’s represent his enemy’s transportation system as a simple directed graph G with n nodes and m edges. Each node is a city and each directed edge is a directe

【HDU 1009】FatMouse&#39; Trade

题 Description FatMouse prepared M pounds of cat food, ready to trade with the cats guarding the warehouse containing his favorite food, JavaBean. The warehouse has N rooms. The i-th room contains J[i] pounds of JavaBeans and requires F[i] pounds of c

【HDU 5647】DZY Loves Connecting(树DP)

pid=5647">[HDU 5647]DZY Loves Connecting(树DP) DZY Loves Connecting Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others) Total Submission(s): 332    Accepted Submission(s): 112 Problem Description DZY has an unroote

【2014 Multi-University Training Contest 3 1002】/【HDU 4888】 Redraw Beautiful Drawings

不容易啊,终于可以补第二个题了!! 顺便说一句:模版写残了就不要怪出题人啊 ~ (这残废模版研究了好长时间才找出错) 题目大意: 有一个n*m的矩阵,每一个格子里都将有一个数.给你每一行数字之和和每一列数字之和.求每一个位置能填0~k之间的哪个数.如果有多种可能输出"Not Unique",如果没有解输出"Impossible",如果一组解则将其输出. 解题思路: 最大流: 不可能的条件:是行之和和列之和不想等或者建图后的最大流与他们不想等. 多组的条件是:在最大流

【HDU 1839】 Delay Constrained Maximum Capacity Path(二分+最短路)

[HDU 1839] Delay Constrained Maximum Capacity Path(二分+最短路) Delay Constrained Maximum Capacity Path Time Limit: 10000/10000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others) Total Submission(s): 1515    Accepted Submission(s): 481 Problem

【HDU 5828】Rikka with Sequence(线段树)

[HDU 5828]Rikka with Sequence(线段树) Rikka with Sequence Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 2311    Accepted Submission(s): 391 Problem Description As we know, Rikka is poor at math.

【HDU 4352】 XHXJ&#39;s LIS (数位DP+状态压缩+LIS)

XHXJ's LIS Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 2422    Accepted Submission(s): 990 Problem Description #define xhxj (Xin Hang senior sister(学姐)) If you do not know xhxj, then careful