描述

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

?

include<iostream>
using namespace std; const int PLAYERS = 3, // check against final problem statement!
MAX_TURNS = 15, HAND = 5, HID = 3, UNK = HID+HAND*(PLAYERS-1);
int turns; char quest[MAX_TURNS][3+1], // interrogations
hand[PLAYERS + 1][5+1], //actual hands, gang at end
maybe[PLAYERS + 1][HAND+1]; //possible hands, gang char unk[UNK]; // letters not in one player's hand
int who[MAX_TURNS], // who interrogated
matches[MAX_TURNS], // matches in interogation
used[PLAYERS+1]; // amount of maybe hand filled
int solved; // max turns needed for current player for comb. so far int Max(int a,int b);
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]);
int t;
for(t=0;t<turns;t++)
{
scanf("%d",&who[t]);
who[t]--; // internal 0 based
scanf("%s",&quest[t]);
scanf("%d",&matches[t]);
}
bestSolved=MAX_TURNS;
int p;
char unkStr[18-5+1];
for(p=0;p<PLAYERS;p++)
{
memset(unkStr,'\0',sizeof(unkStr));
for(j=0;j<=PLAYERS;j++){
if(j!=p)
strcat(unkStr,hand[j]);
}
strcpy(maybe[p],hand[p]); // player knows own
used[p] = HAND; // no further characters to choose
solved = 0; // after recursion max turns to eliminate a maybe
solve(0,unkStr);
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");
}//end while
return 0;
}//end main() int Max(int a,int b)
{ return (a>b)?a:b; } // accumulate combinations recursively, check when one is complete
void solve(int i,char unk[])
{
if(i==(18-5)){ // assigned all choices, now check
if (maybe[PLAYERS][0] == unk[i-HID]) return; //match end gang char
solved = Max(solved, countConsistent(maybe));
}
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
maybe[j][used[j]]=unk[i];
used[j]++;
solve(i+1,unk);
used[j]--; // undo change before trying next player
}
}
}
} // return first inconsistent turn (0 based) or total turns if consistent
int countConsistent(char choice[][HAND+1])
{
int t=0;
while(t<turns &&
countDups(choice[who[t]],quest[t])==matches[t])
t++;
return t;
} int countDups(char a1[],char a2[])
{
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;
}

  

1091-Black Vienna的更多相关文章

  1. [swustoj 1091] 土豪我们做朋友吧

    土豪我们做朋友吧(1091) 问题描述: 人都有缺钱的时候,缺钱的时候要是有个朋友肯帮助你,那将是一件非常幸福的事情.有N个人(编号为1到N),一开始他们互相都不认识,后来发生了M件事情,事情分为2个 ...

  2. ural 1091. Tmutarakan Exams 和 codeforces 295 B. Greg and Graph

    ural 1091 题目链接:http://acm.timus.ru/problem.aspx?space=1&num=1091 题意是从1到n的集合里选出k个数,使得这些数满足gcd大于1 ...

  3. [Swust OJ 1091]--土豪我们做朋友吧(并查集,最值维护)

    题目链接:http://acm.swust.edu.cn/problem/1091/ Time limit(ms): 1000 Memory limit(kb): 32768   人都有缺钱的时候,缺 ...

  4. ural 1091. Tmutarakan Exams(容斥原理)

    1091. Tmutarakan Exams Time limit: 1.0 secondMemory limit: 64 MB University of New Tmutarakan trains ...

  5. 51Nod 1091 线段的重叠(贪心+区间相关,板子题)

    1091 线段的重叠 基准时间限制:1 秒 空间限制:131072 KB 分值: 5         难度:1级算法题 X轴上有N条线段,每条线段包括1个起点和终点.线段的重叠是这样来算的,[10 2 ...

  6. PAT 乙级 1091 N-自守数 (15 分)

    1091 N-自守数 (15 分) 如果某个数 K 的平方乘以 N 以后,结果的末尾几位数等于 K,那么就称这个数为“N-自守数”.例如 3×92​2​​=25392,而 25392 的末尾两位正好是 ...

  7. nyoj 1091 还是01背包(超大数dp)

    nyoj 1091 还是01背包 描述 有n个重量和价值分别为 wi 和 vi 的物品,从这些物品中挑选总重量不超过W的物品,求所有挑选方案中价值总和的最大值 1 <= n <=40 1 ...

  8. 【PAT】1091 Acute Stroke(30 分)

    1091 Acute Stroke(30 分) One important factor to identify acute stroke (急性脑卒中) is the volume of the s ...

  9. ural 1091. Tmutarakan Exams(容斥)

    http://acm.timus.ru/problem.aspx? space=1&num=1091 从1~s中选出k个数,使得k个数的最大公约数大于1,问这种取法有多少种. (2<=k ...

随机推荐

  1. 【转】MyBatis学习总结(四)——解决字段名与实体类属性名不相同的冲突

    [转]MyBatis学习总结(四)——解决字段名与实体类属性名不相同的冲突 在平时的开发中,我们表中的字段名和表对应实体类的属性名称不一定都是完全相同的,下面来演示一下这种情况下的如何解决字段名与实体 ...

  2. Docker Machine, Compose, and Swarm: How They Work Together

    The three tools are now neatly packaged into what’s called the Docker Toolbox. Docker Machine1/ crea ...

  3. HW--漂亮度

    描述 给出一个名字,该名字有26个字符串组成,定义这个字符串的“漂亮度”是其所有字母“漂亮度”的总和.每个字母都有一个“漂亮度”,范围在1到26之间.没有任何两个字母拥有相同的“漂亮度”.字母忽略大小 ...

  4. xp 中的IIS安装成功之后,访问网页显示没有权限访问解决方法

    在做xp的IIS发布网站时遇到一个问题就是当你访问网站的时候,显示没有权限访问网站,但是我已经开启了匿名访问网站了,怎么还没有权限访问呢?后来经过上网搜资料解决,当时很多网上都说没打开匿名访问,当时我 ...

  5. 使用SqlBulkCopy类批量复制大数据

    using System; using System.Configuration; using System.Data; using System.Data.SqlClient; using Syst ...

  6. GridView 自定义表头

    //修改表头 protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e) { switch (e.Row.Ro ...

  7. 11_Jaxws常用注解

    [不使用注解] 默认namespace是服务类包名的倒序 默认portType是服务类的类名 ............... 注解的所起的作用: Jaxws提供的注解可以对WebService的接口规 ...

  8. linux下screen工具的简单使用

    有时候,希望即使退出终端了,下次登录linux的时候,还能回到程序的控制界面,这个时候,screen工具就很有用了例如,写了一个从控制台读取屏幕输入的程序input_test,如果从终端退出了,下次登 ...

  9. C#拓展练习之模拟键盘录入

    摘自<31天学会CRM项目开发<C#编程入门级项目实战>> 使用C#调用Windows API使程序模拟键盘输入,也可模拟按下快捷键.本例中,单击“模拟输入”按钮,可录入字符“ ...

  10. HTML5的离线储存

    在用户没有与因特网连接时,可以正常访问站点或应用,在用户与因特网连接时,更新用户机器上的缓存文件.        原理:HTML5的离线存储是基于一个新建的.appcache文件的缓存机制(不是存储技 ...