zoukankan      html  css  js  c++  java
  • 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

    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)

    题目大意就是:实现一个经典的“猜数字”游戏。给定答案序列和用户猜的序列,统计有多少数字正确且位置正确的个数,记为A,有多少数字在两个序列都出现过但位置不对的个数,记为B。
    输入包含多组数据。每组输入第一行为序列长度n,第二行是答案序列,接下来是若干猜测序列。猜测序列全为0时该组数据结束。n=0是输入结束。

    例如 : 1 3 5 5 (答案序列)
    1 1 2 3 (猜测序列) 两个序列中的第一个1数字位置匹配成功,A个数加1,往后再没有匹配成功的数字,故A的值为1;
    两个序列中的1由于被A“使用过”,故B不接受,所以B只找到与之匹配的3,故B的值为1; 下面的数据以此类推;
    A 1 0 0 0
    B 0 0 0 1

     1 #include<stdio.h>
     2 #include<string.h>
     3 #define maxn 1010
     4 int ra[10],rb[10];
     5 int a[maxn],b[maxn];
     6 int min(int a,int b)
     7 {
     8     if(a<b) return a;
     9     return b;
    10 }
    11 int main()
    12 {
    13     int n,ans=0;
    14     while(~scanf("%d",&n),n)
    15     {
    16         memset(a,0,sizeof(a));
    17         memset(b,0,sizeof(b));
    18         printf("Game %d:
    ",++ans);
    19         for(int i=0;i<n;i++){
    20             scanf("%d",&a[i]);
    21         }
    22       while(1)
    23         {
    24         memset(ra,0,sizeof(ra));
    25         memset(rb,0,sizeof(rb));
    26             int A=0,B=0;
    27             for(int i=0;i<n;i++){
    28                 scanf("%d",&b[i]);
    29                 if(a[i]==b[i]) A++;
    30                 ra[a[i]]++; //依次遍历数组中的数字,若满足条件则加1
    31                 rb[b[i]]++;
    32             }
    33             if(!b[0]) break;
    34             for(int i=1;i<=9;i++)
    35                 B+=min(ra[i],rb[i]); //两个数组比较,出现次数少的数相加,得到的sum再减去A的个数即为B的个数
    36                 printf(" (%d,%d)
    ",A,B-A);
    37         }
    38     }
    39     return 0;
    40 }
    
    

    运行结果

    4
    Game 1:
    1 3 5 5
    1 1 2 3
     (1,1)
    4 3 3 5
     (2,0)
    6 5 5 1
     (1,2)
    6 1 3 5
     (1,2)
    1 3 5 5
     (4,0)
    0 0 0 0
    10
    Game 2:
    1 2 2 2 4 5 6 6 6 9
    1 2 3 4 5 6 7 8 9 1
     (2,4)
    1 1 2 2 3 3 4 4 5 5
     (3,2)
    1 2 1 3 1 5 1 6 1 9
     (5,0)
    1 2 2 5 5 5 6 6 6 7
     (7,0)
    0 0 0 0 0 0 0 0 0 0
    0

    还有一种做法是用map容器,详细代码见如下链接

    http://www.cnblogs.com/acvay/p/3947270.html









  • 相关阅读:
    npm
    React
    php区分new static 和new self
    tiny java web server
    算法可视化
    在线markdown编辑器
    JAVA
    linux find命令
    自定义windows新建菜单
    floyd算法
  • 原文地址:https://www.cnblogs.com/bearkid/p/7269290.html
Copyright © 2011-2022 走看看