uva 340 -----Master-Mind Hints(猜数字游戏的提示)

来源:互联网 发布:用c 计算矩阵特征值 编辑:程序博客网 时间:2024/04/30 06:14

    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 playersagree 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. Aftereach 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 s1 . . . sn and a guess g1 . . . gn, and are to determinethe hint. A hint consists of a pair of numbers determined as follows.

   A match is a pair (i, j), 1 ≤ i ≤ n and 1 ≤ j ≤ n, such that si = gj . Match (i, j) is called strongwhen i = j, and is called weak otherwise. Two matches (i, j) and (p, q) are called independent wheni = p if and only if j = q. A set of matches is called independent when all of its members are pairwiseindependent.

  Designer chooses an independent set M of matches for which the total number of matches and thenumber of strong matches are both maximal. The hint then consists of the number of strong followedby the number of weak matches in M. Note that these numbers are uniquely determined by the secretcode 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 integerspecifying 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, eachalso represented as N integers, each in the range 1 to 9. Following the last guess in each game will beN 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 anew value for N. The last game in the input will be followed by a single ‘0’ (when a value for N wouldnormally 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 hintper line. Each hint should be represented as a pair of integers enclosed in parentheses and separated bya comma. The entire list of hints for each game should be prefixed by a heading indicating the gamenumber; games are numbered sequentially starting with 1. Look at the samples below for the exactformat.


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)


         刚一开始看这道题,没具体读,先浏览了一遍,然后对照着样例输入输出看了一下,对于输出结果的前一个数字,就知道怎么出来的了。但是对于第二个数字,仍旧一脸懵。。。后来有仔细审了一遍题,B的个数是:不在对应位置上的且两行都出现过的数字。OK,盖住在对应位置上的那一列数字,看剩余几列,只要数字在两列里都出过,就“B++”。再对照样例输入输出判断一下,果然如此。好了  ,这就是如何输出的思路。

       

        

      








#include<iostream>
#include<set>
#include<cstdio>
#include<vector>
#include<algorithm>


using namespace std;
int main() {


     vector <int> a;
     vector <int> b;
      vector <int>::iterator p;
       vector <int>::iterator q;




       set<int> biaozhun;
       set<int> ceshi;
       set<int> jiaoji;
        set <int>::iterator f;
     int case_=1;
    int N;
    while(scanf("%d",&N)==1)                           
  {


      if(N==0) break;    //   如果N等于0,结束程序


       a.assign(N,0);
       cout<<"Game "<<case_<<":"<<endl;


   int t;
   for(int i=0;i<N;i++)      //输入 标准答案 数列
   {
       cin>>t;
       a[i]=t;
   }




   while(1)
   {
         int A=0,B=0;
        b.assign(N,0);
        int ling=0;
        int m;
        for(int i=0;i<N;i++)   ///输入测试的数列
   {
       cin>>m;
       if(m==0)  ling++;
       b[i]=m;  
   }


    if(ling==N) break;   如果 测试数列是N 个0,则跳出循环


    for(p=a.begin(),q=b.begin();p!=a.end();p++,q++)    //如果对应位置的数字相同,A++
    {
        if(*p==*q)
        {
            A++; *q=0;
        }
    }


     for(p=a.begin(),q=b.begin();p!=a.end();p++,q++)  //  将两行数组  分别存入两个集合中,自动忽略各自重复项
     {
         if(*q!=0)
         {
           biaozhun.insert(*p);
           ceshi.insert(*q);
         }
     }




     set_intersection(biaozhun.begin(),biaozhun.end(),ceshi.begin(),ceshi.end(),inserter(jiaoji,jiaoji.begin()));  //取两集合交集


           if(jiaoji.empty())
           {}


           else                                              //  根据交集元素个数,B++
           { 
                for(int i=0;i<jiaoji.size();i++)  
                B++;




           }






     cout<<"("<<A<<","<<B<<")"<<endl;    //  输出对应测试数列的 A、B值
      


    b.clear();                                //    清除
        ceshi.clear();   
        biaozhun.clear();
         jiaoji.clear();


   }




   a.clear();    
   case_++;
  }




    return 0;
}



0 0