Cutting Game---博弈

来源:互联网 发布:淘宝店铺买卖 编辑:程序博客网 时间:2024/05/22 16:00

这里写图片描述

#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;}

这里写图片描述

0 0