NOJ1091Black Vienna——人工智能?No...No...No...二分+枚举

44 篇文章 1 订阅

Black Vienna

时间限制(普通/Java):1000MS/3000MS          运行内存限制:65536KByte
总提交:49            测试通过:14

描述

This problem is based on the game of Black Vienna. In this version there are three players and 18 cards labeled A-R. Three of the cards are set aside (hidden) and form the "Black Vienna" gang. The remaining cards are shuffled and dealt to the players so that each player has 5 cards. Players never reveal their cards to each other. There is a separate deck of "interrogation cards" which contain three distinct letters in ascending order, like ACG or BHR.  Turns rotate through players 1, 2, and 3.  On each player's turn, that player selects an interrogation card, puts it face up in front of another player, and that other player must indicate the total number of these cards being held, without saying which ones.  All players see the result of the "interrogation". The play continues until a player deduces the three cards in the "gang".     
For example, suppose the cards are distributed as follows, and the game then proceeds:
Player 1: DGJLP; Player 2: EFOQR; Player 3: ACHMN;  Gang: BIK
Turn 1:  Player 1 interrogates player 2 with BJK; answer 0
Turn 2:  Player 2 interrogates player 3 with ABK; answer 1
Turn 3:  Player 3 interrogates player 2 with DEF; answer 2
Turn 4:  Player 1 interrogates player 2 with EIL; answer 1
Turn 5:  Player 2 interrogates player 3 with FIP; answer 0
Turn 6:  Player 3 interrogates player 1 with GMO; answer 1
Turn 7:  Player 1 interrogates player 2 with OQR; answer 3
Turn 8:  Player 2 interrogates player 3 with ADQ; answer 1
Turn 9:  Player 3 interrogates player 1 with EGJ; answer 2
In fact, the game does not need to get to turn 9.  With enough thought, player 1 can deduce after turn 8 that the gang is BIK.  It is your job to analyze records of games and deduce the earliest time that the gang could be determined for sure.

输入

The input will consist of one to twelve data sets, followed by a line containing only 0.   
The first line of a dataset contains the number, t, of turns reported, 2 ≤ t ≤ 15.  
The next line contains four blank separated strings for the hands of players 1, 2, and 3, followed by the cards for the gang.
The remaining t lines of the data set contain the data for each turn in order.  Each line contains three blank separated tokens:  the number of the player interrogated, the string of interrogation letters, and the answer provided.
All letter strings will contain only capital letters from A to R, in strictly increasing alphabetical order.  The same interrogation string may appear in more than one turn of a game.

输出

There is one line of output for each data set.  The line contains the single character "?" if no player can be sure of the gang after all the turns listed.  If a player can determine the gang, the line contains the earliest turn after which one or more players can be sure of the answer.

样例输入

9
DGJLP EFOQR ACHMN BIK
2 BJK 0
3 ABK 1
2 DEF 2
2 EIL 1
3 FIP 0
1 GMO 1
2 OQR 3
3 ADQ 1
1 EGJ 2
3
ABCDE FGHIJ KLMNO PQR
3 BKQ 1
1 ADE 3
2 CHJ 2
0

样例输出

8
?

题目来源

ACM Mid-Central Regional 2009


分析:很好的一道题目。解决了就像人工智能一样。其实细细想想就是枚举。至于更加简洁就是加上了二分法减小复杂度。

具体题解(引用自《ACM国际大学生程序设计竞赛题目与解读》)

>>>>>>>>>>

for each player
枚举3张gang,继续枚举另外2人手里的牌。
只要其中任意一种情况满足询问及答案,那么那3张gang就是可能的,
如果桌上那三张牌有且只有1种可能,则该player能确定gang。

总的复杂度:O(logN*3*C(13,3)*C(10,5)*N)
(N为询问次数)
**************************

#include<iostream>
using namespace std;
#define MAX(x, y) (x) > (y) ? (x) : (y)

const int PLAYERS = 3, MAX_TURNS = 15, HAND = 5, HID = 3, UNK = HID+HAND*(PLAYERS-1); // 13
int turns;

