340 - Master-Mind Hints

来源:互联网 发布:网络安全法执法案例 编辑:程序博客网 时间:2024/04/29 22:47

Uva

340 - Master-Mind Hints


 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 wheni = j, and is called weakotherwise. Two matches (i,j) and (p,q) are calledindependentwheni = p if and only if j = q. A set of matches is calledindependent 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

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)
又是考英语。。。

题目大意:

给一个N(有多组,以0为结束)代表每行序列的数字个数,N下面有不确定组数据,第一行为正确的数据,后面行均为猜测的数据(以N个0结束),用猜测数据去跟正确数据匹配。匹配方式如下:

 两匹配的数据的位置均固定,如果同一位置的数据相等,则代表强匹配,强匹配的个数即输出(a,b)中的a,不同位置相等的数据为弱匹配,个数为b,只要强(或弱)匹配了的数据在以后匹配中不考虑。例如:

1 2 3 4 5

1 2 4 3 2

第一、二列为强匹配个数为2,第一行3(4)列与第二行4(3)列、为弱匹配,个数为2,应输出(2,2)。


#include <iostream>#include <cstdio>#include <cstring>using namespace std;int main(){//    freopen("a.txt","r",stdin);    int n,secret[1000],guess[1000],tmp[1000],cas=0,f;    while(cin>>n&&n)    {        cout<<"Game "<<++cas<<":"<<endl;        for(int i=0;i<n;i++)            cin>>secret[i];        f=0;        for(;;)        {            int a=0,b=0;            memcpy(tmp,secret,sizeof(tmp));            for(int i=0;i<n;i++)            {                cin>>guess[i];                if(!i&&!guess[0])                    f=1;                if(tmp[i]==guess[i])                {                    a++;                    tmp[i]=guess[i]=0;                }            }            if(f)                break;            for(int i=0;i<n;i++)            {                if(!guess[i])                    continue;                for(int j=0;j<n;j++)                {                    if(!tmp[j]) continue;                    if(guess[i]==secret[j])                    {                        tmp[j]=0;                        b++;                        break;                    }                }            }            printf("    (%d,%d)\n",a,b);        }    }    return 0;}



原创粉丝点击