POJ 2311 Cutting Game (博弈)
题意:给定一个长方形纸张,每次只能水平或者垂直切,如果切到1*1的方格就胜,问先手胜还是负。
析:根据Nim游戏可知,我们可以分别求出每个子游戏的和,就是答案,所以我们就枚举每一种切法,然后求出SG函数,那么就能得到答案。
代码如下:
#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <cstdio>
#include <string>
#include <cstdlib>
#include <cmath>
#include <iostream>
#include <cstring>
#include <set>
#include <queue>
#include <algorithm>
#include <vector>
#include <map>
#include <cctype>
#include <cmath>
#include <stack>
#include <sstream>
#define debug() puts("++++");
#define gcd(a, b) __gcd(a, b)
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define freopenr freopen("in.txt", "r", stdin)
#define freopenw freopen("out.txt", "w", stdout)
using namespace std; typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int, int> P;
const int INF = 0x3f3f3f3f;
const double inf = 0x3f3f3f3f3f3f;
const double PI = acos(-1.0);
const double eps = 1e-8;
const int maxn = 200 + 10;
const int mod = 1e6 + 10;
const int dr[] = {-1, 0, 1, 0};
const int dc[] = {0, 1, 0, -1};
const char *de[] = {"0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111", "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111"};
int n, m;
const int mon[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
const int monn[] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
inline bool is_in(int r, int c){
return r >= 0 && r < n && c >= 0 && c < m;
}
int dp[maxn][maxn]; int dfs(int r, int c){
int &ans = dp[r][c];
if(ans >= 0) return ans;
set<int> sets;
for(int i = 2; r - i > 1; ++i) sets.insert(dfs(r-i, c) ^ dfs(i, c));
for(int i = 2; c - i > 1; ++i) sets.insert(dfs(r, i) ^ dfs(r, c-i));
int res = 0;
while(sets.count(res)) ++res;
return ans = res;
} int main(){
memset(dp, -1, sizeof dp);
while(scanf("%d %d", &m, &n) == 2){
printf("%s\n", dfs(m, n) ? "WIN" : "LOSE");
}
return 0;
}
POJ 2311 Cutting Game (博弈)的更多相关文章
- POJ 2311 Cutting Game(Nim博弈-sg函数/记忆化搜索)
Cutting Game 题意: 有一张被分成 w*h 的格子的长方形纸张,两人轮流沿着格子的边界水平或垂直切割,将纸张分割成两部分.切割了n次之后就得到了n+1张纸,每次都可以选择切得的某一张纸再进 ...
- POJ 2311 Cutting Game(二维SG+Multi-Nim)
Cutting Game Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 4798 Accepted: 1756 Desc ...
- POJ 2311 Cutting Game (Multi-Nim)
[题目链接] http://poj.org/problem?id=2311 [题目大意] 给出一张n*m的纸,每次可以在一张纸上面切一刀将其分为两半 谁先切出1*1的小纸片谁就赢了, [题解] 如果切 ...
- POJ 2311 Cutting Game(SG函数)
Cutting Game Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 4806 Accepted: 1760 Desc ...
- poj 2311 Cutting Game 博弈论
思路:求SG函数!! 代码如下: #include<iostream> #include<cstdio> #include<cmath> #include<c ...
- POJ 2311 Cutting Game(SG+记忆化)
题目链接 #include<iostream> #include<cstdio> #include<cstring> using namespace std; ][ ...
- POJ 2311 Cutting Game [Multi-SG?]
传送门 题意:n*m的纸片,一次切成两份,谁先切出1*1谁胜 Multi-SG? 不太一样啊 本题的要求是后继游戏中任意游戏获胜就可以了.... 这时候,如果游戏者发现某一单一游戏他必败他就不会再玩了 ...
- POJ 2311 Cutting Game(SG函数)
题目描述 意思就是说两个人轮流剪纸片,直到有一个人剪出1*1的方格就算这个人赢了.然后给出纸片的长和宽,求先手会赢还是会输 (1<=n,m<=200) 题解 看了一眼,这不是裸的SG吗 啪 ...
- poj 2311 Cutting Game (SG)
题意: 有一张W*H的纸片. 每人每次可以横着撕或者竖着撕,先撕出1*1那一方胜. 数据范围: W and H (2 <= W, H <= 200) 思路: 很好抽象出游戏图的模型,用SG ...
随机推荐
- 用CMakeLists.txt组织工程
1 一个工程会有多个CMakeLists.txt,如何组织这些CMakeLists.txt来构建一个工程? 1.1 最外层一个CMakeLists.txt,是总的CMakeList.txt,在这个里 ...
- 偏函数应用 pfa
- 爬虫-Selenium -抱错ElementNotVisibleException: Message: element not visible
1.当使用Selenium IDE 完成了脚本的录制和回放通过后,想要将脚本转换为其他语言如java.Python等,首次使用时打开Options->Format发现没有可以转换的语言,如下: ...
- eclipse 安装tomcat
- ubuntu nohup命令用法
让程序在后台运行 该命令的一般形式nohup command & 程序在后台运行并打印日志 nohup ./china_fund.py > china_fund.file 2>&a ...
- [LeetCode] 698. Partition to K Equal Sum Subsets
Problem Given an array of integers nums and a positive integer k, find whether it's possible to divi ...
- 服务器中判断客户端socket断开连接的方法【转】
本文转载自:http://www.cnblogs.com/jacklikedogs/p/3976208.html 1, 如果服务端的Socket比客户端的Socket先关闭,会导致客户端出现TIME_ ...
- 简单的C++程序题总结
1.求一个数的二进制中1的个数. 思想的关键在于x=x&(x-1)这里,例如二进制为0x0729,即x=0000 0111 0010 1001,那么x-1=0000 0111 0010 100 ...
- SDUT OJ I样(0-1背包问题 【模板】)
I样 Time Limit: 1000ms Memory limit: 65536K 有疑问?点这里^_^ 题目描述 这是个什么问题呢?DP,贪心,数据结构,图论,数论还是计算几何?管他呢,反正 ...
- Java内部类复习
package com.t_06; import org.junit.Test; import com.t_06.StaticClass.StaticInnerClass; /** * 一个类的定义放 ...