UVA 10561 Treblecross

白书上的例题,用长度作为状态推算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");
    }
}

 

posted @ 2016-07-22 21:13  MartinEden  阅读(162)  评论(0编辑  收藏  举报