Master-Mind Hints
Master-Mind Hints |
MasterMind is a game for two players. One of them, Designer, selects asecret code. The other,Breaker, tries to break it. A code is no morethan a row of colored dots. At the beginning of a game, the playersagree upon the lengthN that a code must have and upon the colors thatmay occur in a code.
In order to break the code, Breaker makes anumber of guesses, each guess itself being a code. After each guessDesigner gives a hint, stating to what extent the guess matches hissecret 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 calledstrong when i = j, and is called weakotherwise. Two matches (i,j) and (p,q) are calledindependentwhen 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 ofmatches and the number of strong matches are both maximal. The hintthen consists of the number of strong followed by the number of weakmatches inM. Note that these numbers are uniquely determined by thesecret code and the guess. If the hint turns out to be (n,0), then theguess 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 specifyingN (the lengthof the code). Following these will be the secret code, representedasN integers, which we will limit to the range 1 to 9. There will thenfollow an arbitrary number of guesses, each also represented asNintegers, each in the range 1 to 9. Following the last guess in eachgame will beN zeroes; these zeroes are not to be considered as a guess.
Following the data for the first game will appear data forthe second game (if any) beginning with a new value forN. The lastgame in the input will be followed by a single zero (when a value forNwould normally be specified). The maximum value for N will be 1000.
Output
The output for each game should list the hints thatwould be generated for each guess, in order, one hint per line. Eachhint should be represented as a pair of integers enclosed in parentheses and separated by a comma. The entire list of hints for each gameshould be prefixed by a heading indicating the game number; games arenumbered sequentially starting with 1. Look at the samples below for theexact 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) 题意: 猜密码, 每一局游戏给出一个N, 代表有一个长度为N的密码, 随后给出同样长度的猜测密码, 每一局的结束标志是猜测密码全为0 每次当前局结束, 需要重新给出一个新N, 是下一局的密码长度, 若为0, 结束游戏. 每次猜密码, 优先看对应位置的密码是否相同, 若对应位置的密码相同, 则A++; 其次才考虑不同位置的密码相同问题, 位置不对应的密码相同, B++; 如1 3 5 5 和 6 5 5 1 若是不分优先级, 则猜测密码中的第二个5就把加密密码中的第三个5对应掉了, 这样B+1, 而A没加 事实上题目要求是值相同时, 位置一样的优先~ AC代码:#include<stdio.h> int main() { int N; int mark; int code[1005]; int test[1005]; int cmp[1005]; int A, B; int cas = 0; while(scanf("%d", &N) != EOF) { if(N == 0) break; for(int i = 0; i < N; i++) scanf("%d", &code[i]); printf("Game %d:\n", ++cas); while(1) { A = 0; B = 0; mark = 1; for(int i = 0; i < N; i++) { scanf("%d", &test[i]); if(test[i] != 0) mark = 0; } if(mark) break; for(int i = 0; i < N; i++) cmp[i] = code[i]; for(int i = 0; i < N; i++) { if(test[i] == cmp[i]) { A++; test[i] = 0; cmp[i] = 0; } } for(int i = 0; i < N; i++) { for(int j = 0; j < N; j++) { if(test[i] == cmp[j] && test[i] != 0 && cmp[j] != 0) { B++; test[i] = 0; cmp[j] = 0; } } } printf(" (%d,%d)\n", A, B); } } return 0; }