UVa340 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.

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)
#include <stdio.h>
#define MAXN 1010

int main()
{
  int n, a[MAXN], b[MAXN];
  int kase = 0;
  while(scanf("%d", &n) == 1 && n)
  {
    printf("Game %d:\n", ++kase);
    for(int i = 0; i < n; i++) scanf("%d", &a[i]);
    for(;;)
    {
       int A = 0, B = 0;
       for(int i = 0; i < n; i++)
       {
         scanf("%d", &b[i]);
         if(a[i] == b[i]) A++;
       }
      if(b[0] == 0) break;
      for(int d = 1; d <= 9; d++)
      {
        int c1 = 0, c2 = 0;
        for(int i = 0; i < n; i++)
        {
          if(a[i] == d) c1++;
          if(a[i] == d) c2++;
        }
        if(c1 < c2) B += c1; else B += c2;
      }
      printf("    (%d,%d)\n", A, B-A);
   }
  }
  return 0;
}

这道题思路比较简单,编码也没有多么困难,不再赘述。

时间: 2024-08-24 21:05:57

UVa340 Master-Mind Hints的相关文章

UVA340 UVALive5448 Master-Mind Hints

Regionals 1995 >> North America - North Central NA 问题链接:UVA340 UVALive5448 Master-Mind Hints.基础训练题,用C语言编写程序. 题意:输入多组测试用例,每个用例第1个数为n,n=0时结束.后面有若干行,每行输入n个正整数(值范围为1到9),第1行是n个秘密数(需要猜测的数),第2行开始是猜测的n个数,若猜测的n个数全为0则结束进入下一个测试用例. 求的是,猜测的n个数有几个相同(位置和值与值都相同),有几

猜数字游戏的提示(Master-Mind Hints,UVa340)

[本博文非博主原创,思路与题目均摘自 刘汝佳<算法竞赛与入门经典(第2版)>] Question 例题3-4 猜数字游戏的提示(Master-Mind Hints,UVa340) 实现一个经典的"猜数字"游戏.给定答案序列和用户猜的序列,统计有多少数字位置正确(A),有多少数字在两个序列都出现过但位置不对(B). 输入包含多组数据.每组输入第一行为序列长度 n,第二行是答案序列,接下来若干行猜测序列.猜测序列全0 时该组数据结束. n=0时输入结束. Example Inp

UVa340(Master-Mind Hints)未完成

#include<stdio.h> int main() { int num,a[100],i,j,b[100]; while(scanf("%d",&num)!=EOF) { int s1=0,s2=0; for(i=0;i<num;i++) { scanf("%d",&a[i]); } for(i=0;i<num;i++) { scanf("%d",&b[i]); } for(i=0;i<

[转]You Could Become an AI Master Before You Know It. Here’s How.

转自:https://www.technologyreview.com/s/608921/ai-algorithms-are-starting-to-teach-ai-algorithms/# You Could Become an AI Master Before You Know It. Here’s How. Automating machine learning will make the technology more accessible to non–AI experts. by 

reset master和reset slave命令解析和区别

reset master删除所有index file 中记录的所有binlog 文件,将日志索引文件清空,创建一个新的日志文件,这个命令通常仅仅用于第一次用于搭建主从关系的时的主库, 注意: reset master 不同于purge binary log的两处地方1.reset master 将删除日志索引文件中记录的所有binlog文件,创建一个新的日志文件 起始值从000001 开始,然而purge binary log 命令并不会修改记录binlog的顺序的数值2.reset maste

微信 {&quot;errcode&quot;:40029,&quot;errmsg&quot;:&quot;invalid code, hints: [ req_id: Cf.y.a0389s108 ]&quot;}

{"errcode":40029,"errmsg":"invalid code, hints: [ req_id: Cf.y.a0389s108 ]"} 问题:微信网页授权后,获取到 openid 了,一刷新又没了 微信网页授权获取到的 code 只能使用一次(5分钟内有效),使用一次后,马上失效. 页面授权跳转成功,根据 code 也换取到 openid 了. 此时刷新页面,并不会再次进行授权,而是直接刷新了一下上一次授权跳转后的链接,带的还是

kubernetes Master部署之Scheduler 以及 HA部署(5)

Kubernetes Scheduler作用是将Controller Manager将要新建的Pod按照特定的调度算法和调度策略绑定到集群中某个合适的Node上,并将绑定信息写入到etcd中. 一.部署Scheduler 下面生成kube-scheduler的kubeconfig文件,操作如下: cd /etc/kubernetes export KUBE_APISERVER="https://192.168.15.200:6443" 配置 cluster kubectl config

nis master的yppasswdd

yppasswdd是nis master特有的,用于和slave进行同步(即push的方式,包括yppasswd密码更新) 操作: master: # /usr/lib/yp/ypinit -m 所有从nis服务器 # service ypserv restart # service yppasswdd restart 有2个关键点: 1是ypinit -m 写上所有nis从服务器 2是需重启yppasswdd服务

2015南阳CCPC A - Secrete Master Plan 水题

D. Duff in Beach Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 无 Description Master Mind KongMing gave Fei Zhang a secrete master plan stashed in a pocket. The plan instructs how to deploy soldiers on the four corners of the city wall. Unfortunately, w