POJ2311 Cutting Game
题目来源:POJ2311 Cutting Game
题意
给定一张
Solution
可以任选一张矩形网格纸,显然是一个独立的有向图游戏。
考虑SG函数的构造方式,需要找出一个必败的局面作为有向图有向图的起点。
因为剪出
对于一张独立的网格纸,其SG函数是固定的,可以通过记忆化来快速求解。
考虑如何计算
需要注意,任何策略下不应该存在
通过记忆化搜索便可以求出答案,考虑总体时间复杂度为
Code
点击查看代码
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <set>
#include <cmath>
using namespace std;
const int N = 250;
typedef long long lld;
inline int read() {
register int w = 0, f = 1;
register char c = getchar();
while (c > '9' || c < '0') {
if (c == '-') f = -1;
c = getchar();
}
while (c >= '0' && c <= '9') {
w = w * 10 + c - '0';
c = getchar();
}
return w * f;
}
int n, m;
int sg[N][N];
inline int SG(register int x, register int y) {
if (sg[x][y] != -1) return sg[x][y];
register bool f[N];
memset(f, 0, sizeof (f));
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;
register 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 (~scanf("%d%d", &n, &m)) puts(SG(n, m) ? "WIN" : "LOSE");
return 0;
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 地球OL攻略 —— 某应届生求职总结
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 提示词工程——AI应用必不可少的技术
· .NET周刊【3月第1期 2025-03-02】