UVA - 1252 Twenty Questions(状态压缩&记忆化搜索)
2016-03-02 22:34
579 查看
题目链接:UVA - 1252 Twenty Questions
dp(i, j) = c
i表示已经询问过的特征的集合
j表示已经确定我选的物体具有的特征的集合
那么显然的,j一定是i的子集。
c表示当前状态还需询问的次数
max里的两项分别为,所询问的是我具有的特征 | 所询问的不适我具有的特征。
因为要保证可以百分百确定,所以选择最坏情况中的最好情况。这是为什么用max的原因。
当符合(满足j的所有特征且不满足i除j外的其他特征)的物体小于等于1个时,可以停止询问,因为可以确定结果了。可以对这个计数过程进行预处理,提高效率。
状态重复太多,所以要用到记忆化搜索。
题意
有n(0思路
从m的数据范围以及题意,很容易可以想到状态压缩,用二进制位来表示集合。dp(i, j) = c
i表示已经询问过的特征的集合
j表示已经确定我选的物体具有的特征的集合
那么显然的,j一定是i的子集。
c表示当前状态还需询问的次数
[code]dp(i, j) = 1 + min(max(dp(i|(1<<k), j|(1<<k)), dp(i|(1<<k), j))) k为所有未询问的特征。
max里的两项分别为,所询问的是我具有的特征 | 所询问的不适我具有的特征。
因为要保证可以百分百确定,所以选择最坏情况中的最好情况。这是为什么用max的原因。
当符合(满足j的所有特征且不满足i除j外的其他特征)的物体小于等于1个时,可以停止询问,因为可以确定结果了。可以对这个计数过程进行预处理,提高效率。
状态重复太多,所以要用到记忆化搜索。
代码
[code]#include <iostream> #include <algorithm> #include <cstring> #include <cstdio> #include <cstdlib> #include <vector> #include <cmath> #include <map> using namespace std; #define LL long long const int MOD = 1000000007; const int N = 200; const int M = 12; int cnt[1<<M][1<<M]; int p ; int dp[1<<M][1<<M]; void init(int n, int m) { for(int s=0; s<(1<<m); s++) for(int i=0; i<n; i++) cnt[s][p[i]&s]++; } int dfs(int s, int a, int m) { if(dp[s][a] != 0x3f3f3f3f) return dp[s][a]; if(cnt[s][a] <= 1) return 0; for(int i=0; i<m; i++) { if(s & (1<<i)) continue; dp[s][a] = min(dp[s][a], max(dfs(s|(1<<i),a,m), dfs(s|(1<<i),a|(1<<i),m))); } return dp[s][a]+=1; } int main() { int n, m; char t[12]; while(~scanf("%d%d", &m, &n) && m && n) { memset(cnt, 0, sizeof(cnt)); memset(dp, 0x3f, sizeof(dp)); for(int i=0; i<n; i++) { scanf("%s", t); p[i] = 0; for(int j=0; j<m; j++) if(t[j] == '1') p[i] |= 1<<j; } init(n, m); printf("%d\n", dfs(0, 0, m)); } }
相关文章推荐
- IOS面试题总结--UIView和CALayer的层级关系和区别
- ui-router(三)controller与template
- Hibernate HQL之org.hibernate.QueryException:could not resolve
- leetcode之Repeated DNA Sequences
- cf 625 Guest From the Past (买饮料_锻炼思维的题)
- iOS学习----------AFNetworking(3)request创建《post请求》
- easyui主界面生成分析1
- iOS_SN_BlueTooth( 一)蓝牙相关基础知识
- Netty实现带UI客户端服务器聊天功能
- 导入opencv.highgui.Highgui会报错
- ui-router详解(二)ngRoute工具区别
- c3p0 配置连接MySQL异常: java.sql.SQLException: Connections could not be acquired from the underlying datab
- POJ 3368 Frequent Values(RMQ)
- UITableview性能优化方案 iOS
- UVA 11995(p186)----I Can Guess the Data Structure!
- Handler消息传递机制(二)Handler,Loop,Message,MessageQueue的工作原理
- 线程
- hdu 3732 Ahui Writes Word 【多重背包】、好题
- ui-router路由控制器(一)
- css之marquee,让你的文字跳起来