观察可以看出
(a+b)%2==1和(a+b)%2==0
两个游戏相互独立
坐标变换
(a,b)->(a-b,a+b)
然后暴力计算sg值
#include<cstdio>
#include<iostream>
#include<cstring>
#include<algorithm>
using namespace std;
const int N=22;
char ss[N][N];
int n,m;
int check(int x,int y){
if(x<0||y<0||x>=n||y>=m)return 0;
return 1;
}
int mp[N*3][N*3][N*3][N*3],ed=20;
int que(int x1,int x2,int y1,int y2){
return mp[x1][x2][y1][y2];
}
void up(int x1,int x2,int y1,int y2,int w){
mp[x1][x2][y1][y2]=w;
}
int dfs(int x1,int x2,int y1,int y2){
int sta[N*N],top=0;
if(x1>x2||y1>y2)return 0;
int tmp=que(x1,x2,y1+ed,y2+ed);
if(tmp!=-1)return tmp;
for(int i=x1;i<=x2;i+=2){
for(int j=y1;j<=y2;j+=2)if((i+j)%2==0){
int x=(i+j)/2,y=(i-j)/2;
if(!check(x,y))continue;
//printf("%d %d:%d %d %d %d\n",i,j,x1,x2,y1,y2);
if(ss[x][y]=='L'){
sta[top++]=dfs(x1,i-2,y1,y2)^dfs(i+2,x2,y1,y2);
}
else if(ss[x][y]=='R'){
sta[top++]=dfs(x1,x2,y1,j-2)^dfs(x1,x2,j+2,y2);
}
else {
sta[top++]=dfs(x1,i-2,y1,j-2)^dfs(i+2,x2,y1,j-2)^
dfs(x1,i-2,j+2,y2)^dfs(i+2,x2,j+2,y2);
}
}
}
sort(sta,sta+top);top=unique(sta,sta+top)-sta;
sta[top]=top+1;
for(int i=0;i<=top;i++)if(sta[i]!=i){
up(x1,x2,y1+ed,y2+ed,i);
//printf("%d %d %d %d %d\n",x1,x2,y1,y2,i);
return i;
}
}
int main(){
#ifdef DouBi
freopen("in.cpp","r",stdin);
#endif // DouBi
while(scanf("%d%d",&n,&m)!=EOF){
for(int i=0;i<n;i++){
scanf("%s",ss[i]);
}
int x1=0,x2=n-1+m-1;
int y1=0-(m-1),y2=n-1-0;
memset(mp,-1,sizeof(mp));
int a,b;
a=dfs(x1,x2-x2%2,y1-y1%2,y2-y2%2);
b=dfs(1,x2-(x2+1)%2,y1-(y1-1)%2,y2-(y2+1)%2);
if(a^b){
printf("WIN\n");
}
else {
printf("LOSE\n");
}
}
return 0;
}