UVA 11795 Mega Man's Mission | 状态压缩
来源:互联网 发布:h3d动漫下载软件 编辑:程序博客网 时间:2024/05/07 15:28
类型:状态压缩DP
杰神的耐心指导加上参考这篇文章,总算把这状态压缩给AC了。
思路:
看到N〈= 16,可以明确是状态压缩。
采用二进制的思想,保存这每个状态时的打败该状态的敌人的方法数。
dp[011]可以由dp[001]和dp[010]推出。
解析:
数组p:保存每个机器人的武器所能打败的机器人;例:p[1] = (110),转为10进制就是p[1] = 6,表示机器人1的武器能打败第 2, 第3个机器人。
数组cot:用于保存已经打败的机器人的武器,还能打哪些机器人。例:cot[011] = (111),表示打败了第1,第2个机器人的状态下,所拥有的武器能打败第1,第2,第3个机器人。
数组dp:用于保存已经打败的机器人的方法数。例如dp[011] = 2,表示打败了第1,第2个机器人的方法数有2种。
*数组cot结合数组dp用于判断状态能否从之前的状态转移到当前状态。例如:dp[011] 可以由dp[010]转移,转移时要清楚知道cot[010]中是否存在武器可以打倒第1个敌人。存在则可以转移成dp[011],反之则不可。
AC代码:
#include <cstring>#include <cstdlib>#include <cstdio>#include <iostream>using namespace std;int cot[1<<17];long long dp[1<<17];char ch[20][20];int p[20];int n;int main(){ int t, cas = 0; cin>>t; while(t--) { scanf("%d", &n); memset(cot, 0, sizeof(cot)); memset(dp, 0, sizeof(dp)); int len = n; for(int i = 0;i <= n; i++) { scanf("%s",ch[i]); p[i] = 0; for(int j = 0; j < len; j++) { if(ch[i][j] == '1') p[i] |= (1<<j); } } cot[0] = p[0]; int sta = (1<<n) - 1; for(int i = 1;i <= sta; i++) { cot[i] = p[0]; for(int j = 0;j < n; j++) { int tmp = 1 << j; if((i >> j) & 1) cot[i] |= p[j+1]; } } //get the dp dp[0] = 1; for(int i = 1;i <= sta; i++) { for(int j = 0; j < n; j++) { int tmp = 1 << j; if( (cot[i^tmp] >> j) & 1) dp[i] += dp[i^tmp]; } } printf("Case %d: %lld\n", ++cas,dp[sta]); } return 0;}
0 0
- UVa 11795 - Mega Man's Mission 状态压缩dp
- UVa 11795 - Mega Man's Mission(状态压缩dp)
- UVA 11795 - Mega Man's Mission(状态压缩dp)
- UVa:11795 Mega Man's Mission(状态压缩)
- UVA 11795 Mega Man's Mission(状态压缩DP)
- UVA 11795 Mega Man's Mission | 状态压缩
- UVA - 11795 Mega Man's Mission 状态压缩DP
- UVA 11795 Mega Man's Mission(状态压缩DP)
- UVA 11795-Mega Man’s Mission(状态压缩DP)
- UVA 11795 - Mega Man's Mission(状态压缩DP)
- UVa 11795 - Mega Man's Mission(集合DP 状态压缩)
- uva 11795 Mega Man's Mission(动态规划-状态压缩DP)
- UVa 11795 Mega Man's Mission(动态规划-状态压缩DP)
- UVA 11795 Mega Man's Mission
- UVA 11795 Mega Man's Mission
- UVA 11795 Mega Man's Mission
- UVA 11795 Mega Man's Mission
- UVA - 11795 Mega Man's Mission
- CF (Round#256) A. Rewards
- Nginx安全配置研究
- poj 1328 Radar Installation
- 常用的正则表达式
- Struts2中"There is no Action mapped for namespace / and action name"
- UVA 11795 Mega Man's Mission | 状态压缩
- 视频字幕制作方法
- 10大iOS开发者最喜爱的类库
- HTML5学习笔记
- hdu 3564 Another Lis
- 水性环氧地坪漆施工与油性环氧地坪漆施工有区别吗?
- Linux 脚本随机
- hanganalyze 3 分析总结(测)
- codeforces #256 A. Rewards