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.
 
题目大意:用任意多个回路覆盖矩阵上的1.
思路:插头DP,参考IOI国家集训队论文,陈丹琦的《基于连通性状态压缩的动态规划问题》
 
代码(62MS)(普通推,一大堆无用状态):
 #include <cstdio>
#include <algorithm>
#include <cstring>
#include <iostream>
using namespace std;
typedef long long LL; const int MAXN = ; int mat[MAXN][MAXN];
LL dp[MAXN][MAXN][ << MAXN];
int n, m, T; LL solve() {
memset(dp, , sizeof(dp));
dp[][m][] = ;
for(int i = ; i <= n; ++i) {
for(int j = ; j < ( << m); ++j) dp[i][][j << ] = dp[i - ][m][j];
for(int k = ; k <= m; ++k) {
for(int state = ; state < ( << (m + )); ++state) {
int y = << k, x = y >> ;
if(mat[i][k]) {
if((state & x) && (state & y)) {
dp[i][k][state] = dp[i][k - ][state - x - y];
} else if((state & x) == && (state & y) == ) {
dp[i][k][state] = dp[i][k - ][state + x + y];
} else dp[i][k][state] = dp[i][k - ][state ^ x ^ y] + dp[i][k - ][state];
} else {
if((state & x) == && (state & y) == ) {
dp[i][k][state] = dp[i][k - ][state];
} else dp[i][k][state] = ;
}
}
}
}
return dp[n][m][];
} int main() {
scanf("%d", &T);
for(int t = ; t <= T; ++t) {
scanf("%d%d", &n, &m);
for(int i = ; i <= n; ++i)
for(int j = ; j <= m; ++j) scanf("%d", &mat[i][j]);
printf("Case %d: There are %I64d ways to eat the trees.\n", t, solve());
}
}

代码(0MS)(hash)(下面代码是lld的……):

 #include <iostream>
#include <algorithm>
#include <cstring>
#include <cstdio>
using namespace std;
typedef long long LL; const int MAXH = ;
const int SIZEH = ; struct hash_map {
int head[SIZEH];
int next[MAXH], state[MAXH];
LL val[MAXH];
int size; void init() {
memset(head, -, sizeof(head));
size = ;
} void insert(int st, LL sv) {
int h = st % SIZEH;
for(int p = head[h]; ~p; p = next[p]) {
if(state[p] == st) {
val[p] += sv;
return ;
}
}
state[size] = st; val[size] = sv; next[size] = head[h]; head[h] = size++;
}
} hashmap[]; int getB(int state, int i) {
return (state >> i) & ;
} void setB(int &state, int i, int val) {
state = (state & ~( << i)) | (val << i);
} int mat[][];
int n, m, T;
hash_map *cur, *last; void update(int state, LL val, int x, int y) {
int left = getB(state, y);
int up = getB(state, y + );
if(mat[x][y] == ) {
if(left == && up == ) cur->insert(state, val);
return ;
}
if(left == && up == ) {
if(x < n - && y < m - ) {
int newState = state;
setB(newState, y, );
setB(newState, y + , );
cur->insert(newState, val);
}
} else if(left == || up == ) {
if(x < n - ) {
int newState = state;
setB(newState, y, );
setB(newState, y + , );
cur->insert(newState, val);
}
if(y < m - ) {
int newState = state;
setB(newState, y, );
setB(newState, y + , );
cur->insert(newState, val);
}
} else {
int newState = state;
setB(newState, y, );
setB(newState, y + , );
cur->insert(newState, val);
}
} LL solve() {
cur = hashmap, last = hashmap + ;
last->init();
last->insert(, );
for(int i = ; i < n; ++i) {
int sz = last->size;
for(int k = ; k < sz; ++k) last->state[k] <<= ;
for(int j = ; j < m; ++j) {
cur->init();
sz = last->size;
for(int k = ; k < sz; ++k)
update(last->state[k], last->val[k], i, j);
swap(cur, last);
}
}
for(int k = ; k < last->size; ++k)
if(last->state[k] == ) return last->val[k];
return ;
} int main() {
scanf("%d", &T);
for(int t = ; t <= T; ++t) {
scanf("%d%d", &n, &m);
for(int i = ; i < n; ++i)
for(int j = ; j < m; ++j) scanf("%d", &mat[i][j]);
printf("Case %d: There are %lld ways to eat the trees.\n", t, solve());
}
}

最新文章

  1. 深入学习jQuery特性操作
  2. codevs2777 栅栏的木料
  3. 高性能Web系统设计方案(初稿目录),支持者进
  4. Flex Builder读书笔记(一)
  5. js中的类GET方法
  6. POJ 2516 Minimum Cost 最小费用流
  7. Lucene:信息检索与全文检索
  8. nrm —— 快速切换 NPM 源 (附带测速功能)
  9. 有用的javascript外部文件或其他外部文件引用
  10. Delphi Web Service和ISAPI的区别与联系 转
  11. Net Core WebApi单元测试
  12. python基础教程(七)
  13. jquery定时刷新数据
  14. 第一册:lesson 105.
  15. Spring 基于Session的创建实例
  16. MyBatis-CURD
  17. 阿里云主机Nginx下配置NodeJS、Express和Forever
  18. JAVA基础--重新整理(1)后版
  19. 使用livereload实现自动刷新
  20. [Sdoi2016]生成魔咒[SAM or SA]

热门文章

  1. Go转json数组
  2. PL/SQL 条件控制语句
  3. TinyMCE(富文本编辑器)在Asp.Net中的使用方法
  4. 【2018 ICPC南京网络赛 A】An Olympian Math Problem(数论题)
  5. 快速解决Kali 更新失败问题
  6. Linux 学习第二天
  7. jsonp 跨域只能调用一次ajax(无法多次调用或者循环调用)
  8. java对接微信支付
  9. AB PLC 编程之状态机
  10. python的爬虫代理设置