列表

详情


NC51073. Cutting Game

描述

Urej loves to play various types of dull games. He usually asks other people to play with him. He says that playing those games can show his extraordinary wit. Recently Urej takes a great interest in a new game, and Erif Nezorf becomes the victim. To get away from suffering playing such a dull game, Erif Nezorf requests your help. The game uses a rectangular paper that consists of W*H grids. Two players cut the paper into two pieces of rectangular sections in turn. In each turn the player can cut either horizontally or vertically, keeping every grids unbroken. After N turns the paper will be broken into N+1 pieces, and in the later turn the players can choose any piece to cut. If one player cuts out a piece of paper with a single grid, he wins the game. If these two people are both quite clear, you should write a problem to tell whether the one who cut first can win or not.

输入描述

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;
}

上一题