UVa1252 Twenty Questions

        传说中的状态压缩DP。。把某一类状态压缩成二进制位。dp(s,a)代表的是提问集合为s,确认具备状态集合为a的情况下,仍需最少提问次数。这个解法妙在两个地方,第一使用int表示一个集合,一个二进制位表示一个元素,这个大家都知道;第二是很好地概括了边界条件,分析出了在什么情况下不需要进一步提问,设置了一个假象的物体,其实这个物体可以指代所有存在的物体。。在状态转移的过程中,min函数是为了最优化提问,不问不必要的问题,max函数是为了考虑所有物体中,可能的最坏情况。注意到求解过程中会计算一些不存在的状态,但是好在不存在的状态不会影响结果。

        但是这道题还是有让我搞不明白的地方,提问的时候顺序应该是无关紧要的,但是如果我人为规定了提问顺序,就会WA。(见代码注释部分)


#include <iostream>    
#include <stdio.h>    
#include <cmath>    
#include <algorithm>    
#include <iomanip>    
#include <cstdlib>    
#include <string>    
#include <memory.h>    
#include <vector>    
#include <queue>    
#include <stack>    
#include <map>  
#include <set>  
#include <ctype.h>    
#define INF 10000000
#define ll long long
#define min3(a,b,c) min(a,min(b,c))
#define max3(a,b,c) max(a,max(b,c))
#define MAXN 100010

using namespace std;  

int obj[130];
int m,n;

int strtobit(char* str){
	int num=0;
	for(int i=0;i<m;i++){
		if(str[i]=='1')num+=(1<<(m-i-1));
	}
	return num;
}

int dp[3000][3000];

int fun(int s,int a){
	if(dp[s][a]!=-1)return dp[s][a];
	int cnt=0;
	for(int i=1;i<=n;i++){
		if( (obj[i]&s)==a)cnt++;
	}
	if(cnt<=1){
		dp[s][a]=0;
		return 0;
	}
	int re=INF;
	
	int k=m;
	/*for(k=0;k<m;k++){
		if( s&(1<<k) ){
			break;
		}
	}*/
	
	while(k--){
		if( s&(1<<k) ) continue;
		int t=1<<k;
		re=min( re , max(fun(s^t,a^t),fun(s^t,a))+1 );
	}

	dp[s][a]=re;
	return re;
}

int main(){
	while(cin>>m>>n){
		if(m==0&&n==0)break;
		memset(dp,-1,sizeof(dp));
		
		char str[15];
		for(int i=1;i<=n;i++){
			cin>>str;
			obj[i]=strtobit(str);
		}
		
		cout<<fun(0,0)<<endl;
	}
	return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
As an AI language model, I don't have personal experiences. However, I can provide you with an article about the party's twenty great experiences. Parties are a great way to let loose and have fun with friends and family. There are so many things you can do at a party that can make it a memorable experience. Here are twenty great experiences that can make any party an unforgettable one. 1. Have a theme: Having a theme for the party can make it more exciting and fun. It can be anything from a 70s disco theme to a Hawaiian luau theme. The possibilities are endless. 2. Decorate the space: Decorating the space can make it more festive and inviting. Balloons, streamers, and banners are just some of the items that can be used to decorate the space. 3. Play games: Playing games can be a great way to break the ice and get everyone involved. Some popular games include charades, Pictionary, and beer pong. 4. Have a photo booth: Setting up a photo booth with fun props can be a great way to capture memories and create some hilarious photos. 5. Have a karaoke session: Karaoke can be a great way to get everyone singing and dancing. It’s a great way to break the ice and get everyone involved. 6. Have a dance-off: A dance-off can be a great way to get everyone moving and grooving. It’s a great way to showcase your dance moves and have some fun. 7. Have a potluck: A potluck can be a great way to get everyone involved in the party planning. Everyone can bring their favorite dish to share with everyone else. 8. Have a piñata: A piñata can be a great way to add some excitement to the party. It’s a great way to get everyone involved and have some fun. 9. Have a movie night: A movie night can be a great way to wind down after a long day. It’s a great way to relax and enjoy some good movies with friends and family. 10. Have a bonfire: A bonfire can be a great way to enjoy the outdoors and roast marshmallows. It’s a great way to relax and enjoy some good company. 11. Have a scavenger hunt: A scavenger hunt can be a great way to get everyone involved and have some fun. It’s a great way to explore the area and find hidden treasures. 12. Have a talent show: A talent show can be a great way to showcase everyone’s talents. It’s a great way to have some fun and see what everyone is capable of. 13. Have a DIY station: A DIY station can be a great way to get creative and make something fun. It’s a great way to get everyone involved and have some fun. 14. Have a game night: A game night can be a great way to get everyone involved and have some fun. It’s a great way to play some classic board games or try out some new ones. 15. Have a costume party: A costume party can be a great way to get everyone involved and have some fun. It’s a great way to dress up and be someone else for a night. 16. Have a cocktail-making contest: A cocktail-making contest can be a great way to get everyone involved and have some fun. It’s a great way to showcase your mixology skills and have some fun. 17. Have a dessert-making contest: A dessert-making contest can be a great way to get everyone involved and have some fun. It’s a great way to showcase your baking skills and have some fun. 18. Have a paint night: A paint night can be a great way to get creative and have some fun. It’s a great way to paint something fun and enjoy some good company. 19. Have a board game tournament: A board game tournament can be a great way to get everyone involved and have some fun. It’s a great way to play some classic board games or try out some new ones. 20. Have a DIY cocktail station: A DIY cocktail station can be a great way to get creative and make something fun. It’s a great way to get everyone involved and have some fun. In conclusion, there are so many great experiences that can make any party an unforgettable one. Whether it’s playing games, having a karaoke session, or having a costume party, there are so many ways to have fun and create great memories with friends and family.

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值