[Shape pressure dp] Twenty questions about UVA1252

The main idea of ​​the title: There are n items, each item has m features, you can query the features, the result of the query is to know whether an object contains the feature, and all the items should be distinguished (the features of the n items are all different from each other) at least how many inquiries are required?

Considering the representation of the state, a set s1 can be used to represent the feature set that has been queried, and s2 to represent the feature set that the object has been determined to have. In each state, consider which feature to ask for. First of all, clarify a question. In a state, it is up to you to decide which feature to ask, but it is not necessarily the answer you get. For example, if you ask a feature that only one object has in all objects, if you answer yes, you will be hit with one hit. But the worse case is to answer no, then you have to keep looking down. So in this state, imagine that the other party is deliberately embarrassing you, and answer the answer that makes you ask more times. But choosing to ask what is up to you is what it means to take min and max. When cnt[s1][s2]=1 is the boundary, then there is no need to ask, because only one object is legal.

The details are more difficult to deal with. cnt[s1][s2] represents the number of objects containing the feature set s2 obtained by querying the set s1. This preprocessing began to be unexpected. I feel that AC is because it follows the idea of ​​​​the problem solution, and many details have to be considered again. If there is no such an object in the s1, s2 state, that is, the state is illegal. In this case, if cnt[s1][s2]=0, you can judge whether the status is legal or not by the value of cnt. If it is illegal, it will return 0, and this status will be ignored when taking max. Then you can think of it along the way, what if there are two transfer states that are not legal? First of all, if you go to the transition step, cnt[s1][s2] must be >=2, and cnt[s1][s2]>=1 indicates that this state is legal. Then the feature of the query, the target object either has or does not have, so one of the states of answering yes and no must be legal

#include<iostream>
#include<cstdio>
#include<cstring>
#define INF (20010)
using namespace std;
int m,n,A[2020],cnt[(1<<11)+5][(1<<11)+5],dp[(1<<11)+5][(1<<11)+5];
char s[2020];
bool vis[(1<<11)+5][(1<<11)+5];
int Dfs(int s1,int s2){
	int &ans=dp[s1][s2];
	if (vis[s1][s2]) return ans;
	vis [s1] [s2] = 1;
	if (cnt[s1][s2]<=1) return ans=0;
	ans=INF;
	int i;
	for (i=1;i<=m;i++)
		if (!((1<<(i-1))&s1))
			ans=min(ans,max(Dfs(s1|(1<<(i-1)),s2|(1<<(i-1))),Dfs(s1|(1<<(i-1)),s2))+1);
	return ans;
}
void Work(){
	int i, j, len;
	memset(A,0,sizeof(A));
	memset(cnt,0,sizeof(cnt));
	memset(vis,0,sizeof(vis));
	for (i=1;i<=n;i++){
		scanf("%s",s);
		len = strlen (s);
		for (j=0;j<len;j++) if (s[j]-'0') A[i]|=(1<<j);
	}
	for (i=0;i<=(1<<m)-1;i++)
		for (j=1;j<=n;j++)
			cnt[i][i&A[j]]++;
	printf("%d\n",Dfs(0,0));
}
int main(){
	while (cin>>m>>n){
		if (!m&&!n) break;
		Work();
	}
}


Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325935799&siteId=291194637