填字母游戏 Posted on 2019-04-21 | Edited on 2019-09-20 | In 蓝桥杯 | Views: 题意一个 $1*N$ 个格子, 上面已有一些 L 和 O, 两人博弈, 谁先拼出 LOL 谁获胜, 空格数<14 题解状态标记搜索 代码123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138#include <bits/stdc++.h>using namespace std;#define forl(i, l, r) for (int i = l; i <= r; i++)#define forr(i, r, l) for (int i = r; i >= l; i--)#define for1(i, n) for (int i = 1; i <= n; i++)#define fro1(i, n) for (int i = 1; i <= n; i++)#define for0(i, n) for (int i = 0; i < n; i++)#define fro0(i, n) for (int i = 0; i < n; i++)#define meminf(a) memset(a, inf, sizeof(a))#define mem_1(a) memset(a, -1, sizeof(a))#define mem0(a) memset(a, 0, sizeof(a))#define oper(type) bool operator <(const type y)const#define mp make_pair#define pb push_back#define fi first#define se secondtypedef pair<long long, long long> pll;typedef vector<long long> vll;typedef pair<int, int> pii;typedef unsigned long long ull;typedef vector<int> vii;typedef long double db;typedef long long ll;typedef int itn;int in(int &a,int &b,int &c,int &d){return scanf("%d%d%d%d",&a,&b,&c,&d);}int in(int &a,int &b,int &c){return scanf("%d%d%d",&a,&b,&c);}int in(int &a,int &b){return scanf("%d%d",&a,&b);}int in(ll &a){return scanf("%lld",&a);}int in(int &a){return scanf("%d",&a);}int in(char *s){return scanf("%s",s);}int in(char &s){return scanf("%c",&s);}int in(db &a){return scanf("%Lf",&a);}void out(int a){printf("%d",a);}void outln(int a){printf("%d\n",a);}void out(ll a){printf("%lld",a);}void outln(ll a){printf("%lld\n",a);}const db pi = acos((db)-1);const ll inf =0x3f3f3f3f;const db eps = 1e-8;const int N = 1.6e6;const ll mod = 1e9+7;int sign(db a) { return a < -eps ? -1 : a > eps;}int db_cmp(db a, db b){ return sign(a-b); }int ans[N];ll pow3[23];bool iskong[23];// int wei[23];int len;// int cnt;int fun(ll status){ // cnt++; int ans_status=0; ll tmp_sta=status; int *wei=new int[23]; bool have0=0; for0(i,len){ int flag=tmp_sta%3; wei[i]=flag; if(flag==0)have0=1; if(iskong[i]){ ans_status*=3; ans_status+=flag; } tmp_sta/=3; } if(ans[ans_status]<inf){ delete [] wei; return ans[ans_status]; } for(int i=0;i+2<len;i++){ if(wei[i]==1&&wei[i+1]==2&&wei[i+2]==1){ delete [] wei; return ans[ans_status]=-1; } } if(!have0){ delete [] wei; return ans[ans_status]=0; } // if(x.find("LOL") != -1)return -1; // if(x.find("*") == -1)return 0; int res = -1; delete [] wei; // delete tmp_sta; tmp_sta=status; for0(i,len){ int flag=tmp_sta%3; if(flag==0){ res = max(res,-fun(status+pow3[i])); res = max(res,-fun(status+2*pow3[i])); if(res==1)break; } tmp_sta/=3; } // for(int i = 0;x[i];i++) // if(x[i] == '*'){ // x[i] = 'L'; // res = max(res,-fun(x)); // if(res == 1)return x[i] = '*',1; // x[i] = 'O'; // res = max(res,-fun(x)); // x[i] = '*'; // } return ans[ans_status]=res;}int main(){ // clock_t wei=clock(); int t; in(t); char s[23]; pow3[0]=1; for1(i,22){ pow3[i]=pow3[i-1]*3; } while(t--){ // cnt=0; meminf(ans); mem0(iskong); in(s); // cout<<s<<endl; ll tmp=0; len=strlen(s); assert(len<23); // cout<<len<<endl; for0(i,len){ tmp*=3; if(s[i]=='L')tmp+=1; else if(s[i]=='O')tmp+=2; else iskong[len-i-1]=1; } outln(fun(tmp)); // outln(cnt); } // printf("%lu\n",clock()-wei); return 0;}