打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
【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): 5079    Accepted Submission(s): 2628


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

 

 

InputThe 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. 

 

OutputFor 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 Input26 31 1 11 0 11 1 11 1 11 0 11 1 12 41 1 1 11 1 1 1 

 

Sample OutputCase 1: There are 3 ways to eat the trees.Case 2: There are 2 ways to eat the trees. 

 

Source2008 “Sunline Cup” National Invitational Contest  

 

Recommendwangye   |   We have carefully selected several similar problems for you:  1691 1695 1689 1692 1690  
Solution之前某次线上赛做过的题,还记得当时调到shi都过不了....重做一遍咸鱼翻身!然而这道题几乎就是插头DP模板题叻,相对于普通的轮廓线每位代表的是一个格子,插头DP的轮廓线每一位代表的是每个边框。

 

如上图,每根红线代表的是插头DP中状态的每一位,特别的地方就在最后一位,那条横线,表示的是当前格子被更新时左边是否有向右的插头,而当前格子上面的竖线表示上面是否有向下的插头...

这样,插头DP的状态就比普通轮廓线多一位。

这两个位置就是转移的重点。

在当前格子上面有插头或者左边有插头,那么可以转移到当前向右或者向下;如果两个状态同时存在,那么将两个状态合并,不能新增插头;如果两个状态都不存在,并且没有障碍格,那么同时增加两个新插头。

第一列、最后一排、最后一列需要特判。

空间要开足!!!

Code

 

#include<bits/stdc  .h>#define LL long longusing namespace std;int n, m, G[12][12], ti;LL dp[2][(1 << 12)];int main() {    int T;    scanf("%d", &T);    while(T --) {        scanf("%d%d", &n, &m);        for(int i = 1; i <= n; i   )            for(int j = 1; j <= m; j   )                scanf("%d", &G[i][j]);        memset(dp, 0, sizeof(dp));        int now = 0;        dp[now][0] = 1;        for(int i = 1; i <= n; i   ) {            for(int j = 1; j <= m; j   ) {                now ^= 1;                memset(dp[now], 0, sizeof(dp[now]));                for(int s = 0; s < (1 << m   1); s   ) {                    int pre = s & (1 << m);                    int las = s & 1;                    if(!G[i][j]) {                        int ss = (s << 1);                        if(!pre && !las)    dp[now][ss]  = dp[now ^ 1][s];                    } else {                        if(j != 1) {                            if(pre && las) {                                int ss = (s ^ pre ^ las) << 1;                                dp[now][ss]  = dp[now ^ 1][s];                            }                            if(pre && !las) {                                int ss = (s ^ pre) << 1 | 1;                                if(j != m)    dp[now][ss]  = dp[now ^ 1][s];                                ss = (s ^ pre) << 1 | 2;                                if(i != n)    dp[now][ss]  = dp[now ^ 1][s];                            }                            if(!pre && las) {                                int ss = (s ^ las) << 1 | 1;                                if(j != m)    dp[now][ss]  = dp[now ^ 1][s];                                ss = (s ^ las) << 1 | 2;                                if(i != n)    dp[now][ss]  = dp[now ^ 1][s];                            }                            if(!pre && !las) {                                int ss = s << 1 | 3;                                dp[now][ss]  = dp[now ^ 1][s];                            }                        } else if(j == 1 && !las) {                            if(pre) {                                int ss = (s ^ pre) << 1 | 2;                                if(i != n)    dp[now][ss]  = dp[now ^ 1][s];                                ss = (s ^ pre) << 1 | 1;                                if(j != m)    dp[now][ss]  = dp[now ^ 1][s];                            } else {                                if(i != n && j != m) {                                    int ss = s << 1 | 3;                                    dp[now][ss]  = dp[now ^ 1][s];                                }                            }                        }                    }                }            }        }        printf("Case %d: There are %lld ways to eat the trees.\n",   ti, dp[now][0]);    }}

 

 

 

来源:http://www.icode9.com/content-4-54751.html
本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
【复习整理】小学英语五年级下册期末复习资料(PEP)
樹療法
小学英语四时态习题
TREES
多重集组合数
C++之笔试题基础45 盘古游戏:笔试,打印机
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服