您的位置:首页 > 其它

Cutting Game---博弈

2016-04-04 22:54 309 查看


#include "iostream"
#include "vector"
#include "string.h"
#include "set"
using namespace std;

int mem[200][200];

int grundy(int w, int h)
{
if(mem[w][h] != -1)
return mem[w][h];
int i;
set<int> s;
for(i=2; w-i>=2; i++)  //如果切完后宽度为1,输
{
//切出的两张纸的grundy值分别为g1, g2, 这两张纸对应状态的grundy值为g1 ^ g2
s.insert(grundy(i, h) ^ grundy(w-i, h));
}
for(i=2; h-i>=2; i++)  //如果切完后高度为1,输
{
s.insert(grundy(w, i) ^ grundy(w, h-i));
}
int g = 0;
while(s.count(g))      //寻找不包含于s的最小值
g++;
return mem[w][h] = g;
}

int main()
{
int w, h;
cout << "纸的宽度 w = ";
cin >> w;
cout << "纸的高度 h = ";
cin >> h;

memset(mem, -1, sizeof(mem));
int result = grundy(w, h);
if(result)
cout << "WIN" << endl;
else
cout << "LOSE" << endl;
return 0;
}


内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: