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. UITabBar的隐藏

    方式一: // 重写导航控制器的push方法 - (void)pushViewController:(UIViewController *)viewController animated:(BOOL) ...

  2. 前端开发bower包管理器

    Bower 是 twitter 推出的一款包管理工具,基于nodejs的模块化思想,他可以很好的帮助你帮你解决js的依赖管理,比如jquery angular bootstrap 等等. 可以很方便的 ...

  3. ZOJ 1733 Common Subsequence(LCS)

    Common Subsequence Time Limit: 2 Seconds      Memory Limit: 65536 KB A subsequence of a given sequen ...

  4. [Linux]Vim的安装及使用

    1.安装:$sudo apt-get install vim 2.查看Vim所在路径$whereis vim 3.启动Vim $'/usr/bin/vim.tiny'  4. 退出Vim窗口:Ctrl ...

  5. iPhone分辨率

    分辨率和像素 1.iPhone5           4"     分辨率320x568,像素640x1136,@2x 2.iPhone6           4.7"  分辨率3 ...

  6. js中对象调用对象中的方法

    var o = {a:"abc", b:{ c:function(param){ alert(this.a); //这里的this指向的不是o而是b,所以this是没有a属性的,这 ...

  7. eclipse + maven + jboss 遇到ClassNotFoundException

    在使用eclipse + maven + jboss开发过程中,碰到ClassNotFoundException, 原因应该是deployed包中未包含maven的依赖jar. 可以通过如下方法把依赖 ...

  8. iOS 中KVC、KVO、NSNotification、delegate 总结及区别-b

    1.KVC,即是指 NSKeyValueCoding,一个非正式的Protocol,提供一种机制来间接访问对象的属性.而不是通过调用Setter.Getter方法访问.KVO 就是基于 KVC 实现的 ...

  9. Netty启动分析

    基于Netty-3.2.5 先看一段Netty的服务端代码: import java.net.InetSocketAddress; import java.util.concurrent.Execut ...

  10. jsonp跨域问题记录

    这段时间用H5做移动app开发,遇到不少之前做web的时候不曾遇到的问题,记录一下,共勉-- 首先说一个:js跨域取数的问题 描述:  之前做web都是通过后台获取数据,没考虑过跨域的问题.这次用h5 ...