首页 > 代码库 > hdu 1693 Eat the Trees (插头dp入门)
hdu 1693 Eat the Trees (插头dp入门)
Eat the Trees
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2507 Accepted Submission(s): 1225
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.
Source
2008 “Sunline Cup” National Invitational Contest
题意:
给n*m的方格,有些方格有障碍,问有多少种方式能使所有的非障碍点都在某个环上,环可以有多个。
思路:
插头dp入门,按照点来转移,dp[i][j][k]表示到i行j列的时候,轮廓线上的插头状态为k时候的种类数,有6中情况,划一划就能知道怎样转移了,注意行与行之间单独转移,每行末尾不能有右插头。
感想:
dp如此之菜,插头dp就先放放了,先把其他的搞好,到一定阶段了再来学习吧。
代码:
#include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #include <cmath> #include <string> #include <map> #include <stack> #include <vector> #include <set> #include <queue> #define maxn 205 #define MAXN 100005 #define mod 100000000 #define INF 0x3f3f3f3f #define pi acos(-1.0) #define eps 1e-8 typedef long long ll; using namespace std; ll n,m,ans,cnt,tot,flag; ll mp[12][12],dp[12][12][1<<12]; void solve() { ll i,j,k,t; ll up,down,left,right; tot=(1<<m+1)-1; memset(dp,0,sizeof(dp)); dp[0][m][0]=1; for(i=1;i<=n;i++) { for(k=0;k<(1<<m);k++) // 行与行之间单独转移 { dp[i][0][k<<1]=dp[i-1][m][k]; } for(j=1;j<=m;j++) { for(k=0;k<=tot;k++) // i j-1的状态 { if(dp[i][j-1][k]==0) continue ; down=left=1<<j-1; right=up=1<<j; if(mp[i][j]) { if((k&left)&&(k&up)) // 两个插头 { dp[i][j][k^left^up]+=dp[i][j-1][k]; } else if(!(k&left)&&!(k&up)) // 0个插头 { dp[i][j][k^left^up]+=dp[i][j-1][k]; } else // 一个插头 { dp[i][j][k]+=dp[i][j-1][k]; dp[i][j][k^left^up]+=dp[i][j-1][k]; } } else { if(!(k&left)&&!(k&up)) dp[i][j][k]+=dp[i][j-1][k]; } } } } ans=dp[n][m][0]; } int main() { ll i,j,t,test=0; scanf("%I64d",&t); while(t--) { scanf("%I64d%I64d",&n,&m); for(i=1;i<=n;i++) { for(j=1;j<=m;j++) { scanf("%I64d",&mp[i][j]); } } solve(); printf("Case %I64d: There are %I64d ways to eat the trees.\n",++test,ans); } return 0; }
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。