NC51073. Cutting Game
描述
输入描述
The input contains multiple test cases. Each test case contains only two integers W and H in one line, which are the width and height of the original paper.
输出描述
For each test case, only one line should be printed. If the one who cut first can win the game, print "WIN", otherwise, print "LOSE".
示例1
输入:
2 2 3 2 4 2
输出:
LOSE LOSE WIN
C++14(g++5.4) 解法, 执行用时: 143ms, 内存消耗: 512K, 提交时间: 2020-09-22 09:29:36
#include <cstdio> #include <cstring> #include <iostream> using namespace std; const int N = 206; int n, m, sg[N][N]; int SG(int x, int y) { bool f[N]; memset(f, 0, sizeof(f)); if (sg[x][y] != -1) return sg[x][y]; for (int i = 2; i <= x - i; i++) f[SG(i,y)^SG(x-i,y)] = 1; for (int i = 2; i <= y - i; i++) f[SG(x,i)^SG(x,y-i)] = 1; int t = 0; while (f[t]) ++t; return sg[x][y] = t; } int main() { memset(sg, -1, sizeof(sg)); sg[2][2] = sg[2][3] = sg[3][2] = 0; while (cin >> n >> m) puts(SG(n, m) ? "WIN" : "LOSE"); return 0; }
C++ 解法, 执行用时: 13ms, 内存消耗: 556K, 提交时间: 2022-01-06 17:46:05
#include<bits/stdc++.h> using namespace std; int sg[201][201],b[201],w,h; int main(){ for(int n=2;n<201;n++)for(int m=2;m<201;m++){ memset(b,0,sizeof(b)); for(int i=2;n-i>1;i++)b[sg[i][m]^sg[n-i][m]]=1; for(int i=2;m-i>1;i++)b[sg[n][i]^sg[n][m-i]]=1; for(int i=0;i<201;i++)if(!b[i]){sg[n][m]=i;break;} } while(cin>>w>>h)cout<<(sg[w][h]?"WIN":"LOSE")<<"\n"; return 0; }