UVa 340 Master-Mind Hints

【题目链接】

http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=276

【解题思路】

本题题意比较晦涩,主要核心部分为下面一段话:

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.

这段话定义了强弱match的条件和两个match之间的独立性成立条件。大致的题意是,对于给定的一串数字s,根据玩家不同的猜测数字串g,从中找出所有的强match和尽量多的弱match,并且所有match两两之间必须独立。统计强match和弱match的数量,组成hint输出。hint输出格式为   

(强match个数,弱match个数) 。
下面就题目所给的样例进行分析:
输入样例
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

第一行是n的数值,代表输入的数字串中数字个数。

第二行是s数字串。

从第三行到倒数第二行,输入的是g数字串,对于每个输入的数字串,都要输出一个与其对应的hint。

最后一行全0代表n=4的这次游戏结束,n接着等待输入,直到0彻底结束。

观察序列1 3 5 5 和 1 1 2 3,列出它的所有match:

(1,1)(1,2)(2,4)

(1,1)满足s[1] = g[1] = 1

(1,2)满足s[1] = g[2] = 1

(2,4)满足s[2] = g[4] = 3

第一个(1,1)为强match,其余两个为弱match。

首先统计强match,得1。

其次看match(1,2),由于左边的数字已经与之前(1,1)中左边的数字相同(都为1),这两个match就是相关的,因此不能把这个match统计进去。

最后看match(2,4),与match(1,1)比较,由于左右两边数字皆不相同,这两个match无关,且(2,4)中2与4不同,为弱match,统计得1。

为了两两比较时不出现相关match,需要设置针对s和g的访问数组,访问过的数组元素不能再访,这样最后输出的hint即为(1,1)。

【代码】

#include	<stdio.h>
#include	<string.h>

int main()
{
	int n, cnt;
	int	s[1010], g[1010];
	bool vs[1010], vg[1010];//作为s,g数组的访问标志,未访问为false,访问过为true
	int strong, weak;//统计强match和弱match的数量

	cnt = 1;
	while (scanf("%d", &n) && n) {
		for (int i = 0; i < n; i ++) {
			scanf("%d", &s[i]);
		}

		printf("Game %d:\n", cnt ++);

		while (true) {
			for (int i = 0; i < n; i ++) {
				scanf("%d", &g[i]);
			}
			if (g[0] == 0) {//由于输入的数字只可能是1-9,因此只要看输入g数组的第一位即可判断是否应该输入结束
				break;
			}

			strong = weak = 0;
			memset(vs, false, sizeof (vs));
			memset(vg, false, sizeof (vg));

			for (int i = 0; i < n; i ++) {
				if (s[i] == g[i]) { //强match的条件
					strong ++;
					vs[i] = true;
					vg[i] = true;
				}
			}

			for (int i = 0; i < n; i ++) {
				for (int j = 0; j < n; j ++) {
					if (!vs[i] && !vg[j] && i != j && s[i] == g[j]) { //弱match的条件
						weak ++;
						vs[i] = true;
						vg[j] = true;
					}
				}
			}

			printf("    (%d,%d)\n", strong, weak);
		}
	}
	return 0;
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值