码迷,mamicode.com
首页 > 其他好文 > 详细

UVA 10561 Treblecross

时间:2016-07-22 22:52:47      阅读:167      评论:0      收藏:0      [点我收藏+]

标签:

白书上的例题,用长度作为状态推算sg值。代码写的有点挫。

#include<complex>
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
#include<vector>
using namespace std;
vector<int> ans;
char s[300]; int g[210];
int len;
int getG(int num) {
    int vis[210];
    memset(vis, 0, sizeof(vis));
    for(int i = 1; i <= num; i++) {
        int t = 0;
        if(num - i - 2 >= 0)
            t ^= g[num-i-2];
        if(i - 3 >= 0)
            t ^= g[i-3];
        vis[t] = 1;
    }
    for(int i = 0; ; i++)
        if(!vis[i])
            return i;
}
void init() {
    memset(g, 0, sizeof(g));
    g[1] = g[2] = g[3] = 1;
    for(int i = 4; i <= 200; i++) {
        g[i] = getG(i);
    }
}
bool check(int flag) {
    int ret = 0;
    int vis[210]; memset(vis, 0, sizeof(vis));
    for(int i = 1; i <= len; i++) {
        if(s[i] == X) {
            if(i - 2 >= 1) {
                vis[i-2] = 1;
            }
            if(i - 1 >= 1) {
                vis[i-1] = 1;
            }
            if(i + 1 <= len) {
                vis[i+1] = 1;
            }
            if(i + 2 <= len) {
                vis[i+2] = 1;
            }
            vis[i] = 1;
        }
    }
    int cnt = 0;
    /*if(flag == 21)
        for(int i = 1; i <= len; i++)
            printf("%d ", vis[i]);
    puts("");*/
    for(int i = 1; i <= len; i++) {
        if(!vis[i]) cnt++;
        else {
            ret ^= g[cnt];
            cnt = 0;
        }
    }
    ret ^= g[cnt];
    /*if(flag == 1)
        printf("%d\n", ret);*/
    return ret == 0;
}

bool solve() {
    len = strlen(s + 1);

    int vis[210]; memset(vis, 0, sizeof(vis));
    for(int i = 1; i <= len; i++) {
        if(s[i] == X) {
            if(i - 2 >= 1) {
                vis[i-2] = 1;
            }
            if(i - 1 >= 1) {
                vis[i-1] = 1;
            }
            if(i + 1 <= len) {
                vis[i+1] = 1;
            }
            if(i + 2 <= len) {
                vis[i+2] = 1;
            }
            vis[i] = 1;
        }
    }
    for(int i = 1; i <= len; i++) {
        if(i - 1 >= 1 && i + 1 <= len && s[i-1] == X && s[i+1] == X)
            ans.push_back(i);
        if(i - 2 >= 1 && s[i-2] == X && s[i-1] == X)
            ans.push_back(i);
        if(i + 2 <= len && s[i+1] == X && s[i+2] == X)
            ans.push_back(i);
    }
    if(ans.size() != 0) return 1;
    for(int i = 1; i <= len; i++) {
        if(s[i] == . && !vis[i]) {
            //printf("check %d\n", i);
            s[i] = X;
            if(check(i))
                ans.push_back(i);
            s[i] = .;
        }
    }
    return ans.size() != 0;
}

int main() {
    /*freopen("in.txt", "r", stdin);
    freopen("out1.txt", "w", stdout);*/
    init();
    int t;
    //for(int i = 0; i <= 200; i++) printf("%d ", g[i]);
    scanf("%d", &t);
    while(t--) {
        ans.clear();
        scanf("%s", s + 1);
        bool flag = solve();
        if(flag) {
            puts("WINNING");
            for(int i = 0; i < ans.size(); i++){
                if(i == 0) printf("%d", ans[i]);
                else printf(" %d", ans[i]);
            }puts("");
        }
        else puts("LOSING\n");
    }
}

 

UVA 10561 Treblecross

标签:

原文地址:http://www.cnblogs.com/lonewanderer/p/5697153.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!