340 - Master-Mind Hints

本文探讨了MasterMind编码谜题的游戏策略,详细解释了如何通过猜测和反馈来解开秘密编码。同时,文章提供了游戏实例和解决步骤,帮助玩家理解并应用破解技巧。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Master-Mind Hints 

MasterMind is a game for two players. One of them, Designer, selects asecret code. The other,Breaker, tries to break it. A code is no morethan a row of colored dots. At the beginning of a game, the playersagree upon the lengthN that a code must have and upon the colors thatmay occur in a code.

In order to break the code, Breaker makes anumber of guesses, each guess itself being a code. After each guessDesigner gives a hint, stating to what extent the guess matches hissecret code.

In this problem you will be given a secret code tex2html_wrap_inline35 and a guesstex2html_wrap_inline37 , and are to determine the hint. A hint consists of a pair of numbers determined as follows.

A match is a pair (i,j), tex2html_wrap_inline41 andtex2html_wrap_inline43 , such thattex2html_wrap_inline45 . Match (i,j) is calledstrong when i = j, and is called weakotherwise. Two matches (i,j) and (p,q) are calledindependentwhen i = p if and only if j = q. A set of matches is called independent when all of its members are pairwise independent.

Designer chooses an independent set M of matches for which the total number ofmatches and the number of strong matches are both maximal. The hintthen consists of the number of strong followed by the number of weakmatches inM. Note that these numbers are uniquely determined by thesecret code and the guess. If the hint turns out to be (n,0), then theguess is identical to the secret code.

Input

The input will consist of data for a number of games. The input for each game begins with an integer specifyingN (the lengthof the code). Following these will be the secret code, representedasN integers, which we will limit to the range 1 to 9. There will thenfollow an arbitrary number of guesses, each also represented asNintegers, each in the range 1 to 9. Following the last guess in eachgame will beN zeroes; these zeroes are not to be considered as a guess.

Following the data for the first game will appear data forthe second game (if any) beginning with a new value forN. The lastgame in the input will be followed by a single zero (when a value forNwould normally be specified). The maximum value for N will be 1000.

Output

The output for each game should list the hints thatwould be generated for each guess, in order, one hint per line. Eachhint should be represented as a pair of integers enclosed in parentheses and separated by a comma. The entire list of hints for each gameshould be prefixed by a heading indicating the game number; games arenumbered sequentially starting with 1. Look at the samples below for theexact format.

Sample Input

4
1 3 5 5
1 1 2 3
4 3 3 5
6 5 5 1
6 1 3 5
1 3 5 5
0 0 0 0
10
1 2 2 2 4 5 6 6 6 9
1 2 3 4 5 6 7 8 9 1
1 1 2 2 3 3 4 4 5 5
1 2 1 3 1 5 1 6 1 9
1 2 2 5 5 5 6 6 6 7
0 0 0 0 0 0 0 0 0 0
0

Sample Output

Game 1:
    (1,1)
    (2,0)
    (1,2)
    (1,2)
    (4,0)
Game 2:
    (2,4)
    (3,2)
    (5,0)
    (7,0)
    此题读完一遍后完全是懵懵的状态,好,再读一遍,还是不懂!回头仔细阅读题目背景部分,再结合样例,反复推敲,花了很长时间(不记得有没有半个多小时了),才把题目读懂,英语真是硬伤啊。。。
    题目要求大概如下,先输入一个n,为序列长度。然后是若干行数据,第一行是答案序列,接下来的若干行是猜测序列,以一组都为0的数位结尾。此为一组数据。所有数据的结尾,会有一个“0”做为标记。找到答案序列和猜测序列之间,两序列相同位置拥有相等数字的组数,并找到两序列都拥有该数字,但位置不对应的组数。输出是用()包含。
    我也是看了刘汝佳的书,才开始决定用UVA,但这网站好不稳定,不知道是不是我的网的缘故。在书上知道此题后便马上去UVA找了,书上给出了重点内容的翻译,但真正找到此题后,我也是花费了不少时间才弄懂。然后开始敲代码。第一个很简单,直接循环然后逐个比较,相同即累加1。第二个的话用两个数组来记录两个序列中都出现的数字的个数,并取两者间最小的数,即为共有数字最大组数,然后用这个数减去第一个,即为答案。
    敲的过程中遇到不少bug,也都一一改正。但对于以“0”输入为结尾,但我用了while(scanf("%d",&k)!=0)后是死循环,才知道自己错的太离谱了,这里判断的是scanf的返回值,怎么会是“0”!!!。改为while(scanf("%d".&k)==1 && k)  OK!搞定
#include <iostream>
using namespace std;
int min(int a,int b)
{
	if (a>=b) return b;
	else return a;
}
int main()
{
	int k,n,i,p,a[1000],b[1000],c[10],d[10];
	int sum1,sum2;
	n=1;
	p=1; 
	while (scanf("%d",&k)==1 && k)
	{
		printf("Game %d:\n",n);
		for (int i=0;i<k;i++)
		  scanf("%d",&a[i]);
		while (1)
		{ 
		  sum1=0;
		  sum2=0;
		  for (int i=1;i<=9;i++)
		  {
		    c[i]=0;
		    d[i]=0;
		  }
		  for (int i=0;i<k;i++)
		  	scanf("%d",&b[i]);
          if (b[0]==0) break;
		   for (int i=0;i<k;i++)
		     if (a[i]==b[i]) sum1++;
		
		   for (int i=0;i<k;i++)
		     {
		     	c[a[i]]++;
		    	d[b[i]]++;
		     }
		   //for (int i=1;i<=9;i++)
			 //printf("%d %d\n",c[i],d[i]);    
		   for (int i=1;i<=9;i++)
		     	sum2=sum2+min(c[i],d[i]);
		   printf("    (%d,%d)\n",sum1,sum2-sum1);
	    }
	    n++; 
	}
	return 0;	
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值