题目大意
写一个程序游玩 Wordle 游戏:在五次内猜出一个给定的单词。单词给定首字母,每次猜测后,交互库会反馈每个位置是「字母且位置正确」「字母正确但位置错误」「完全错误」中的哪一种。
解题思路
首先,有一个显然的乱搞:每次从单词库里随机一个单词输出,然后排除掉与已知条件不相符的单词。重复这个步骤,知道只剩下一个单词。
事实上,这个乱搞并不优秀,但是在此题的评分标准下,它能得到大概 80pt 的成绩。
考虑优化这个乱搞,每次输出单词时,不要随机输出,而是对每个单词进行估价,选择一个价值最高的单词输出。我们可以定义每个单词的价值为该单词的信息量,用信息熵来表示这个单词会带来的信息量,即:
但是这样做会引起时间复杂度变高:每次猜一个新单词时,我们要对 个单词进行估价,估价一个单词为 ,这样的话时间复杂度会变成 ,尽管此题有 60s 的时间限制,仍然无法通过。考虑预处理出首字母分别为 时选择什么单词进行猜测最优,这样就可以避免第一次猜测时枚举全部单词估价而造成超时。
该优化作用比比较明显,平均每个词所用次数减少了约 ,但是仍然有少量单词无法在规定次数内猜出。
考虑这样一种情况:假设正确答案为 VAIL,程序可能依次猜测 JAIL、FAIL、BAIL、HAIL 等,而每次猜测返回的结果都为 -GGG,我们的程序会在猜测第一个字母上用掉大量交互次数。而一个比较优秀的程序会在确定三个字母时,转而构造一个包含尽量多 J、F、B、H、V 等可能为首字母的单词,从而一次排除多个可能的首字母。考虑优化程序,让其可以选择「可能成为答案的单词」之外的单词。重新设计估价函数:
定义 时,效果较好。
这时的程序就可以通过本题了,且本地测试下所有单词都可以在规定次数内猜出。
参考代码
vector<string> words, a[28], curr;
string lst;
const char hintStr[] = ".SG";
const int pwLen = 243;
int hintStrR[255];
const char *bests[] = {
"slier", "lares", "lares", "tores", "tarns", "arles", "lares", "lares", "snare",
"ousel", "ranis", "nares", "tares", "aides", "tries", "lares", "raise", "aides",
"plate", "nares", "snare", "riles", "nares", "cones", "kanes", "aeons",
};
string GetOp(string right, string guess) {
static int buk[28];
memset(buk, 0, sizeof buk);
string op(5, hintStr[0]);
rep (i, 0, 5 - 1)
if (right[i] == guess[i])
op[i] = hintStr[2];
else
++buk[right[i] - 'a'];
rep (i, 0, 5 - 1)
if (op[i] != hintStr[2] && buk[guess[i] - 'a']) op[i] = hintStr[1];
return op;
}
inline double GetE(const string &s) {
static int mp[250];
memset(mp, 0, sizeof(int) * pwLen);
each (x, curr) {
string op = GetOp(x, s);
int res = 0;
rep (i, 0, 5 - 1)
res *= 3, res += hintStrR[(int)op[i]];
++mp[res];
}
double e = 0;
rep (i, 0, pwLen) {
if (!mp[i]) continue;
double p = 1.0 * mp[i] / curr.size();
e += p * log2(1.0 / p);
}
return e;
}
inline string GetBest() {
if (curr.empty()) {
// 该分支不可能被进入
return "test";
}
double mxCurr = -1, mxWords = -1;
int idCurr = -1, idWords = -1;
rep (i, 0, curr.size() - 1) {
double res = GetE(curr[i]);
if (res > mxCurr) idCurr = i, mxCurr = res;
}
rep (i, 0, words.size() - 1) {
double res = GetE(words[i]);
if (res > mxWords) idWords = i, mxWords = res;
}
if (GetE(curr[idCurr]) > GetE(words[idWords]) - 0.01)
return curr[idCurr];
else
return words[idWords];
}
bool Ck(string x, bool *g, bool *s) {
string op = GetOp(x, lst);
rep (i, 0, 5 - 1)
if (op[i] == hintStr[0] && (g[i] || s[i]))
return 0;
else if (op[i] == hintStr[1] && !s[i])
return 0;
else if (op[i] == hintStr[2] && !g[i])
return 0;
return 1;
}
vector<string> GetFilter(bool *g, bool *s) {
vector<string> ans;
each (x, curr) {
if (Ck(x, g, s)) ans.push_back(x);
}
return ans;
}
const char *guess(int num_testcase, int remaining_guesses, char initial_letter, bool *gold,
bool *silver) {
int c = initial_letter - 'a';
if (remaining_guesses == 5) {
curr = a[c];
lst = bests[c];
return lst.c_str();
}
curr = GetFilter(gold, silver);
return (lst = GetBest()).c_str();
}
void init(int num_scramble, const char *scramble) {
rep (i, 0, 2)
hintStrR[(int)hintStr[i]] = i;
rep (i, 0, num_scramble - 1) {
string s;
rep (j, 0, 4)
s.push_back(scramble[i * 5 + j]);
words.push_back(s);
}
each (x, words)
a[x[0] - 'a'].push_back(x);
}