UVALive-7278 - Game of Cards【博弈】【sg定理】
来源:互联网 发布:淘宝速刷 编辑:程序博客网 时间:2024/05/21 07:31
UVALive-7278 - Game of Cards
题目大意:A、B两个人玩游戏。A先手,问最后谁赢。
游戏规则:
- 给出n堆纸牌,可任意选择其中一堆,记为x
- 在x的顶部可取走[0,k]张纸牌,该堆纸牌至少留下一张
- x剩下来的纸牌中,记顶部的纸牌值为y,则移除最顶部的y张纸牌(即该堆至少还剩下y张纸牌才是合法)
如果有人不能进行合法移动,则输了。
题目思路:主要是写SG函数,每一堆的sg函数都不同。最后每一堆 ans ^= sg[n],即可。
SG函数详见代码。
以下是代码:
#include <iostream>#include <iomanip>#include <fstream>#include <sstream>#include <cmath>#include <cstdio>#include <cstring>#include <cctype>#include <algorithm>#include <functional>#include <numeric>#include <string>#include <set>#include <map>#include <stack>#include <vector>#include <queue>#include <deque>#include <list>using namespace std;int k;int num[10005];int sg[10005];void sg_solve(int N){ int i,j; bool hash[N]; memset(sg,0,sizeof(sg)); for (i = 1; i <= N; i++) //第i这个状态 { memset(hash,0,sizeof(hash)); for (j = 0; j <= k; j++) //遍历所有可走的状态,每次可以取走[0,k]张 { if (i - j <= 0) continue; int bu = j + num[i - j]; //取走j张牌,还要加上对应的最后一张牌的值(因为还要移除这么多张牌) if (i - bu >= 0) { hash[sg[i - bu]] = 1; } } for (j = 0; j < N; j++) { if (!hash[j]){ sg[i] = j; break; } } }}int main(){ int p; while(cin >> p >> k) { int ans = 0; while(p--) { int t; cin >> t; memset(num,0,sizeof(num)); for (int i = 1; i <= t; i++) { cin >> num[i]; } sg_solve(t + 10); ans ^= sg[t]; } if (ans) cout << "Alice can win.\n"; else cout << "Bob will win.\n"; } return 0;}
0 0
- UVALive-7278 - Game of Cards【博弈】【sg定理】
- UVALive 7278 Game of Cards
- LA 3668 A Funny Stone Game(博弈,SG定理)
- 博弈SG定理模板
- LightOJ 1315 - Game of Hyper Knights(博弈sg函数)
- Codeforces 768E Game of Stones 博弈SG(打表)
- ACM-博弈之SG定理
- [nim博弈扩展 sg函数] UVALive 3668 A Funny Stone Game
- Game of Cards Gym
- 【POJ2311】Cutting Game-SG博弈
- uva 1378 A Funny Stone Game 博弈/组合游戏 sg定理
- codeforces603CLieges of Legendre+SG博弈
- uva10561 博弈 Treblecross 组合游戏/SG定理
- B. Game of Credit Cards
- 777Game of Credit Cards
- Codeforces777B Game of Credit Cards
- 博弈——sg函数和sg定理
- hdu1851_A Simple Game博弈—SG函数
- Android之伪装QQ后台偷偷发短信
- (uva 11732) "strcmp()" Anyone? (trie+左儿子右兄弟表示法)
- 高反差保留&阈值——素描效果——鸟儿
- 平衡二叉树(AVL树)深入解读
- HDU 3496 dp
- UVALive-7278 - Game of Cards【博弈】【sg定理】
- L2-011. 玩转二叉树-PAT团体程序设计天梯赛GPLT
- Windows下本机SSH的生成
- 在linux中如何用C语言实现读取“上下左右、ESC”键
- InvalidateRect function
- GDB详解
- Unity Mecanim动画的实现(三):Animation编辑之Root Motion
- 测试经理、自动化测试工程师工作指导书
- leetcode_c++:Add Binary(067)