poj 3254
来源:互联网 发布:淘宝搜不到以前的订单 编辑:程序博客网 时间:2024/06/09 16:38
题意:
有很多的格子,里面分为可以放牧(1)和不可以放牧(0),不可以在相邻的格子里面放牧。
问总共有几种方案(1头牛也不放,也算一种方案)。
思路:
这是学习状态压缩dp的第一道题,具体有几个步骤。
1.把所有可能的初始状态存在一个数组里面。(2进制)
2.把有相邻格子的状态去掉。
3.把能放牧与否冲突的去掉
4.把上一行和这一行相邻的去掉
5.按行计算,把所有从上一行的能到达此行此格子的状态全都加到dp数组里面。
6.计算最后一行的所有状态方案和。
(第一次做状压dp,感觉就是一个搜索,把所有能预处理的剪枝都先处理好了,然后再解决冲突,最后的出来的就是结果,其中经典的就是位运算的高效性了。不知道这个想法对不对)
Code:
#include<cstdio>#include<cstdlib>#include<cmath>#include<cctype>#include<cstring>#include<algorithm>#include<iostream>#include<string>#include<bitset>#include<vector>#include<queue>#include<stack>#include<set>#include<map>#include<list>//#define TEST#define LL long long#define Mt(f, x) memset(f, x, sizeof(f));#define rep(i, s, e) for(int i = (s); i <= (e); ++i)#define xep(i, n) for(int i = 0; i < (n); ++i)#define dep(i, s, e) for(int i = (s); i >= (e); --i)#ifdef TEST #define See(a) cout << #a << " = " << a << endl; #define debug(a, s, e) rep(_i, s, e) {cout << a[_i] << ' ';} cout << endl; #define debug2(a, s, e, ss, ee) rep(i_, s, e) {debug(a[_i], ss, ee);}#else #define See(a) {} #define debug(a, s, e) {} #define debug2(a, s, e, ss, ee) {}#endifconst int MAX = 2e9;const int MIN = -2e9;const double eps = 1e-8;const double PI = acos(-1.0);using namespace std;const int N = 12;const int Mod = 100000000;int n, m;int a[N + 5];int sta[1 << N], num;int dp[N + 2][1 << N];inline bool isL(int x){ return !(x & (x >> 1));}inline bool fit(int i, int k){ return !(a[i] & k);}void init(){ num = 0; xep(i, 1 << m) { if(isL(i)) { sta[num++] = i; } } See(num);}int main(){ while(~scanf("%d%d", &n, &m)) { xep(i, n) { a[i] = 0; xep(k, m) { int d; scanf("%d", &d); if(!d) { a[i] |= 1 << k; } } } init(); Mt(dp, 0); xep(i, num) { dp[0][i] = fit(0, sta[i]); } rep(i, 1, n - 1) { xep(k, num) { if(!fit(i, sta[k])) continue; xep(u, num) { if(!fit(i - 1, sta[u])) continue; if(sta[k] & sta[u]) continue; dp[i][k] = (dp[i][k] + dp[i - 1][u]) % Mod; } } } See(num); int ans = 0; xep(i, num) { ans = (ans + dp[n - 1][i]) % Mod; } printf("%d\n", ans); } return 0;}
0 0
- POJ 3254
- poj 3254
- POJ 3254
- poj 3254
- poj 3254
- poj 3254
- poj 3254
- poj 3254
- poj-3254
- poj 3254
- poj 3254
- poj 3254
- poj 3254
- POJ 3254
- poj 3254
- poj 3254
- POJ 3254
- [状态压缩DP] Poj 3254, Poj 1185
- Linux mysql连接问题
- 联想机房同传日记——2014/09/17
- 我的开源夏令营--刚刚过去的两个月
- Swift之协议(Protocols)
- Android开发之去掉标题栏的三种方法,推荐第三种
- poj 3254
- NYOJ 题目837 Wythoff Game(威佐夫博奕必败点,水题)
- JavaScript跨域问题探究一
- 算法学习(一)
- cocos2dx简单弹窗实现
- 交互设计
- 举例说同步异步阻塞非阻塞机制
- Codeforces Round #266 (Div. 2)D(DP)
- 链表的一些基本操作:动态链表、插入和删除等