UVa 10561 Treblecross (SG函数)

题意:给定上一行字符串,其中只有 X 和 . 并且没有连续的三个 X,两个玩家要分别在 . 上放 X,如果出现三个连续的 X,则该玩家胜利,现在问你先手胜还是败,如果是胜则输出第一步可能的位置。

析:首先,如果输入中出现了 XX 或者 X.X,那么先手必胜,这种可以先处理,然后考虑剩下的,首先每个玩家肯定不能放X-1,X-2,X+1,X+2的位置,其中 X 表示格子X中已经是 X 了,因为一放上,那么下一个玩家就一定能胜利,除非没有其他地方可以放了,那么游戏也就可以终止了,然后除了这些地方,那么剩下的都是可以随便放了,而且分成了好几段了,也就是每一个都是一个子游戏,这个题就是求子游戏的和,我们g(x) 表示连续的 x 个位置的对应的SG函数值,我们来推一下这个式子,g(x) = mex{ g(x-3), g(x-4), g(x-5), g(1) xor g(x-6), g(2) xor g(x-7) ... },我们来分别解释一下,如果不能理解的建议画上图来看看。g(x-3)表示把 X 放在格子的最左边,那么他右边的相邻的两个也不能放了,所以是 x-3,同理可以知道 g(x-4), g(x-5),再解释一下 g(1) xor g(x-6),首先 xor 是异或,这个意思就是已经把连续的 x 个位置已经分成两部分了,左边 1 个,右边 x-6 个,为什么是 x - 6,因为 x 左边两个,右边两个不能放,再加上左边那一个和本身的那一个。边界就是 g(0) = 0, g(1) = g(2) = g(3) = 1。然后就能求出 SG 函数,怎么求第一步的位置,枚举,所有的 . ,然后再求新的游戏和,如果是 0 ,那么就是可能的位置,否则就不是。

代码如下:

#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <cstdio>
#include <string>
#include <cstdlib>
#include <cmath>
#include <iostream>
#include <cstring>
#include <set>
#include <queue>
#include <algorithm>
#include <vector>
#include <map>
#include <cctype>
#include <cmath>
#include <stack>
#include <sstream>
#include <list>
#include <assert.h>
#include <bitset>
#include <numeric>
#define debug() puts("++++")
#define gcd(a, b) __gcd(a, b)
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define fi first
#define se second
#define pb push_back
#define sqr(x) ((x)*(x))
#define ms(a,b) memset(a, b, sizeof a)
#define sz size()
#define pu push_up
#define pd push_down
#define cl clear()
#define lowbit(x) -x&x
//#define all 1,n,1
#define FOR(i,x,n)  for(int i = (x); i < (n); ++i)
#define freopenr freopen("in.txt", "r", stdin)
#define freopenw freopen("out.txt", "w", stdout)
using namespace std;

typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int, int> P;
const int INF = 0x3f3f3f3f;
const LL LNF = 1e17;
const double inf = 1e20;
const double PI = acos(-1.0);
const double eps = 1e-8;
const int maxn = 200 + 10;
const int maxm = 100 + 2;
const LL mod = 100000000;
const int dr[] = {-1, 1, 0, 0, 1, 1, -1, -1};
const int dc[] = {0, 0, 1, -1, 1, -1, 1, -1};
const char *de[] = {"0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111", "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111"};
int n, m;
const int mon[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
const int monn[] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
inline bool is_in(int r, int c) {
  return r >= 0 && r < n && c >= 0 && c < m;
}

int g[maxn], cnt[maxn];
char s[maxn];

bool judge(){
  for(int i = 0; i < n; ++i)  if(s[i] == 'X'){
    if(i + 1 < n && s[i+1] == 'X')  return true;
    else if(i + 2 < n && s[i+2] == 'X')  return true;
  }
  for(int i = 0; i < n; ++i)  if(s[i] == 'X'){
    int j = i - 1, cnt = 2;  while(j >= 0 && cnt-- && s[j] == '.')  s[j--] = 'x';
    j = i + 1, cnt = 2;  while(j < n && cnt-- && s[j] == '.')  s[j++] = 'x';
  }
  int ans = 0;
  for(int i = 0; i < n; ++i)  if(s[i] == '.'){
    int cnt = 0;  while(s[i+cnt] == '.') ++cnt;
    i += cnt;
    ans ^= g[cnt];
  }
  return ans;
}

int main(){
  g[0] = 0;  g[1] = g[2] = g[3] = 1;
  g[4] = 2; g[5] = 2;
  for(int i = 6; i <= 200; ++i){
    for(int j = 3; j < 6; ++j)  cnt[g[i-j]] = i;
    for(int j = 6; j <= i; ++j)
      cnt[g[i-j]^g[j-5]] = i;
    for(int j = 0; ; ++j)
      if(cnt[j] != i){  g[i] = j; break; }
  }
  int T;  cin >> T;
  getchar();
  while(T--){
    fgets(s, maxn, stdin);
    n = strlen(s);
    set<int> sets;
    for(int i = 0; i < n; ++i)  if(s[i] == 'X'){
      if(i + 1 < n && s[i+1] == 'X'){
        if(i)  sets.insert(i);
        if(i+2 < n)  sets.insert(i+3);
      }
      else if(i + 2 < n && s[i+2] == 'X')  sets.insert(i+2);
    }
    if(sets.sz){
      puts("WINNING");
      for(auto it = sets.begin(); it != sets.end(); ++it)
        if(it == sets.begin())  printf("%d", *it);
        else printf(" %d", *it);
      printf("
");
      continue;
    }

    for(int i = 0; i < n; ++i)  if(s[i] == 'X'){
      int j = i - 1, cnt = 2;  while(j >= 0 && cnt-- && s[j] == '.')  s[j--] = 'x';
      j = i + 1, cnt = 2;  while(j < n && cnt-- && s[j] == '.')  s[j++] = 'x';
    }
    int ans = 0;
    for(int i = 0; i < n; ++i)  if(s[i] == '.'){
      int cnt = 0;  while(s[i+cnt] == '.') ++cnt;
      i += cnt;
      ans ^= g[cnt];
    }
    if(ans == 0){ puts("LOSING
");  continue; }
    puts("WINNING");
    for(int i = 0; i < n; ++i)  if(s[i] == 'x')  s[i] = '.';
    for(int i = 0, cnt = 0; i < n; ++i)  if(s[i] == '.'){
      s[i] = 'X';
      if(!judge()){
        if(cnt)  putchar(' ');
        printf("%d", i+1);
        ++cnt;
      }
      for(int j = 0; j < n; ++j)  if(s[j] == 'x')  s[j] = '.';
      s[i] = '.';
    }
    putchar('
');
  }
  return 0;
}

  

原文地址:https://www.cnblogs.com/dwtfukgv/p/8424211.html