zoukankan      html  css  js  c++  java
  • 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
    ", 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
    ", 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
    ");
        }
    }
  • 相关阅读:
    javascript 犀牛书
    Hmtl/css
    SQL Server 查询中使用Union或Union All后Order by排序无效(嵌套查询乱序)
    CSS 动画
    CSS 文字排版
    CSS 太极图
    Amazon Products 服务费估算
    CSS3 结构伪类选择器
    JS 执行机制
    vue 生命周期函数的学习
  • 原文地址:https://www.cnblogs.com/lonewanderer/p/5697153.html
Copyright © 2011-2022 走看看