char quest[MAX_TURNS][3+1], // 每次询问的3张牌
	 hand[PLAYERS + 1][5+1], // 实际每人(0 1 2)的牌,hand[3][]代表gang
	 maybe[PLAYERS + 1][HAND+1]; // 枚举另外2个人每人的5张牌和gang的3张牌

char notHave[UNK]; // letters not in one player's hand 某一位玩家没有的13张牌
int who[MAX_TURNS],  // 被询问的人
	matches[MAX_TURNS], // 匹配数
	used[PLAYERS+1]; // amount of maybe hand filled
int solved;  // max turns needed for current player for comb. so far

void solve(int i,char unk[]);
int countConsistent(char choice[][HAND+1]);
int countDups(char a1[],char a2[]);

int main()
{
	//freopen("in.txt","r",stdin);
	int i,j;
	int bestSolved;
	while(scanf("%d",&turns)!=EOF && turns>0)
	{
		for(i=0;i<=PLAYERS;i++)
			scanf("%s",&hand[i]); // 每人实际的牌 + gang(hand[3][])
		int t;
		for(t=0;t<turns;t++)
		{
			scanf("%d",&who[t]); // 1 2 3 
			who[t]--;  // internal 0 based
			scanf("%s",&quest[t]);
			scanf("%d",&matches[t]);
		}
		bestSolved=MAX_TURNS; // 15
		int p;
		char notHave[18-5+1]; // 某位玩家没有的13张牌
		for(p=0;p<PLAYERS;p++) // 对每一位玩家判断
		{
			memset(notHave,'\0',sizeof(notHave));
			for(j=0;j<=PLAYERS;j++){
				if(j!=p)
					strcat(notHave, hand[j]); // 构造玩家p没有的13张牌赋给notHave[]
			}
			strcpy(maybe[p],hand[p]); // player knows own 玩家p知道的牌的情况,自己的肯定是知道的,其他的需要枚举 !!!
			used[p] = HAND; // used[j] = 5/3表示玩家j或者gang(0 1 2 3 4 / 0 1 2)的牌已经确定(这里自己的牌显然是确定的)
			solved = 0; // after recursion max turns to eliminate a maybe 递归最大化消除枚举的错误的可能

			solve(0, notHave); // 判断,如果成功将改变solved的值

			memset(maybe[p],'\0',sizeof(maybe[p]));
			used[p]=0;
			if(solved<bestSolved)
				bestSolved=solved;
		}
		if(bestSolved<turns)
			printf("%d\n",bestSolved+1);
		else
			printf("?\n");
	}

	return 0;
}

// accumulate combinations recursively, check when one is complete
void solve(int i,char notHave[])
{
	if(i == (18-5)){ // p玩家没有的13张牌都已经枚举了,判断是否有且只有一种情况满足条件
		if (maybe[PLAYERS][0] == notHave[i-3]) return; //match end gang char // maybe[3][0] == notHave[i-3](gang!) 最后才成功了
		solved = MAX(solved, countConsistent(maybe)); // MAX!!! 这里会多次运行,注意这里的return先后顺序
	}
	else{
		int j;
		for(j=0;j<=PLAYERS;j++){ // 枚举上场!
			if((j<PLAYERS && used[j]<5) || (j==PLAYERS && used[j]<3)){ //add char to partial hand 枚举另外2个人和gang
				//used[j] 代表第几张牌(players:0 1 2 3 4 / gang:0 1 2)
				maybe[j][used[j]] = notHave[i];
				used[j]++;
				solve(i+1, notHave);
				used[j]--; // undo change before trying next player
			}
		}
	}
}

// return first inconsistent turn (0 based) or total turns if consistent
int countConsistent(char maybe[][HAND+1])
{
	int t=0;
	while(t<turns &&
		countDups(maybe[who[t]],quest[t]) == matches[t]) // 符合询问
		t++;
	return t;
}

int countDups(char a1[],char a2[]) // who[t]玩家的5张牌与询问的3张牌判断是否等于询问中的匹配数match[t]
{
	int i,j;
	int n=0;
	for(i=0;i<5;i++){
		for(j=0;j<3;j++)
			if(a1[i]==a2[j])
				n++;
	}
	return n;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值