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 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
 
Recommend
wangye   |   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 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][]);
}
}

【HDU】1693:Eat the Trees【插头DP】的更多相关文章

  1. hdu 1693 Eat the Trees——插头DP

    题目:http://acm.hdu.edu.cn/showproblem.php?pid=1693 第一道插头 DP ! 直接用二进制数表示状态即可. #include<cstdio> # ...

  2. HDU 1693 Eat the Trees(插头DP)

    题目链接 USACO 第6章,第一题是一个插头DP,无奈啊.从头看起,看了好久的陈丹琦的论文,表示木看懂... 大体知道思路之后,还是无法实现代码.. 此题是插头DP最最简单的一个,在一个n*m的棋盘 ...

  3. HDU 1693 Eat the Trees ——插头DP

    [题目分析] 吃树. 直接插头DP,算是一道真正的入门题目. 0/1表示有没有插头 [代码] #include <cstdio> #include <cstring> #inc ...

  4. hdu1693 Eat the Trees [插头DP经典例题]

    想当初,我听见大佬们谈起插头DP时,觉得插头DP是个神仙的东西. 某大佬:"考场见到插头DP,直接弃疗." 现在,我终于懂了他们为什么这么说了. 因为-- 插头DP很毒瘤! 为什么 ...

  5. HDU 1693 Eat the Trees(插头DP、棋盘哈密顿回路数)+ URAL 1519 Formula 1(插头DP、棋盘哈密顿单回路数)

    插头DP基础题的样子...输入N,M<=11,以及N*M的01矩阵,0(1)表示有(无)障碍物.输出哈密顿回路(可以多回路)方案数... 看了个ppt,画了下图...感觉还是挺有效的... 参考 ...

  6. HDU - 1693 Eat the Trees(多回路插头DP)

    题目大意:要求你将全部非障碍格子都走一遍,形成回路(能够多回路),问有多少种方法 解题思路: 參考基于连通性状态压缩的动态规划问题 - 陈丹琦 下面为代码 #include<cstdio> ...

  7. HDU 1693 Eat the Trees(插头DP,入门题)

    Problem Description Most of us know that in the game called DotA(Defense of the Ancient), Pudge is a ...

  8. HDU 1693 Eat the Trees (插头DP)

    题意:给一个n*m的矩阵,为1时代表空格子,为0时代表障碍格子,问如果不经过障碍格子,可以画一至多个圆的话,有多少种方案?(n<12,m<12) 思路: 这题不需要用到最小表示法以及括号表 ...

  9. hdu 1693 : Eat the Trees 【插头dp 入门】

    题目链接 题意: 给出一个n*m大小的01矩阵,在其中画线连成封闭图形,其中对每一个值为1的方格,线要恰好穿入穿出共两次,对每一个值为0的方格,所画线不能经过. 参考资料: <基于连通性状态压缩 ...

  10. HDU 1693 Eat the Trees

    第一道(可能也是最后一道)插头dp.... 总算是领略了它的魅力... #include<iostream> #include<cstdio> #include<cstr ...

随机推荐

  1. 【codeforces】【比赛题解】#869 CF Round #439 (Div.2)

    良心赛,虽然我迟了半小时233333. 比赛链接:#869. 呃,CF的比赛都是有背景的……上次是<哈利波特>,这次是<物语>…… [A]巧妙的替换 题意: Karen发现了石 ...

  2. nanosleep()

    函数原型 #include <time.h> int nanosleep(const struct timespec *rqtp, struct timespec *rmtp);   描述 ...

  3. unity 优秀开源项目

    ihaiu.GUIDRef (查看项目资源使用情况) http://blog.ihaiu.com/unity-GUIDRef Ihaiu.PoolManager (对象池) http://github ...

  4. Nginx - keepliave 相关知识点

    目录 - 1. 前言- 2. keepalive 介绍- 3. Nginx 与 keepalive 的关系    - 3.1 Nginx - keepalive_timeout    - 3.2 Ng ...

  5. SQL Server中常用全局变量介绍

    在SQL Server中,全局变量是一种特殊类型的变量,服务器将维护这些变量的值.全局变量以@@前缀开头,不必进行声明,它们属于系统定义的函数.下表就是SQL Server中一些常用的全局变量. 全局 ...

  6. 1975: [Sdoi2010]魔法猪学院

    k短路,只会写A*的飘过..priority_queue超空间差评!

  7. 多线程-TaskExecutor-使用Demo

    BasicExecute.java package com.jef.executeTest; public abstract class BasicExecute extends Thread { @ ...

  8. scrapy-redis 更改队列和分布式爬虫

    这里分享两个技巧 1.scrapy-redis分布式爬虫 我们知道scrapy-redis的工作原理,就是把原来scrapy自带的queue队列用redis数据库替换,队列都在redis数据库里面了, ...

  9. bzoj 3926 转换+广义后缀自动机

    思路:重点在于叶子节点只有20个,我们把叶子节点提到根,把20个trie图插入后缀自动机,然后就是算有多少个本质不同的字串. #include<bits/stdc++.h> #define ...

  10. Django实战(18):提交订单

    前面的内容已经基本上涵盖了Django开发的主要方面,我们从需求和界面设计出发,创建模型和修改模型,并通过scaffold作为开发的起点:在scaffold的基础上重新定制模板,并且通过Model类和 ...