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 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 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;}
- UVa 340 Master-Mind Hints
- uva 340 Master-Mind Hints
- UVA 340 - Master-Mind Hints
- UVa 340 - Master-Mind Hints
- UVa 340 Master-Mind Hints
- UVA 340 Master-Mind Hints
- uva 340 Master-Mind Hints
- UVA 340 - Master-Mind Hints
- UVa 340 - Master-Mind Hints
- UVa 340 - Master-Mind Hints
- UVA 340 Master-Mind Hints
- uva 340 Master-Mind Hints
- UVa - 340 - Master-Mind Hints
- UVa 340 - Master-Mind Hints
- uva 340 Master-Mind Hints
- uva - 340 - Master-Mind Hints
- UVa 340 - Master-Mind Hints
- UVa:340 - Master-Mind Hints
- 线段树学习——入门篇
- VC实现HTTP协议的GET和POST方法
- Linux内核中实现生产者与消费者(避免无效唤醒)
- 一份130+题的题目列表
- CentOS LNMP安装
- UVa 340 Master-Mind Hints
- 创建oracle数据库,用户、表空间、设置角色权限脚本
- linux 网络编程相关结构体
- 分组并排序
- 从头开始写java--第三篇:最简单的java程序
- 使用批处理命令移动子目录下的所有文件到当前目录下,并删除子目录
- 感受傅立叶数学之美
- 堆与栈详解
- NVARCHAR 和VARCHAR区别和使用