Uva340
MasterMindisagamefortwoplayers. Oneofthem,Designer,selectsasecretcode. Theother,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. Inorderto breakthecode, Breakermakesanumber ofguesses, eachguessitself beinga 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 strongwhen 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 (thelengthofthecode). Followingthesewillbethesecretcode,representedas 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. Output Theoutputforeachgameshouldlistthehintsthatwouldbegeneratedforeachguess,inorder,onehint 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)
1 #include <cstdio> 2 #include <iostream> 3 #include <queue> 4 #include <vector> 5 #include<string.h> 6 #include<map> 7 #include<bits/stdc++.h> 8 #define LL long long 9 #define maxn 1005 10 using namespace std; 11 int n,ans,sum; 12 int a[maxn],b[10],c[10]; 13 int w,tot; 14 int main() 15 { 16 while(~scanf("%d",&n),n) 17 { 18 printf("Game %d:\n",++tot); 19 memset(b,0,sizeof b); 20 for(int i=0;i<n;i++) 21 { 22 scanf("%d",&a[i]); 23 b[a[i]]++; 24 } 25 for(;;) 26 { 27 ans=0,sum=0; 28 memset(c,0,sizeof c); 29 for(int i=0;i<n;i++) 30 { 31 scanf("%d",&w); 32 c[w]++; 33 if(w==a[i]) 34 ans++; 35 } 36 if(!w)break; 37 for(int i=1;i<=9;i++) 38 { 39 c[i]=min(c[i],b[i]); 40 sum+=c[i]; 41 } 42 printf(" (%d,%d)\n",ans,sum-ans); 43 } 44 } 45 return 0; 46 }
思路:
输入答案序列,放入数组里,用w表示猜测序列的字符,不用把猜测序列放入数组中,用一个变量可以节约空间,每输入一次w在线处理。直接统计就可以得到答案序列和猜测序列相同的部分,
对于“有多少个数字出现过但位置不对”可以这么想:先计算有多少个数在两个序列出现过,再减去前面直接统计得到的答案序列和猜测序列相同的个数就行了。
怎么计算有多少个数在两个序列出现过呢?可以开两个数组b[10],c[10]; 用于计算在两个序列中1~9这些数字出现的个数。
注意点:
循环里套循环,在里层的循环里写break;只能跳出里层的循环。