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. [配置文件] C#修改App.config,Web.config文件帮助类,ConfigHelper (转载)

    点击下载 ConfigHelper-sufei.rar 主要功能如下 .根据Key取Value值 .根据Key修改Value .添加新的Key ,Value键值对 .根据Key删除项 /// < ...

  2. viewpager在最后一页滑动之后,跳转到主页面

    [TOC] viewpager在最后一页滑动之后,跳转到主页面 思路 主要有是两个监听, 一是addOnPageChangeListener();二是setOnTouchListener(): add ...

  3. ideadpad-安装win7

    今天帮一个同学的朋友安装win7, 竟然安不上,很久没有出现安不上系统了, 后来发现他的笔记本是新的类型,预先注意事项为: 1,启动的时候,使用 Fn+F2进入 BIOS. 2,将Boot中的Boot ...

  4. SQL函数大全(字符串函数).

    SQL Server 2005  函数大全 字符串函数 字符串函数 SubString在SQL和C#中不同, 一,select  substring('abcde',-1,3) select LEN( ...

  5. oracle 非空闲等待事件排查

    想必大家都知道Oracle的等待时间分为两种,一种我们称之为“空闲等待事件”,另外一种称之为“非空闲等待事件”.“空闲等待事件”——作为DBA可以不用过分的关注这类等待事件.“非空闲等待事件”——当D ...

  6. maven依赖缺少oracle驱动包

    maven添加oracle jdbc驱动的依赖问题 在maven工程里不能下载oracle的jdbc驱动jar包,需要Oracle官方授权才可以,找一个ojdbc14的10.2.0.1.0或10.2. ...

  7. C语言之分配

    #include "stdio.h" void main() { ] = {,,}; *sizeof(int));//malloc calloc relloc arr2[] = ; ...

  8. java中关于static的小知识

    static能够修饰属性和方法.凡是static修饰的方法和属性都是和类的关系较大,都在加载的时候要特殊处理(包括属性和类的优先加载).下面比较下static修饰属性和方法时的区别: 一.修饰属性的时 ...

  9. 基于matlab的GUI界面开发软件

    matlab工具deploytool编译M为可执行程序 http://blog.sina.com.cn/s/blog_60f8483a0100gnsm.html

  10. 从内部剖析C#集合之HashTable

    计划写几篇文章专门介绍HashTable,Dictionary,HashSet,SortedList,List 等集合对象,从内部剖析原理,以便在实际应用中有针对性的选择使用. 这篇文章先介绍Hash ...