Iva*_*van 5 algorithm artificial-intelligence
有一天,我被问到这样一个问题,两个玩家(A,B)和四个位置,每个玩家在这些位置上放“ N”或“ O”,谁先拼写“ NON”赢得了这场比赛。有策略玩家A或玩家B一定会成功吗?我对此不太熟悉,因此他在以下情况下给出了一些提示,无论A提出什么,B都会成功。
[N(A看跌期权)| _ | _ | N(B看跌期权)]
首先,A将N放置在此数组的第一个索引中,然后B将N放置在最后一个位置。然后,无论A放置在什么位置,A都会赢。
所以问题是,如果将插槽添加到7个插槽,是否有相同的策略?
[_ | _ | _ | _ | _ | _ | _]
我想过类似四个灵魂的情况,但是需要这样的前提条件。我不确定这背后是否有任何理论。
[N | _ | _ | N | _ | _ | 】
第一个玩家将永远赢得这场比赛。获胜棋步是 _ _ _ N _ _ _
由于只有7个槽位,所以这个游戏只有3^7种状态。因此每个状态都可以通过动态规划轻松计算。这是我的 C++ 解决方案
#include <cstdio>
#include <string>
#include <map>
#include <iostream>
using namespace std;
map<string, string> mp;
string go(string s) {
if (mp.find(s) != mp.end()) {
return mp[s];
}
if (s.find("_") == -1) {
cout<<s<<" "<<"DRAW"<<endl;
return mp[s] = "DRAW";
}
string s1 = s;
bool draw_found = false;
for (int i = 0; i < s.size(); ++i) {
if (s[i] == '_') {
string t = "NO";
for (int j = 0; j < t.size(); ++j) {
s[i] = t[j];
if (s.find("NON") != -1) {
cout<<s1<<" WIN by move: "<<s<<endl;
return mp[s1] = "WIN";
}
string r = go(s);
if (r == "LOSE") {
cout<<s1<<" "<<" WIN by move: "<<s<<endl;
return mp[s1] = "WIN";
}
else if (r == "DRAW") {
draw_found = true;
}
s[i] = 'O';
}
s[i] = '_';
}
}
if (draw_found) {
cout<<s<<" "<<"DRAW"<<endl;
return mp[s] = "DRAW";
}
cout<<s<<" "<<"LOSE"<<endl;
return mp[s] = "LOSE";
}
int main (void) {
string s;
for (int i = 0; i < 7; ++i) {
s += "_";
}
string g = go(s);
cout<<g<<endl;
return 0;
}
Run Code Online (Sandbox Code Playgroud)