UVA 340 Master-Mind Hints

来源:互联网 发布:宝宝吃奶记录软件 编辑:程序博客网 时间:2024/05/17 01:10

 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 tex2html_wrap_inline35 and a guess tex2html_wrap_inline37 , and are to determine the hint. A hint consists of a pair of numbers determined as follows.

match is a pair (i,j), tex2html_wrap_inline41 and tex2html_wrap_inline43 , such that tex2html_wrap_inline45 . 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 Nzeroes; 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 theexact format.


简直就是一道英语阅读题目。。。。

题意是很重要的。。看了半天没看懂。,看了下别人的题解http://blog.csdn.net/oceaniwater/article/details/7947558 明白的题意

4
1 3 5 5         a


1 1 2 3         b        (1,1)
4 3 3 5         c         (2,0)
6 5 5 1         d         (1,2)
6 1 3 5         e         (1,2)
1 3 5 5         f          (1,2)
0 0 0 0

 题意: a行是设计者出的密码 ,下面几行都是破译者给出的猜想,将下面每次猜想的那行密码一次与a行代码比较,先比较所有strong的,也就是两行不但值一样,且位置都一样,发现一个就加一,得到的数就是括号里第一个数字,注意,每行猜想的密码与a行代码比较时,每个位置的数字只能使用一次,所以最好用两个标记数组来记录一下密码的使用状况,接着就算weak的值,也就是两行值一样,但位置却不同且每个位置都还没标记过的,发现一个就加一,值就是括号里第二个的大小。

 

  i  0 123a 1355b 1123c4355

 考虑自己的表达不好,就用b行和c行来说明一下

b行的strong和weak值分别就是括号里的两个数,strong的那个1是(a.0) (b.0),weak是 (a.1)(b.3)

c行strong值有两个,一个是 (a,1)(c,1),另一个是 (a.3)(c.3)没有符合weak要求的

还是感谢这兄弟的题意。。 不然真的坑爹无比啊。。。


知道题意就简单了。。先找strong的。找过的标记掉,我是把找过标记为0(反正数字不会有0)。。然后在找weak的。。


#include <stdio.h>#include <string.h>int n;int star1[1005];int star[1005];int end[1005];int first, second;int main(){    int t = 1;    while (scanf("%d", &n) != EOF && n)    {memset(star1, 0 ,sizeof(star1));memset(end, 0, sizeof(end));for (int i = 0; i < n ; i ++)    scanf("%d", &star1[i]);printf("Game %d:\n", t ++);while (1){    first = 0;    second = 0;    memset(star, 0 , sizeof(star));    for (int i = 0; i < n; i ++)    {scanf("%d", &end[i]);star[i] = star1[i];    }    if (end[0] == 0)break;    for (int i = 0; i < n; i ++)    {if (star[i] == end[i]){    star[i] = end[i] = 0;    first ++;}    }    for (int i = 0; i < n; i ++)    {if (star[i] == 0)    continue;for (int j = 0; j < n; j ++){    if (end[j] == 0)continue;    if (star[i] == end[j])    {star[i] = 0;end[j] = 0;second ++;    }}    }    printf("    (%d,%d)\n", first, second);}    }    return 0;    }



原创粉丝点击