UVa 340 Master-Mind Hints

来源:互联网 发布:windows snmp 软件 编辑:程序博客网 时间:2024/09/21 08:15


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 weakotherwise. 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.


题目中难理解的部分是:独立,实际是形成secret code和guess中每个数字位置最多只有一个匹配。先找到strong match,记录下匹配过的数字位置,然后再扫描一遍secret code,找剩余的weak match。


较好的想法是以每个数字为角度来想,对每个数字来说,match的总数为secret code和guess中数字个数的较小值,算weak match只要用这个match总数减去strong match就可以得到了。


#include "stdio.h"#definemaxn 1000int answer[maxn];int guess[maxn];int s_slot[maxn];int w_slot[maxn];int main(){int n;int count = 0;while(scanf("%d", &n) == 1 && n != 0){int i;for(i = 0; i < n; i++){scanf("%d", &answer[i]);}printf("Game %d:\n", ++count);while(1){int flag = 0;for(i = 0; i < n; i++){                        scanf("%d", &guess[i]);if(guess[i] != 0)flag = 1;}if(!flag)break;int strong_c = 0;int weak_c = 0;for(i = 0; i < n; i++){if(answer[i] == guess[i]){strong_c++;s_slot[i] = 1;w_slot[i] = 1;}else{s_slot[i] = 0;w_slot[i] = 0;}}for(i = 0; i < n; i++){if(s_slot[i] == 0){for(int j = 0; j < n; j++){if(answer[i] == guess[j] && w_slot[j] == 0){weak_c++;w_slot[j] = 1;break;}}}}printf("    (%d,%d)\n", strong_c, weak_c);}}return 0;}



0 0
原创粉丝点击