链接
http://poj.org/problem?id=2311
题意
给定一张 N ∗ M ( 2 ≤ n , m ≤ 200 ) N*M(2 \le n,m \le 200) N∗M(2≤n,m≤200) 的矩形网格纸,两名玩家轮流行动
在每一次行动中,可以任选一张矩形网格纸,沿着某一行或某一列的格线,把它剪成两部分
首先剪出 1 ∗ 1 1*1 1∗1 的格纸的玩家获胜
两名玩家都采取最优策略行动,求先手是否能获胜
思路
得到 1 ∗ 1 1*1 1∗1,除了会经过 1 ∗ n 1*n 1∗n 和 n ∗ 1 n*1 n∗1 这两种必胜局面外,一定会经过 2 ∗ 2 2*2 2∗2, 2 ∗ 3 2*3 2∗3, 3 ∗ 2 3*2 3∗2 这三种局面,而这三种局面往后必得到前两种必胜局面,所以这三种局面为必败局面
我们以这三种局面为最终局面进行递推
s g [ i , j ] = m e x ( ( s g [ x , j ] ⨁ s [ i − x , j ] ) ( 2 ≤ x ≤ i / 2 ) ∪ s g [ i , x ] ⨁ s [ i , j − x ] ) ( 2 ≤ x ≤ j / 2 ) ) sg[i,j]=mex((sg[x,j] \bigoplus s[i-x,j])(2\le x\le i/2)\cup sg[i,x]\bigoplus s[i,j-x])(2\le x\le j/2)) sg[i,j]=mex((sg[x,j]⨁s[i−x,j])(2≤x≤i/2)∪sg[i,x]⨁s[i,j−x])(2≤x≤j/2))
代码
#include<iostream>
#include<cstring>
using namespace std;
const int N=210;
int sg[N][N];
bool vis[N];
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
memset(sg,-1,sizeof sg);
for(int i=2;i<=N;i++)
for(int j=2;j<=N;j++)
if(sg[i][j]<0) {
memset(vis,false,sizeof vis);
for(int k=2;k<=i/2;k++) vis[sg[k][j]^sg[i-k][j]]=true;
for(int k=2;k<=j/2;k++) vis[sg[i][k]^sg[i][j-k]]=true;
for(int k=0;;k++) if(!vis[k]) {sg[i][j]=sg[j][i]=k;break;}
}
int n,m;
while(cin>>n>>m) {
if(sg[n][m]) cout<<"WIN"<<endl;
else cout<<"LOSE"<<endl;
}
return 0;
}