UVA - 10561 Treblecross (博弈数学&SG函数)

Time Limit: 3000MS  64bit IO Format: %lld & %llu

 Status uDebug

Description

Download as PDF

借鉴自:http://blog.csdn.net/yeyeyeguoguo/article/details/47068427


题意:

n个格子排成一行,其中一些格子有字符X。两个游戏中轮流操作,每次可以将一个X放在空格上。若出现连续3个X,则该游戏中胜。给你一串字符问你先手必胜还是必败,必胜的话输出所有必胜策略(就是当前局势下你走哪一步可以确认为必胜,所有种类都要输出,理解了半天)

分析:

一行格子可被X分为两部分,一部分为X及其禁区(左右半径两格内),另一部分为安全区域可进行子游戏,根据SG定理,可通过计算若干个游戏的和来得到最终结果。

SG函数可通过SG定理得到,借鉴的大神代码,思路清晰,比白书好懂多了。。

#include <algorithm>
#include <iostream>
#include <sstream>
#include <cstring>
#include <cstdlib>
#include <string>
#include <vector>
#include <cstdio>
#include <cmath>
#include <queue>
#include <stack>
#include <map>
#include <set>
using namespace std;
#define INF 0x3f3f3f3f
const long long N=205;
const long long mod=1e9;
const double PI=acos(-1.0);
typedef long long ll;

int t,res[N],way,len,sg[N];
char str[N];
int judge() {
    for (int i = 0; i < len - 2; i++) {
        if (str[i] == 'X' && str[i + 1] == 'X' && str[i + 2] == 'X')
            return true;
    }
    return false;
}

int SG(int x) {
    int vis[N];
    if (sg[x] != -1) return sg[x];
    if (x == 0) return sg[x] = 0;
    memset(vis, false, sizeof(vis));
    for (int i = 1; i <= x; i++) {
        int t = SG(max(0, i - 3))^SG(max(0, x - i - 2));
        vis[t] = true;
    }
    for (int i = 0; i < N; i++) {
        if (vis[i]) continue;
        return sg[x] = i;
    }
    return 0;
}

int GetRes() {
    for (int i=0; i<len; i++) {
        if (str[i]=='.') {
            str[i]='X';
            if (judge()) {
                str[i]='.';
                return false;
            }
            str[i]='.';
        }
    }
    int ans=0,num=0;
    for (int i=0; i<len; i++) {
        if (str[i] == 'X' || (i >= 1 && str[i - 1] == 'X') || (i >= 2 && str[i - 2] == 'X') || (i + 1 < len && str[i + 1] == 'X') || (i + 2 < len && str[i + 2] == 'X')) {
            ans ^= SG(num);
            num = 0;
        }
        else num++;
    }
    ans^=SG(num);
    return ans==0;
}

void solve() {
    way=0;
    len=strlen(str);
    for (int i=0; i<len; i++) {
        if (str[i]!='.') continue;
        str[i]='X';
        if (judge()||GetRes()) {
            res[way++]=i+1;
        }
        str[i]='.';
    }
}

int main() {
    memset(sg, -1, sizeof(sg));
    cin>>t;
    while (t--) {
        scanf("%s",str);
        solve();
        if (way==0) cout<<"LOSING\n\n";
        else {
            cout<<"WINNING\n"<<res[0];
            for (int i=1; i<way; i++) {
                cout<<" "<<res[i];
            }
            cout<<endl;
        }
    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值