Donald Knuth算法为Mastermind

时间:2012-06-28 08:46:48

标签: c algorithm knuth

我正在为Mastermind(here)研究Donald Knuth 1977算法。我已经实施了一些步骤,但我不知道如何计算每个可能得分将消除的可能性数量。

Integer Bulls <- 0
WHILE Bulls <= 4
    Integer Cows <- 0
    WHILE Cows <= 4
        Integer CurrentSetSize <- 0
        WHILE CurrentSetSize <= Set.size
           // Now, I should know if this possibility can be eliminated. But how ?
        END WHILE
    END WHILE
END WHILE

你有办法吗?

感谢。

编辑:我是用C做的。现实中不是Mastermind,而是Bulls&amp;奶牛,但它是一样的。

// ASCII-compatible

#define POSSIBILITIES           6720

struct Guess {
    int i[5];
};

struct Answer {
    int bulls;
    int cows;
};

static struct Set {
    struct Guess value;
    int    score;
} *CurrentSet;

void
initSet(void)
{
    for (int i = 'a'; i <= 'h'; ++i) {
        for (int j = 'a'; j <= 'h'; ++j) {
            if (j == i) continue;
            for (int k = 'a'; k <= 'h'; ++k) {
                if (k == j || k == i) continue;
                for (int l = 'a'; l <= 'h'; ++l) {
                    if (l == k || l == j || l == i) continue;
                    for (int m = 'a'; m <= 'h'; ++m) {
                        if (m == l || m == k || m == j || m == i) continue;
                        CurrentSet->value.i[0] = i;
                        CurrentSet->value.i[1] = j;
                        CurrentSet->value.i[2] = k;
                        CurrentSet->value.i[3] = l;
                        CurrentSet->value.i[4] = m;
                        CurrentSet->score    = 0;
                    }
                }
            }
        }
    }
}

void
computeScore(int index)
{
    struct answer p;

    for (p.bulls = 0; p.bulls <= 4; ++p.bulls) {
        for (p.cows = 0; p.cows <= 4; ++p.cows) {
            for (int i = 0; i < POSSIBILITIES; ++i) {

            }
        }
    }
}

void
updateSet(void)
{
    for (int i = 0; i < POSSIBILITIES; ++i)
        computeScore(i);
}

0 个答案:

没有答案