A Chess Game
Time Limit: 3000MS   Memory Limit: 65536K
Total Submissions: 3551   Accepted: 1440

Description

Let's design a new chess game. There are N positions to hold M chesses in this game. Multiple chesses can be located in the same position. The positions are constituted as a topological graph, i.e. there are directed edges connecting some positions, and no cycle exists. Two players you and I move chesses alternately. In each turn the player should move only one chess from the current position to one of its out-positions along an edge. The game does not end, until one of the players cannot move chess any more. If you cannot move any chess in your turn, you lose. Otherwise, if the misfortune falls on me... I will disturb the chesses and play it again.

Do you want to challenge me? Just write your program to show your qualification!

Input

Input
contains multiple test cases. Each test case starts with a number N (1
<= N <= 1000) in one line. Then the following N lines describe the
out-positions of each position. Each line starts with an integer Xi
that is the number of out-positions for the position i. Then Xi integers
following specify the out-positions. Positions are indexed from 0 to
N-1. Then multiple queries follow. Each query occupies only one line.
The line starts with a number M (1 <= M <= 10), and then come M
integers, which are the initial positions of chesses. A line with number
0 ends the test case.

Output

There
is one line for each query, which contains a string "WIN" or "LOSE".
"WIN" means that the player taking the first turn can win the game
according to a clever strategy; otherwise "LOSE" should be printed.

Sample Input

4
2 1 2
0
1 3
0
1 0
2 0 2
0 4
1 1
1 2
0
0
2 0 1
2 1 1
3 0 1 3
0

Sample Output

WIN
WIN
WIN
LOSE
WIN

Hint

Huge input,scanf is recommended.

Source

PKU Monthly,CHEN Shixi(xreborner)

【思路】

SG函数。

把每个询问视作一组游戏,ans为每组游戏sg函数的xor值。

sg(x)=mex(S),其中mex(S)表示取后继集合所没有出现的sg值中最小的一个。

  做这道题学到了一个有意思的经验:因为vis每一层dfs都会独立用到,所以如果放在dfs外面声明为全局变量的话vis就会被接下来的dfs所覆盖。

【代码】

  

 #include<cstdio>
#include<vector>
#include<cstring>
#include<iostream>
#define FOR(a,b,c) for(int a=(b);a<(c);a++)
using namespace std; const int N = +; int n,m,sg[N];
vector<int> G[N]; int dfs(int u) {
if(sg[u]!=-) return sg[u];
int vis[N];
memset(vis,,sizeof(vis));
FOR(i,,G[u].size())
vis[dfs(G[u][i])]=;
for(int i=;;i++)
if(!vis[i]) return sg[u]=i;
}
int main() {
while(scanf("%d",&n)==) {
FOR(i,,n) G[i].clear();
int v;
FOR(i,,n) {
scanf("%d",&m);
FOR(j,,m) {
scanf("%d",&v);
G[i].push_back(v);
}
}
memset(sg,-,sizeof(sg));
while(scanf("%d",&m) && m) {
int ans=,q;
FOR(i,,m)
scanf("%d",&q) , ans^=dfs(q);
if(ans) puts("WIN"); else puts("LOSE");
}
}
return ;
}

