您的位置:首页 > 其它

UVa 340 Master-Mind Hints

2014-07-01 08:42 260 查看
蛋疼的题目描述,看了好长好长时间才看懂,题目本身是很简单的。

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

//#define LOCAL
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;

const int maxn = 1000 + 5;
int code[maxn], guess[maxn];
int num1[10], num2[10], num3[10];//存放code和guess中每个数字出现的次数
//num3来放num1的拷贝

int main(void)
{
#ifdef LOCAL
freopen("340in.txt", "r", stdin);
#endif

int N, kase = 0;
while(scanf("%d", &N) == 1 && N)
{
memset(code, 0, sizeof(code));
memset(num1, 0, sizeof(num1));
int i;
for(i = 0; i < N; ++i)
{
scanf("%d", &code[i]);
++num1[ code[i] ];
}
printf("Game %d:\n", ++kase);
while(true)
{
memset(guess, 0, sizeof(guess));
memset(num2, 0, sizeof(num2));
memcpy(num3, num1, sizeof(num1));
for(i = 0; i < N; ++i)
{
scanf("%d", &guess[i]);
++num2[ guess[i] ];
}
if(guess[0] == 0)
break;
//统计strong的个数
int strong = 0;
for(i = 0; i < N; ++i)
{
if(code[i] == guess[i])
{
++strong;
--num3[ code[i] ];//每个code[i]只能匹配一次
--num2[ guess[i] ];
}
}
//计算weak
int weak = 0;
for(i = 1; i <= 9; ++i)
weak += min(num3[i], num2[i]);
printf("    (%d,%d)\n", strong, weak);
}
}
return 0;
}


代码君
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: