主脑提示( Master-Mind Hints )

2023-05-16

题目

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 s1 . . . sn and a guess g1 . . . gn, and are to determine the 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 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.

输入

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 ‘0’ (when a value for N would normally be specified). The maximum value for N will be 1000.

输出

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.

输入样例

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

输出样例 

Game 1:

(1,1)

(2,0)

(1,2)

(1,2)

(4,0)

Game 2:

(2,4)

(3,2)

(5,0)

(7,0)

分析题意

原题为英文,可根据题意,可分析为先输入一个数N,接下来是输入 N 个整数的猜测,我们将其限制在 1 到 9 的范围内。然后会有任意数量的猜测,每次输入也表示为 N 个整数,每个整数都在 1 到 9 的范围内。每一段的最后一次输入将是 N 个零; 这些零不应被视为猜测。 在第一段的数据之后将出现第二段的数据(如果有的话),以 N 的新值开头。输入中的最后一场比赛将跟在单个“0”之后(当 N 的值通常为 指定的)。 N 的最大值为 1000。

每段的输出应该列出每个猜测会生成的提示,按顺序一个提示一行。 每个提示应表示为一对用括号括起来并由一个逗号分隔 。前一个数为位置正确的数的个数,即强匹配数,后一个数为强匹配数减去猜测的拥有正确数的个数,即弱匹配数。

代码如下

#include <iostream>
#include<string>
#include <algorithm>
#include <cstdlib>
void  pdd(int h[1000]);
int  love(int a[1000]);
int b[10]={0},yuan[1000],flag=0,n,li[1000];
int main()
{
	int i,j=1,m=0,a[1000];
	while(1)
	{
    	for(i=1;i<10;i++)
			b[i]=0;	
		scanf("%d",&n);
		if(n==0)break;
		printf("Game %d:\n",j);
		j++;
		for(i=0;i<n;i++)
		{
			scanf("%d",&li[i]);
			b[li[i]]++;
		}
		while(1)
		{
			for(i=0;i<n;i++)
			{
				scanf("%d",&yuan[i]);
				if(yuan[i]!=0)
				m=1;
			}
			if(m==0)break;
			m=0;
			pdd(yuan);
		}
	}
} 
void pdd(int h[1000])
{
  int i,j,m=0,k=0;
  for(i=n-1;i>=0;i--)
  	if(li[i]==yuan[i]) k++;
  m=love(h);
  printf("    ");
  printf("(%d,%d)\n",k,m-k);
}
int love(int c[1000])
{   int p,d[10]={0},k=0;
	for(p=0;p<n;p++){
	  d[c[p]]++;
	  if(d[c[p]]<=b[c[p]]) k+=1;}
    return k;
}

附:

编者曾用该题目类似的原理写过一个游戏:

#include<stdio.h>
#include<stdlib.h>
#include<time.h>
void  pdd(int h[4]);
int  love(int a[4]);
int b[7]={0},yuan[4],flag=0;
int main()
{
    int i,j,a[4],li;
    char m,n;
    while(1){
    srand(time( NULL ));
    for(i=0;i<4;i++){
        yuan[i]=rand()%6+1;
        b[yuan[i]]++;}
    for(i=0;i<10;i++){
    scanf("%d",&li);
    for(j=0;j<4;j++){
    if(j==0)printf("       ");
    a[j]=li%10;
    li/=10;}
    for(j=3;j>=0;j--)
    printf("   %d",a[j]); 
    pdd(a);
    if(flag==1)  {printf("You are right\n");break;}
    } 
  if(flag==0)    printf("Game over\n");
  printf("需要再来一次吗?(Y/N)");
  scanf("%c",&m);
  if(m=='N' || m=='n')
    break;
    }

void pdd(int h[4])
{
  int i,j,m=0,n=0;
  for(i=3;i>=0;i--)
  if(h[i]==yuan[i]) n++;
  m=love(h);
  printf(" || %d %d\n",m,n);
  if(m==4&&n==4) flag=1;
}
int love(int c[4])
{   int p,d[7]={0},k=0;
    for(p=0;p<4;p++){
      d[c[p]]++;
      if(d[c[p]]<=b[c[p]]) k+=1;}
    return k;
}

原理差不多,系统会生成一个由四个随机1---6的数组,即题目,题目不可见。玩家要正确输入对应的数,和题目相同,才可以挑战成功。如果输入错误,系统会给出提示,左边的数为猜测的拥有正确数的个数,即强匹配数-弱匹配数。右边的数为强匹配数。每次共有10次猜测机会。

如随机生成 1 2 3 4,我们猜测为2 3 4 5,输入2345。系统给出提示为3 0。我们猜测为4 2 3 1,输入4231。系统给出提示为4 2。直至我们猜出1 2 3 4,系统会跳出“恭喜成功”。否则超过10次,系统会提示失败。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

主脑提示( Master-Mind Hints ) 的相关文章

随机推荐

  • 中文与 \u5927\u732a\u8e44\u5b50 这一类编码互转

    了解更多关注微信公众号 木下学Python 吧 a 61 39 大猪蹄子 39 a 61 a encode 39 unicode escape 39 print a 运行结果 xff1a b 39 u5927 u732a u8e44 u5b
  • python字典删除键值对

    https blog csdn net uuihoo article details 79496440
  • 计算机网络(4)传输层

    目录 小知识点 xff1a 三次握手 xff1a 状态 xff1a tcpdump xff1a 一 xff1a 命令介绍 xff1a 二 xff1a 命令选项 xff1a tcpdump的表达式 xff1a 使用python扫描LAN工具
  • MSE 治理中心重磅升级-流量治理、数据库治理、同 AZ 优先

    作者 xff1a 流士 本次 MSE 治理中心在限流降级 数据库治理及同 AZ 优先方面进行了重磅升级 xff0c 对微服务治理的弹性 依赖中间件的稳定性及流量调度的性能进行全面增强 xff0c 致力于打造云原生时代的微服务治理平台 前情回
  • TF多层 LSTM 以及 State 之间的融合

    第一是实现多层的LSTM的网络 第二是实现两个LSTM的state的concat操作 分析 state 的结构 对于第一个问题 之前一直没有注意过 看下面两个例子 在这里插入代码片 import tensorflow as tf num u
  • 实例讲解PMP相关方参与度评估矩阵

    在规划相关方参与计划过程中 xff0c 会用到相关方参与度评估矩阵 如下图所示 在上图中 xff0c C 代表每个相关方的当前参与水平 xff0c D 是项目团队评估出来的 为确保项目成功所必不可少的参与水平 xff08 期望的 xff09
  • 在Mac OS中安装 wget

    先从Apple Store下载Xcode xff0c 然后安装Xcode 接着安装Homebrew包管理 xff0c 类似于Ubuntu下的apt get xff0c 终端下输入 xff1a ruby span class hljs ope
  • 前端与产品经理配合

    产品经理PM职业介绍 如何构建原型图 axure软件
  • C++ 重载运算符

    C 43 43 重载运算符号 本文针对结构体重载运算符号进行讲解 其实这是一个困扰我蛮久的问题 xff0c 就是结构体如何使用sort函数进行排序 xff0c 去网上找了很多 xff0c 满多都是关于类的 xff0c 虽然类跟结构体只有访问
  • &运算符的用法

    按位与运算符 34 amp 34 是双目运算符是参与运算的两数各对应的二进位相与 按位与 34 amp 34 功能是参与运算的两数各对应的二进位相与 只有对应的两个二进位均为1时 xff0c 结果位才为1 xff0c 否则为0 参与运算的数
  • 火柴棒游戏(暴力枚举)C++

    暴力枚举 P1149 NOIP2008 提高组 火柴棒等式 题目描述 xff1a 给你n根火柴棍 xff0c 你可以拼出多少个形如 A 43 B 61 CA 43 B 61 C 的等式 xff1f 等式中的AA BB CC是用火柴棍拼出的整
  • 2021蓝桥杯B组 G题砝码称重

    题目大意 xff1a 解法一 xff1a 首先想到的是可以用广度优先搜索的方式来进行暴力求解 xff0c 通过使用递归来将每一种方法遍历 xff0c 并且标记 xff0c 不过由于此方法的时间复杂度是O n3 故使用暴力搜索只能完成50 的
  • 2021蓝桥杯B组 第I题杨辉三角形

    第I题 杨辉三角形 题目大意 xff1a 解法一 xff1a xff08 得20 xff09 思路 xff1a 当指考虑小范围的值时 xff0c 我们可以直接根据杨辉三角形的规律 xff1a 第i行第j列的值 61 第i 1行第j列的值 4
  • Docker--安装Docker和简单使用

    1 docker的安装 1 首先先有一台配置高的虚拟机 xff08 至少两核四G xff09 2 按官方文档 Install Docker Engine on CentOS Docker Documentation 删除docker软件包
  • 力扣 1697. 检查边长度限制的路径是否存在

    给你一个 n 个点组成的无向图边集 edgeList xff0c 其中 edgeList i 61 ui vi disi 表示点 ui 和点 vi 之间有一条长度为 disi 的边 请注意 xff0c 两个点之间可能有 超过一条边 给你一个
  • c++stl 学习心得

    一 c 43 43 和c的区别 xff1a 1 函数默认值 在C 43 43 中我们在定义或声明一个函数的时候 xff0c 有时会在形参中给它赋一个初始值作为不传参数时候的缺省值 xff0c 例如 xff1a int is xff08 in
  • LeetCode 189.轮转数组 (双指针)

    题目传送门 xff1a 轮转数组 题目详情 xff1a 给你一个数组 xff0c 将数组中的元素向右轮转 k 个位置 xff0c 其中 k 是非负数 示例 1 输入 nums 61 1 2 3 4 5 6 7 k 61 3 输出 5 6 7
  • P1005 [NOIP2007 提高组] 矩阵取数游戏

    题目描述 帅帅经常跟同学玩一个矩阵取数游戏 xff1a 对于一个给定的 n m 的矩阵 xff0c 矩阵中的每个元素 ai j 均为非负整数 游戏规则如下 xff1a 每次取数时须从每行各取走一个元素 xff0c 共 n 个 经过 m 次后
  • C - The Domino Effect(dfs+回溯)

    作者 xff1a JF 题目描述 一组标准的双六多米诺骨牌包含28块骨牌 xff08 称为骨头 xff09 xff0c 每个骨牌使用类似骰子的点子显示从0 xff08 空白 xff09 到6的两个数字 28块独特的骨骼由以下PIP组合组成
  • 主脑提示( 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