紫书第三章训练 UVA 340 Master-Mind Hints by 16 BobHuang

本文详细解析了MasterMind游戏的算法实现,介绍了如何通过比较秘密代码与猜测代码来确定匹配情况,包括强匹配与弱匹配的概念,并提供了一段AC代码作为实现示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

来源:http://www.cnblogs.com/BobHuang/p/6798797.html?from=singlemessage&isappinstalled=1

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 

Sample Output

Game 1:
    (1,1)
    (2,0)
    (1,2)
    (1,2)
    (4,0)
Game 2:
    (2,4)
    (3,2)
    (5,0)
    (7,0)
这道题不能不让我想起新生赛那到题,两道题意思相差不是很多,都是猜数字,统计有多少数字位置正确(f1),有多少数字在两个序列都出现过但位置不对(f2),
一行数字全为0结束,所以我的思路就是分别写两个循环,找到f1,f2。因为出现的数字会相同,我怕我没有统计多次,所以用vis,vi来标记了出现的数字。

AC代码:
 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 const int N=1005;
 4 int a[N],q[N],vi[N],vis[N];
 5 int main()
 6 {
 7    int k=0,n;
 8    while (scanf("%d",&n)!=EOF,n){
 9      printf("Game %d:\n",++k);
10      for (int i=1;i<=n;i++)
11         scanf("%d",&a[i]);
12      for(;;){
13      for (int i=1;i<=n;i++)
14      scanf("%d",&q[i]);
15      int s=0;
16      for (int i=1;i<=n;i++)
17      s+=q[i];
18      if(s){
19      int f1=0,f2=0;
20      memset(vi,0,sizeof(vi));
21      memset(vis,0,sizeof(vis));
22           for (int i=1;i<=n;i++)
23             if (a[i]==q[i]){
24                 vi[i]=1;
25                 vis[i]=1;
26                 f1++;
27             }
28           for (int i=1;i<=n;i++){
29             for (int j=1;j<=n;j++){
30                 if (a[i]==q[j]&&!vi[i]&&!vis[j]){
31                     f2++;
32                     vi[i]=1;
33                     vis[j]=1;
34                 }
35             }
36           }
37           printf("    (%d,%d)\n",f1,f2);}
38         else break;
39      }
40    }
41    return 0;
42 }

 

转载于:https://www.cnblogs.com/tzcacm/p/6801442.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值