UVa 340 Master-Mind Hints
蛋疼的题目描述,看了好长好长时间才看懂,题目本身是很简单的。
Designer给出一串长度为N的Code,Breaker用Guess来破译。
对于两串数字,如果有同一列相等的数字,那么叫做strong match,
位于不同列的相等的两个数字,叫做weak match。
题目要求就是先输出strong的个数,然后是weak的个数。
对了,需要注意的是
1、每个code只能匹配一次,不论是strong还是match。
2、输出(*,*)的时候注意前面那四个空格,就因为这个我还PE了一次。
再说说我的算法:我又开了两个数组num1,num2来分别放code和guess中1~9每个数字出现的次数。首先匹配strong,匹配好的同时将num1和num2中对应的元素减1,表示已经匹配过了。strong匹配完以后,剩下的就是weak。因为已经不存在strong的情况,所以weak直接+=min(code, guess)。
以下是原题:
| 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)
AC代码:
//#define LOCAL
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std; const int maxn = + ;
int code[maxn], guess[maxn];
int num1[], num2[], num3[];//存放code和guess中每个数字出现的次数
//num3来放num1的拷贝 int main(void)
{
#ifdef LOCAL
freopen("340in.txt", "r", stdin);
#endif int N, kase = ;
while(scanf("%d", &N) == && N)
{
memset(code, , sizeof(code));
memset(num1, , sizeof(num1));
int i;
for(i = ; i < N; ++i)
{
scanf("%d", &code[i]);
++num1[ code[i] ];
}
printf("Game %d:\n", ++kase);
while(true)
{
memset(guess, , sizeof(guess));
memset(num2, , sizeof(num2));
memcpy(num3, num1, sizeof(num1));
for(i = ; i < N; ++i)
{
scanf("%d", &guess[i]);
++num2[ guess[i] ];
}
if(guess[] == )
break;
//统计strong的个数
int strong = ;
for(i = ; i < N; ++i)
{
if(code[i] == guess[i])
{
++strong;
--num3[ code[i] ];//每个code[i]只能匹配一次
--num2[ guess[i] ];
}
}
//计算weak
int weak = ;
for(i = ; i <= ; ++i)
weak += min(num3[i], num2[i]);
printf(" (%d,%d)\n", strong, weak);
}
}
return ;
}
代码君
UVa 340 Master-Mind Hints的更多相关文章
- 【例题3-4 UVA - 340】Master-Mind Hints
[链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 这里出现了没有在相同位置的只能唯一配对. 就是说 3322 2234 这种情况. 只有3个weak pair. 即key[1]=a[ ...
- UVa 340 Master-Mind Hints (优化查找&复制数组)
340 - Master-Mind Hints Time limit: 3.000 seconds http://uva.onlinejudge.org/index.php?option=com_on ...
- UVa 340 Master-Mind Hints(猜数字游戏的提示)
题意 猜数字游戏 统计猜的数字有多少个数字位置正确 有多少个数字在答案中出现可是位置不对 每一个字符仅仅能匹配一次 直接匹配每位数 #include<cstdio> #includ ...
- UVa 340 - Master-Mind Hints 解题报告 - C语言
1.题目大意 比较给定序列和用户猜想的序列,统计有多少数字位置正确(x),有多少数字在两个序列中都出现过(y)但位置不对. 2.思路 这题自己思考的思路跟书上给的思路差不多.第一个小问题——位置正确的 ...
- UVA 340 Master-Mind Hints 猜密码游戏(水)
题意: 给一串密码(第一行),接着再给你很多行猜测,针对每行猜测,输出两个数字,分表代表:同一列上匹配的个数,不同列上匹配的个数.注:匹配指的是一次,一旦配对,不能再与其他配对. 思路: 每接受一行猜 ...
- 猜数字游戏的提示(Master-Mind Hints, UVa 340)
实现一个经典"猜数字"游戏. 给定答案序列和用户猜的序列,统计有多少数字位置正确 (A),有多少数字在两个序列都出现过但位置不对(B). 输入包含多组数据.每组输入第一行为序列长度 ...
- uva340 Master-Mind Hints (UVA - 340)
题目简要 题目意思很简单每个测试都由原题目在第一行,然后后面的都是去猜的答案,如果猜测的位置正确那么输出的结果的数对里面的第一个数就加一,如果仅答案正确(原题目里有这个数,但是位置不一样)那么就在输出 ...
- 【每日一题】 UVA - 340 阅读理解+模拟
https://cn.vjudge.net/problem/UVA-340 题目很难读,差不多读了两天 意思是给你一个n个数的数列,然后有m个询问,每个询问也是一个n个数的数列,让你输出两个数:一个是 ...
- 《算法竞赛入门经典》习题及反思 -<2>
数组 Master-Mind Hints,Uva 340 题目:给定答案序列和用户猜的序列,统计有多少数字对应正确(A),有多少数字在两个序列都出现过但位置不对. 输入包括多组数据.每组输入第一行为序 ...
随机推荐
- post 方式提交XML文件调用接口
import java.io.BufferedReader; import java.io.InputStreamReader; import java.util.Date; import java. ...
- Java script 看看黑客怎么写的
在2011年的BlackHat DC 2011大会上Ryan Barnett给出了一段关于XSS的示例java script 代码: ($=[$=[ ] ] [(__=!$+$)[_=-~-~-~$] ...
- Android的事件处理机制详解(二)-----基于监听的事件处理机制
基于监听的事件处理机制 前言: 我们开发的app更多的时候是需要与用户的交互----即对用户的操作进行响应 这就涉及到了android的事件处理机制; android给我们提供了两套功能强大的处理机制 ...
- Java中常用的加密方法(JDK)
加密,是以某种特殊的算法改变原有的信息数据,使得未授权的用户即使获得了已加密的信息,但因不知解密的方法,仍然无法了解信息的内容.大体上分为双向加密和单向加密,而双向加密又分为对称加密和非对称加密(有些 ...
- 使用datatable 将测试数据与业务分离
当基本一致的业务流程, 需要测试繁杂的测试数据时,就可以将测试数据参数化了,同时使用datatable来实现测试数据与业务分离,这个方法在我现在的项目中应用在很多地方,感觉超级方便,现对工作中的应用做 ...
- Java多线程-线程的调度(合并)
线程的合并的含义就是将几个并行线程的线程合并为一个单线程执行,应用场景是当一个线程必须等待另一个线程执行完毕才能执行时可以使用join方法. join为非静态方法,定义如下:void join(): ...
- getElementByClassName封装函数用法
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/ ...
- mysql 支持中文,防止程序乱码的方法
1. 查看你的mysql的字符设置 mysql> show variables like 'character%'; +--------------------------+---------- ...
- Android TextView 文字居中
有2种方法可以设置TextView文字居中: 一:在xml文件设置:android:gravity="center" 二:在程序中设置:m_TxtTitle.setGravity( ...
- TCP建立连接的三次握手过程
TCP是因特网中的传输层协议,使用三次握手协议建立连接,下面是TCP建立连接的全过程. 上图画出了TCP建立连接的过程.假定主机A运行的是TCP客户程序,B运行的是TCP服务器程序.最初两端的TCP进 ...