UVA947 Master Mind Helper【Game】

Master Mind was invented around 1970/71 by Mordecai Meirowitz, an Israeli Postmaster/Telecommunications expert. His idea was at first turned down by many of the leading toy companies, but he persisted, and took it to the International Toy Fair at Nuremberg in February 1971, where he showed it to a small English company, Invicta Plastics Ltd. The small Leicester based company bought up the entire intellectual property rights to the game, and under the guidance of its founder Mr. Edward JonesFenleigh, refined it, and released it in 1971/72. It was an immediate hit, and went on to win the first ever Game of the Year Award in 1973. It also received a Design Centre Award, and the Queen’s Award for Export Achievement. So, for sure you have heard about this game, right?
在这里插入图片描述
    Its idea is simple: you have to break the secret color code combination that is a sequence of a number of colored pegs. You make guesses and receive feedback about your guesses, namely, the number of correct colors on correct places, and the number of correct colors on wrong places. For example, imagine a game with 9 colors and 4 positions. Let’s say that the secret code is blue, blue, yellow, red. At the start you don’t know what the code is and you make a guess: blue, green, green, yellow. The info you would receive would be that you have 1 correct color in the correct position (the first blue peg) and one correct color on a wrong place (the yellow peg, which is not on the correct position). See figure 1 to better understand this idea. The objective of the game is to guess the secret code, using as few guesses as possible.
在这里插入图片描述
Figure 1 - Explanation of a guess feedback
    We are interested in a particular property of this game. Knowing a guess and its feedback, how many combinations of colors are consistent with the answer? For example, imagine a game with 9 colors and 3 positions. We make the guess blue, red, green and receive the info 1 correct color on correct place and 2 correct colors on wrong places. How many combinations are consistent whit this? In other words, how many possible secret codes are there that would give this answer? In this case, there are exactly 3 secret codes with this feedback for this particular guess: code 1 - blue, green, red; code 2 - green, red, blue and code 3 - red, blue, green. Your task is precisely to discover this kind of numbers.
    Given a guess and its feedback, your task is to write how many secret codes are consistent with that info. In this particular problem we will only be interested in 9 color mastermind games, with 2 to 5 positions. In order to simplify, we will represent colors as numbers, starting with 1 and ending with 9. Blue will be number 1, red will be 2 and so on (note that it does not matter which color is assigned to each number; the important thing is that a different number represents a different color).
Input
The first line of input will contain a single integer N, indicating the number of test cases that follow (1 ≤ N ≤ 30).
    Then follow exactly N lines, each containing one test case consisting of three parts separated by single spaces. Each case starts with a valid guess, in a form of a string of digits (remember that the colors are coded with digits 1 to 9). This string can have from 2 to 5 digits. Then follows the feedback received in the form of two integer numbers: the first one represents the number of correct colors on correct places, and the second one represents the number of correct colors on wrong places.
Output
For each test case you must output a line containing a single integer representing the number of possible secret codes that would give that feedback for that particular guess. Remember that there are always 9 different colors and that the size of the secret code must be equal to the size of the guess.
Sample Input
5
1234 2 2
111 1 0
567 0 1
91543 5 0
91543 0 5
Sample Output
6
192
234
1
44

问题链接UVA947 Master Mind Helper
问题简述:(略)
问题分析
    游戏题,不解释。
程序说明:(略)
参考链接:(略)
题记:(略)

AC的C++语言程序如下:

/* UVA947 Master Mind Helper */

#include <bits/stdc++.h>

using namespace std;

const int N = 10;
int d1[N + 1], d2[N + 1];

pair<int, int> judge(int x, int y)
{
    int a = 0, b = 0;
    memset(d1, 0, sizeof(d1));
    memset(d2, 0, sizeof(d2));
    while(x || y) {
        if(x % 10 == y % 10) a++;
        if(y % 10 == 0) return {-1, -1};
        d1[x % 10]++, x /= 10;
        d2[y % 10]++, y /= 10;
    }
    for(int i = 0; i < N; i++)
        b += min(d1[i], d2[i]);
    return {a, b - a};
}

int main()
{
    int n, colors, a, b;
    scanf("%d", &n);
    while(n--) {
        scanf("%d%d%d", &colors, &a, &b);

        int x = 1e6, ans = 0;
        while(x > colors) x /= 10;
        for(int i = x; i < x * 10; i++)
            if(judge(colors, i) == make_pair(a, b))
                ans++;

        printf("%d\n", ans);
    }

    return 0;
}
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值