poj 2425 A Chess Game(SG函数)的更多相关文章

  1. POJ 2425 A Chess Game 博弈论 sg函数

    http://poj.org/problem?id=2425 典型的sg函数,建图搜sg函数预处理之后直接求每次游戏的异或和.仍然是因为看不懂题目卡了好久. 这道题大概有两个坑, 1.是搜索的时候vi ...

  2. POJ 2425 A Chess Game(有向图SG函数)题解

    题意:给一个有向图,然后个m颗石头放在图上的几个点上,每次只能移动一步,如果不能移动者败 思路:dfs打表sg函数,然后求异或和 代码: #include<queue> #include& ...

  3. HDU 5724 Chess(SG函数)

    Chess Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submi ...

  4. poj 2425 A Chess Game 博弈论

    思路:SG函数应用!! 代码如下: #include<iostream> #include<cstdio> #include<cmath> #include< ...

  5. HDU 5724 Chess(SG函数+状态压缩)

    http://acm.split.hdu.edu.cn/showproblem.php?pid=5724 题意: 现在有一个n*20的棋盘,上面有一些棋子,双方每次可以选择一个棋子把它移动到其右边第一 ...

  6. 2016多校联合训练1 B题Chess (博弈论 SG函数)

    题目大意:一个n(n<=1000)行,20列的棋盘上有一些棋子,两个人下棋,每回合可以把任意一个棋子向右移动到这一行的离这个棋子最近的空格上(注意这里不一定是移动最后一个棋子),不能移动到棋盘外 ...

  7. POJ 2425 A Chess Game#树形SG

    http://poj.org/problem?id=2425 #include<iostream> #include<cstdio> #include<cstring&g ...

  8. poj 2425 A Chess Game_sg函数

    题意:给你一个有向无环图,再给你图上的棋子,每人每次只能移动一个棋子,当轮到你不能移动棋子是就输了,棋子可以同时在一个点 比赛时就差这题没ak,做了几天博弈终于搞懂了. #include <io ...

  9. HDU 5724 Chess (sg函数)

    Chess 题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5724 Description Alice and Bob are playing a s ...

随机推荐

  1. A题笔记(3)

    No. 1381 容器相关 #include <vector> 头文件 vector<Presents> present; present.push_back(name); 向 ...

  2. jquery和js cookie的使用解析

    JavaScript是运行在客户端的脚本,因此一般是不能够设置Session的,因为Session是运行在服务器端的.而cookie是运行在客户端的,所以可以用JS来设置cookie. 在这里分别通过 ...

  3. ios 将状态栏改为白色方法!

    1在Info.plist中设置UIViewControllerBasedStatusBarAppearance 为NO2 在需要改变状态栏颜色的ViewController中在ViewDidLoad方 ...

  4. Cocos2dx开发(4)——Windows环境创建Cocod2dx 3.2第一个项目HelloWorld

    本文内容:cocos2dx+VS2013环境下创建项目,部分代码简析.会的朋友可以略过. 前面简单安装了几个环境,程序完全可以顺利跑起来(其他的cocos-stadio这些需要用到再装) 1.命令行形 ...

  5. windows phone 之ListBox模板选择

    有时做项目时,会遇到一种情况:需要把获取到的数据集合显示到首页,比如新闻,但是: 新闻也分类别啊,比如:图片类新闻.文字类新闻.视频类新闻. 那我们可能采用的模板就不一样了,那么,如何根据类别来选择模 ...

  6. Linux权限体系总结

    总结 1.  LINUX9位权限及rwx字符的作用.对应的数字及对应的用户和用户组图解. 2.  rwx对文件来说代表什么意思. 3.  rwx对目录来说代表什么意思. 4.  企业生产环境目录设置权 ...

  7. [Neural Networks] Momentum

    一.目的 加快参数的收敛速度. 二.做法 另第t次的权重更新对第t+1次的权重更新造成影响. 从上式可看出,加入momentum后能够保持权重的更新方向,同时加快收敛.通常alpha的取值为[0.7, ...

  8. Linux进程和进程边界

    1. 进程和线程 2. 手机操作系统的发展 3. 进程的地址空间边界 4. 进程边界的安全围栏: Crash的不可扩延性 5. 进程边界的安全围栏: 全局数据和服务的不可访问性 http://www. ...

  9. python核心编程2第二章课后练习

    2-1 变量, print 和字符串格式化运算符.启动交互式解释器.给一些变量赋值(字符串,数值等等)并通过输入变量名显示它们的值.再用 print 语句做同样的事.这二者有何区别? 也尝试着使用字符 ...

  10. python基础 [Alex视频]

    vi hello.py#!/usr/bin/env pythonprint "hello world!"while True: print("hello world!&q ...