首页 > 代码库 > Problem 004——Master-Mind Hints
Problem 004——Master-Mind Hints
Master-Mind Hints |
MasterMind is a game for two players. One of them, Designer, selects a secret code. The other, Breaker, tries to break it. A code is no more than a row of colored dots. At the beginning of a game, the players agree upon the length N that a code must have and upon the colors that may occur in a code.
In order to break the code, Breaker makes a number of guesses, each guess itself being a code. After each guess Designer gives a hint, stating to what extent the guess matches his secret code.
In this problem you will be given a secret code and a guess , and are to determine the hint. A hint consists of a pair of numbers determined as follows.
A match is a pair (i,j), and , such that . Match (i,j) is called strong when i = j, and is called weak otherwise. Two matches (i,j) and (p,q) are called independent when 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 of matches and the number of strong matches are both maximal. The hint then consists of the number of strong followed by the number of weak matches in M. Note that these numbers are uniquely determined by the secret code and the guess. If the hint turns out to be (n,0), then the guess 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 specifying N (the length of the code). Following these will be the secret code, represented as N integers, which we will limit to the range 1 to 9. There will then follow an arbitrary number of guesses, each also represented as N integers, each in the range 1 to 9. Following the last guess in each game will be N zeroes; these zeroes are not to be considered as a guess.
Following the data for the first game will appear data for the second game (if any) beginning with a new value for N. The last game in the input will be followed by a single zero (when a value for N would normally be specified). The maximum value for N will be 1000.
Output
The output for each game should list the hints that would be generated for each guess, in order, one hint per line. Each hint should be represented as a pair of integers enclosed in parentheses and separated by a comma. The entire list of hints for each game should be prefixed by a heading indicating the game number; games are numbered sequentially starting with 1. Look at the samples below for the exact format.
Sample Input
41 3 5 51 1 2 34 3 3 56 5 5 16 1 3 51 3 5 50 0 0 0101 2 2 2 4 5 6 6 6 91 2 3 4 5 6 7 8 9 11 1 2 2 3 3 4 4 5 51 2 1 3 1 5 1 6 1 91 2 2 5 5 5 6 6 6 70 0 0 0 0 0 0 0 0 00
Sample Output
Game 1: (1,1) (2,0) (1,2) (1,2) (4,0)Game 2: (2,4) (3,2) (5,0) (7,0)
CODE
#include"stdio.h"
int main()
{
int n;
int a[999],b[999];
int i,j,k,l;
int c=1;
int A,B;
while(scanf("%d",&n)!=EOF)
{
if(n==0)
break;
printf("Game %d:\n",c);
c++;
for(i=0;i<n; i++)
scanf("%d",&a[i]);
while(1)
{
A=0,B=0;
for(j=0; j<n; j++)
{
scanf("%d",&b[j]);
if(a[j]==b[j])
A++;
}
if(b[0]==0)
break;
int an,bn;
for(k=1;k<=9;k++)
{
an=0,bn=0;
for(l=0;l<n;l++)
{
if(a[l]==k)
an++;
if(b[l]==k)
bn++;
}
if(bn>an)
B+=an;
else
B+=bn;
}
printf(" (%d,%d)\n",A,B-A);
}
}
return 0;
}
My Way
对于这个题目,我真想说一句,那本对应书籍真挺萌的坑爹啊!
因为某些原因,我的输入和输出都是按照书上给的来做题,用手机交到UVa上怎么也不对。到后来才发现,是书上给的输出格式错了,我勒个去,不要这么坑人的。
但是总的来说,这个题的题目还是蛮长的,仅仅是翻译就醉了,看来以后要好好学英语了。
这个题目不算特别难。
读懂题意后,首先是定义了俩数组,毕竟一个用来存正确的密码一个存猜测的嘛。
然后通过while、scanf和EOF的搭配来实现多次输入密码长度n。
用for循环输入后与正确密码进行比较,得到A。
最重要的部分就是下面这个:
int an,bn;
for(k=1;k<=9;k++)
{
an=0,bn=0;
for(l=0;l<n;l++)
{
if(a[l]==k)
an++;
if(b[l]==k)
bn++;
}
if(bn>an)
B+=an;
else
B+=bn;
我了个去,这玩意耗费了我好多好多脑细胞,真TMD难想啊。
首先一层循环是从1到9挨个试,这个无需解释。
二层循环是最重要的,它的目的就是找出某个数字在数组a和b里面各有多少个,此时得到an和bn。
然后将an和bn中小的那个加进B中。
这是因为,an和bn中小的那个就是有多少个数字在正确代码中的。
如此,就可以得到B,B就是猜测数据中所有和正确重复的数字数量。
用B减去A即为想要求的括号中后面的那个值。
哎呦我去。。。看来我自己真的不会表达,算了,自己懂就可以了。。。真布吉岛该咋说。。。
这是第四个题,有人据说UVA已经完成20+了,我去,我落后了这么多!我得努力啦!!!
Problem 004——Master-Mind Hints