HDU 1693 Eat the Trees 插头DP
来源:互联网 发布:java软件开发硬件环境 编辑:程序博客网 时间:2024/05/12 08:33
求一个棋盘不定量条回路覆盖方案数。
是不是插头dp的入门题呢。。
对于正在决策的格子有几种情况。
1. 障碍
1) 有插头:显然方案数为0。
2) 无插头:方案数不变。
2. 平地
1) 有2个插头:当前决策的格子已经有路通过,因此下个状态是无插头。
2) 有1个插头:当前决策的格子有路通过,但不确定向右还是向下,因此下个状态是1个插头。
3) 有0个插头:当前决策的格子暂时没有路通过,要通一条路,因此下个状态是2个插头。
#include <cstdio>#include <cstring>#define rep(i,j,k) for(int i=j;i<k;++i)#define FOR(i,j,k) for(int i=j;i<=k;++i)const int N = 13, M = 1 << N;typedef long long ll;ll dp[N][N][M];int n, m, mp[N][N];int main() { int t; scanf("%d", &t); FOR(kase,1,t) { scanf("%d%d", &n, &m); FOR(i,1,n) FOR(j,1,m) scanf("%d", &mp[i][j]); memset(dp, 0, sizeof dp); dp[0][m][0] = 1; FOR(i,1,n) { rep(k,0,1<<m) dp[i][0][k << 1] = dp[i - 1][m][k]; FOR(j,1,m) rep(k,0,1<<m+1) { int x = 1 << j - 1, y = 1 << j; if (!mp[i][j]) if (k & x || k & y) dp[i][j][k] = 0; else dp[i][j][k] = dp[i][j - 1][k]; else if (((k & x) << 1) == (k & y)) // 2.1, 2.3, 插头状态相同时 dp[i][j][k] = dp[i][j - 1][k ^ x ^ y]; // 取相反的状态 else // 2.2 dp[i][j][k] = dp[i][j - 1][k] + dp[i][j - 1][k ^ x ^ y]; } } printf("Case %d: There are %I64d ways to eat the trees.\n", kase, dp[n][m][0]); } return 0;}
Eat the Trees
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 3534 Accepted Submission(s): 1729
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
26 31 1 11 0 11 1 11 1 11 0 11 1 12 41 1 1 11 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.
0 0
- HDU-1693 Eat the Trees 插头DP
- HDU 1693 Eat the Trees 插头DP
- HDU 1693 Eat the Trees 插头DP
- hdu 1693 Eat the Trees 插头dp
- hdu 1693 Eat the Trees 插头dp
- hdu 1697 Eat the Trees(插头dp)
- HDU 1693(Eat the Trees-不用匹配的插头Dp)
- hdu 1693 Eat the Trees 轮廓线 插头dp
- hdu 1693 Eat the Trees 插头dp入门题
- hdu 1693 Eat the Trees (插头dp入门)
- HDU 1693 Eat the Trees (插头DP,闭合路径)
- HDU - 1693 Eat the Trees(多回路插头DP)
- HDU 1693 Eat the Trees (插头dp)
- hdu 1693 eat the trees----插头dp入门
- hdu 1693 Eat the Trees (基础插头dp)
- 【插头DP】Eat the trees
- HDU 1693 Eat the Trees(插头DP,多条回路)
- hdu1693 Eat the Trees (插头dp)
- ios版本更新
- mongodb地理信息应用
- mac 对手机抓包命令.
- 可以彻底从你的mac上卸载掉node.js
- Java——private,protected,public
- HDU 1693 Eat the Trees 插头DP
- Oracle 禁止操作系统认证登录
- PAT (Basic Level)1027. 打印沙漏
- JAVA反射机制的作用是什么
- 带闪光效果的自定义TextView
- RAC 环境中最常见的 5 个数据库和/或实例性能问题 (文档 ID 1602076.1)
- mysql 存储过程、存储过程嵌套、自定义函数代码示例
- 随手收藏,python中的各种类型转换
- hdu2270 How Many Friends Will Be Together With You