3-4 猜數字遊戲的提示(Master-Mind Hints, UVa 340)

3-4 猜數字遊戲的提示(Master-Mind Hints, UVa 340)

實現一個經典"猜數字"遊戲。給定答案序列和用戶猜的序列,統計有多少數字位置正確(A),有多少數字在兩個序列都出現過但位置不對(B)。

輸入包含多組數據。每組輸入第一行爲序列長度n,第二行是答案序列,接下來是若干猜測序列。猜測序列全0時該組數據結束。n=0時輸入結束。

樣例輸入:

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 56 6 6 9

1 2 3 4 5 67 8 9 1

1 1 2 2 3 34 4 5 5

1 2 1 3 1 51 6 1 9

1 2 2 5 5 56 6 6 7

0 0 0 0 0 00 0 0 0

0

樣例輸出:

Game 1:

     (1,1)

     (2,0)

     (1,2)

     (1,2)

     (4,0)

Game 2:

    (2,4)

    (3,2)

    (5,0)

    (7,0)

【分析】

直接統計可得A,爲了求B,對於每個數字(1~9),統計二者出現的次數c1和c2,則min(c1,c2)就是該數字對B的貢獻。最後要減去A的部分。代碼如下:

#include<stdio.h>
#define maxn 1010
int main() 
{
	int n, a[maxn], b[maxn];
	int kase = 0;
	while(scanf("%d", &n) == 1 && n) { //n=0時輸入結束
		printf("Game %d:\n", ++kase);
		for(int i = 0; i < n; i++) scanf("%d", &a[i]);
		for(;;) {
			int A = 0, B = 0;
			for(int i = 0; i < n; i++) {
				scanf("%d", &b[i]);
				if(a[i] == b[i]) A++;
			}
			if(b[0] == 0) break; //正常的猜測序列不會有0,所以只判斷第一個數是否爲0即可
			for(int d = 1; d <= 9; d++) {
				int c1 = 0, c2 = 0; //統計數字d在答案序列和猜測序列中各出現多少次
				for(int i = 0; i < n; i++) {
					if(a[i] == d) c1++;
					if(b[i] == d) c2++;
				}
				if(c1 < c2) B += c1; else B += c2;
			}
			printf("    (%d,%d)\n", A, B-A);//注意格式需空4個格子 
		}
	}
	return 0;
}

原文如下:

340 - Master-Mind Hints

MasterMind is a game for two players. One of them, Designer, selectsa secret code. The other, Breaker, tries to break it. A code is no more than arow of colored dots. At the beginning of a game, the players agree upon thelength N that a code must have and upon the colors that may occur in a code. Inorder to break the code, Breaker makes a number of guesses, each guess itselfbeing a code. After each guess Designer gives a hint, stating to what extentthe guess matches his secret code. In this problem you will be given a secretcode s1 . . . sn and a guess g1 . . . gn, and are to determine the hint. A hintconsists 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 strong when i = j, andis called weak otherwise. Two matches (i, j) and (p, q) are called independentwhen i = p if and only if j = q. A set of matches is called independent whenall of its members are pairwise independent.

Designer chooses an independent set M of matches for which the totalnumber of matches and the number of strong matches are both maximal. The hintthen consists of the number of strong followed by the number of weak matches inM. Note that these numbers are uniquely determined by the secret code and theguess. If the hint turns out to be (n, 0), then the guess is identical to thesecret code.

Input

The input will consist of data for a number of games. The input foreach game begins with an integer specifying N (the length of the code).Following these will be the secret code, represented as N integers, which wewill limit to the range 1 to 9. There will then follow an arbitrary number ofguesses, 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 tobe considered as a guess.

Following the data for the first game will appear data for thesecond game (if any) beginning with a new value for N. The last game in theinput will be followed by a single ‘0’ (when a value for N would normally bespecified). The maximum value for N will be 1000.

Output

The output for each game should list the hints that would begenerated for each guess, in order, one hint per line. Each hint should berepresented as a pair of integers enclosed in parentheses and separated by acomma. The entire list of hints for each game should be prefixed by a headingindicating 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)


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章