poj 3254
2017-02-11 15:56
218 查看
链接:http://poj.org/problem?id=3254
分析:一道经典的状压dp,核心是将排列方式看做01串,用十进制表示
代码:
分析:一道经典的状压dp,核心是将排列方式看做01串,用十进制表示
代码:
/*2017-2-11 15:51 author:lian */ #include <iostream> #include <algorithm> #include <cstdlib> #include <cstdio> #include <cstring> using namespace std; typedef long long ll; const int MAX = 100000000; const int N = 15; int probable[1<<N],mapp ,dp [1<<N]; bool judge_probale(int i); //保证没有相邻的1 bool judge_two(int i,int j);//保证两个排列没有相邻的1 int main() { int n,m,i,j,k; while(cin>>n>>m) { int len = 0; memset(mapp,0,sizeof(mapp)); for(i=1; i<=n; i++) for(j=1; j<=m; j++) { cin>>k; mapp[i] += (k<<(j-1));//构造相应的十进制 } memset(dp,0,sizeof(dp)); for(i=0; i<(1<<m); i++) if(judge_probale(i)) { if((i&mapp[1]) == i) dp[1][i] = 1; probable[len++] = i; } for(i=1; i<=n; i++) for(j=0; j<len; j++) //当前的状态 { for(k=0; k<len; k++) //上一行的状态 { if(judge_two(probable[j],probable[k]) == false) continue; if((probable[j]&mapp[i]) != probable[j]) break; dp[i][probable[j]] += dp[i-1][probable[k]]; } } int ans = 0; for(j=0; j<len; j++) { ans += dp [probable[j]]; ans %= MAX; } cout<<ans<<endl; } return 0; } bool judge_probale(int i) { if(i&(i<<1)) return false; return true; } bool judge_two(int i,int j) { if(i&j) return false; return true; }
相关文章推荐
- DataGridView中将column与数据库表对应的属性
- 【dev Cpp新手请进】dev导入ege图形库
- MySQL数据库MyISAM与InnoDB存储引擎的比较
- MVP+Dagger2+Rxjava+Retrofit+GreenDao 开发的小应用,包含新闻、图片、视频3个大模块,代码封装良好
- mysql数据库服务提示This function has none of DETERMINISTIC的解决方法
- Linux网络预备知识
- android post请求接口demo
- tensorflow的安装教程(基于Ubuntu)
- 美国队长3(内战)
- 【jzoj4264】【tty妹子的函数】
- struts.xml配置问题
- thinkphp-三元运算
- POJ 3713 Transferring Sylla (三连通图)
- VS 2013 with update 5 编译程序出现A task was cancel
- 第二章 Spring MVC入门
- 记录一个breed服务器地址
- 洛谷 1449——后缀表达式(线性数据结构)
- HDU1856:More is better(并查集)
- 图像卷积与滤波的一些知识点
- 389. Find the Difference | 找字符串中的不同