uva 340 A - Master-Mind Hints (暴力)

本文详细解析了MasterMind游戏中的核心算法,介绍了如何计算猜测与秘密代码之间的匹配度,包括强匹配与弱匹配的数量,并提供了具体的实现代码。
A - Master-Mind Hints
Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu
Submit  Status

Description

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 tex2html_wrap_inline35 and a guess tex2html_wrap_inline37 , and are to determine the hint. A hint consists of a pair of numbers determined as follows.

match is a pair (i,j), tex2html_wrap_inline41 and tex2html_wrap_inline43 , such that tex2html_wrap_inline45 . 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.

 

给定一个答案序列.

之后给定若干个相同长度的序列

问有多少位置对应的数字相同,以及数字相同,但位置不对应的位置的个数.(位置的意思是,如果不同的位置出现相同的数字,那么要算多次)

前者扫一遍就可以得到结果,答案记为ans1

后者我们可以先考虑在两个序列中都出现的数字的个数,再减去ans1

 1 /*************************************************************************
 2     > File Name: code/uva/340.cpp
 3     > Author: 111qqz
 4     > Email: rkz2013@126.com 
 5     > Created Time: 2015年09月15日 星期二 16时19分44秒
 6  ************************************************************************/
 7 
 8 #include<iostream>
 9 #include<iomanip>
10 #include<cstdio>
11 #include<algorithm>
12 #include<cmath>
13 #include<cstring>
14 #include<string>
15 #include<map>
16 #include<set>
17 #include<queue>
18 #include<vector>
19 #include<stack>
20 #include<cctype>
21 #define y1 hust111qqz
22 #define yn hez111qqz
23 #define j1 cute111qqz
24 #define ms(a,x) memset(a,x,sizeof(a))
25 #define lr dying111qqz
26 using namespace std;
27 #define For(i, n) for (int i=0;i<int(n);++i)  
28 typedef long long LL;
29 typedef double DB;
30 const int inf = 0x3f3f3f3f;
31 const int N=1E3+7;
32 int n;
33 int a[N],b[N];
34 int cnt1,cnt2,ans1,ans2,ans;
35 
36 int main()
37 {
38   #ifndef  ONLINE_JUDGE 
39     freopen("in.txt","r",stdin); 
40   #endif
41     int cas = 0 ;
42     while (scanf("%d",&n)!=EOF&&n)
43     {
44     cas++;
45     printf("Game %d:\n",cas);
46     for ( int i = 0 ; i < n ; i++)
47         scanf("%d",&a[i]);
48     
49 
50     int x;
51     while (scanf("%d",&x)!=EOF)
52     {
53         ans1 = 0 ;
54         ans2 = 0 ;
55         ans = 0;
56         b[0] = x;
57         for ( int i = 1 ; i < n  ; i++)
58         scanf("%d",&b[i]);
59         if (x==0) break; //注意要都读完再break掉....傻逼错误多少次了
60         
61         for (int i = 0 ; i < n ; i++)
62         if (a[i]==b[i])
63             ans1++;
64         
65         
66     
67         
68         for ( int dig = 1 ; dig <= 9 ; dig++)
69         {
70         cnt1 = 0 ;
71         cnt2 = 0;
72         for (int i = 0 ; i < n ; i++)
73         {
74             if (a[i]==dig) cnt1++;
75             if (b[i]==dig) cnt2++;
76         }
77         int MIN = min(cnt1,cnt2);
78         ans2 = ans2 + MIN;
79         }
80         printf("    (%d,%d)\n",ans1,ans2-ans1);
81         
82     }
83     }
84   
85   
86  #ifndef ONLINE_JUDGE  
87   fclose(stdin);
88   #endif
89     return 0;
90 }
View Code

 

转载于:https://www.cnblogs.com/111qqz/p/4810876.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值