题意:有n件物品,每件物品有m个特征,可以对特征进行询问,询问的结果是得知某个物体是否含有该特征,要把所有的物品区分出来(n个物品的特征都互不相同),

最小需要多少次询问?

析:我们假设心中想的那个物体为W,首先知道的是,同一个特征不用问多次,所以首先用一个集合s表示已经问的特征,在这里面有的是W具备的,有的不是,

所以再加一个集合a表示W所以具备的特征,并且a是s的子集。d(s, a) 表示问了特征集s,其中已经确认W所具备的特征集为a时,还要询问的最少次数。

再加一个状态,继续询问呗,所以次数就是max{d(s+{k}, a+{k}), d(s+{k}, a)}+1,其中k是下一个特征。

边界就是:如果一个物体满足 具备a中的所有特征,但是不具备s-a中的所有特征。这就是边界。返回0.

代码如下:

#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <cstdio>
#include <string>
#include <cstdlib>
#include <cmath>
#include <iostream>
#include <cstring>
#include <set>
#include <queue>
#include <algorithm>
#include <vector>
#include <map>
#include <cctype>
#include <stack>
#include <sstream>
using namespace std ; typedef long long LL;
typedef pair<int, int> P;
const int INF = 0x3f3f3f3f;
const double inf = 0x3f3f3f3f3f3f;
const double PI = acos(-1.0);
const double eps = 1e-8;
const int maxn = 130 + 5;
const int mod = 1e9 + 7;
const char *mark = "+-*";
const int dr[] = {-1, 0, 1, 0};
const int dc[] = {0, 1, 0, -1};
int n, m;
inline bool is_in(int r, int c){
return r >= 0 && r < n && c >= 0 && c < m;
}
inline int Min(int a, int b){ return a < b ? a : b; }
inline int Max(int a, int b){ return a > b ? a : b; }
int d[1<<11][1<<11];
int st[maxn];
char ss[15]; void init(int i){
for(int j = 0; j < m; ++j)
if(ss[j] == '1') st[i] |= (1<<j);
} int dp(int s, int a){
int &ans = d[s][a];
if(ans >= 0) return ans;
int cnt = 0;
for(int i = 0; i < n; ++i)
if((st[i] & s) == a) ++cnt;
if(cnt <= 1) return ans = 0;//如果这样的物体只有1个,或者没有就可以区别了 ans = INF;
for(int i = 0; i < m; ++i){
if(s & (1<<i)) continue; ans = Min(ans, Max(dp(s|(1<<i), a|(1<<i)), dp(s|(1<<i), a))+1);//加一个特征
}
return ans;
} int main(){
while(scanf("%d %d", &m, &n) == 2){
if(!m && !n) break;
memset(st, 0, sizeof(st));
for(int i = 0; i < n; ++i){
scanf("%s", ss);
init(i);
}
memset(d, -1, sizeof(d));
int ans = dp(0, 0);
printf("%d\n", ans);
}
return 0;
}

  

最新文章

  1. Linux用户态和内核态
  2. JDK环境变量详细讲解
  3. angularjs之自己定义指令篇
  4. hibernate中 inverse的用法(转载)
  5. 保护隐私:清除cookie、禁用cookie确保安全【分享给身边的朋友吧】
  6. hpuoj回文串问题(manacher+kmp)
  7. echo json数据给ajax后, 需要加上exit,防止往下执行,带上其他数据,到时ajax失败
  8. java中json数据生成和解析(复杂对象演示)
  9. 蓝桥杯-李白打酒-java
  10. easyui1.2.6 validate输入框验证在火狐下的一个bug
  11. Event Sourcing pattern
  12. 基于Python的Web应用开发实践总结
  13. python——函数
  14. mongodb的几种运算符
  15. WPF Binding学习(三)
  16. bzoj4481非诚勿扰(期望dp)
  17. TensorFlow学习笔记:保存和读取模型
  18. Python Matplot中文显示完美解决方案
  19. 使用redis
  20. 洛谷P3227 切糕

热门文章

  1. hdu 1513 &amp;&amp; 1159 poj Palindrome (dp, 滚动数组, LCS)
  2. 宏buf_pool_t
  3. EXT 数据按F12,F11 显示问题
  4. 【Java集合框架】规则集--Set
  5. 【转载】两个Web.config中连接字符串中特殊字符解决方案
  6. Mysql 的存储过程和存储函数
  7. [Everyday Mathematics]20150207
  8. Minimax Triangulation
  9. [Tommas] 测试用例覆盖率(三)
  10. PhoneGap,Cordova[3.5.0-0.2.6]:利用插件Cordova-SQLitePlugin来操作SQLite数据库