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

  1. 4
  2. 2 1 2
  3. 0
  4. 1 3
  5. 0
  6. 1 0
  7. 2 0 2
  8. 0
  9.  
  10. 4
  11. 1 1
  12. 1 2
  13. 0
  14. 0
  15. 2 0 1
  16. 2 1 1
  17. 3 0 1 3
  18. 0

Sample Output

  1. WIN
  2. WIN
  3. WIN
  4. LOSE
  5. 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所覆盖。

【代码】

  

  1. #include<cstdio>
  2. #include<vector>
  3. #include<cstring>
  4. #include<iostream>
  5. #define FOR(a,b,c) for(int a=(b);a<(c);a++)
  6. using namespace std;
  7.  
  8. const int N = +;
  9.  
  10. int n,m,sg[N];
  11. vector<int> G[N];
  12.  
  13. int dfs(int u) {
  14. if(sg[u]!=-) return sg[u];
  15. int vis[N];
  16. memset(vis,,sizeof(vis));
  17. FOR(i,,G[u].size())
  18. vis[dfs(G[u][i])]=;
  19. for(int i=;;i++)
  20. if(!vis[i]) return sg[u]=i;
  21. }
  22. int main() {
  23. while(scanf("%d",&n)==) {
  24. FOR(i,,n) G[i].clear();
  25. int v;
  26. FOR(i,,n) {
  27. scanf("%d",&m);
  28. FOR(j,,m) {
  29. scanf("%d",&v);
  30. G[i].push_back(v);
  31. }
  32. }
  33. memset(sg,-,sizeof(sg));
  34. while(scanf("%d",&m) && m) {
  35. int ans=,q;
  36. FOR(i,,m)
  37. scanf("%d",&q) , ans^=dfs(q);
  38. if(ans) puts("WIN"); else puts("LOSE");
  39. }
  40. }
  41. return ;
  42. }

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. Mybatis的学习总结:mybatis的入门介绍

    一.myBatis简述 MyBatis是一个支持普通SQL查询,存储过程和高级映射的优秀持久层框架.MyBatis消除了几乎所有的JDBC代码和参数的手工设置以及对结果集的检索封装.MyBatis可以 ...

  2. ExtJs中处理时间,出现NaN-NaN-NaN的解决方式

    关键字: extjs日期格式问题(二) 一般我们的前台代码Ext.grid.ColumnModel里会这样写,以便显示日期格式: Js代码 1.....   2.{header:"birth ...

  3. ListPreference之entries和entryValues

    在使用PreferenceActivity时,碰到配置文件的ListPreference有两个属性android:entries,android:entryValues.这两个属性其实就和html的o ...

  4. python学习笔记--随时更新

    # coding=GBK score = 90 if score >= 80: print("好") elif score >= 60: print("及格& ...

  5. Qt程序开机启动的怪现象————无法正常显示程序皮肤

    事情很简单:最近公司项目在做即时通讯软件,类似QQ.该软件应该支持开机启动这样的常用功能.但是实际上开发该功能的时候碰到了个问题:开机启动程序无法正常加载皮肤文件. 这个问题让我头疼了很久啊.最终确定 ...

  6. 也谈Excel导出

    吐槽 Excel导出在天朝的软件大环境下,差点成为软件开发必备.俺就遇到过,所有报表不提供导出功能,就能不验收的囧事.报表能查看能打印能形成图表已经完美,实在搞不懂导出excel有个毛用,但是公司依靠 ...

  7. 利用青瓷布局自定义加载的场景,而不是自己改写qici-loading

    加载界面如果全部通过自己手动布局不仅不美观,还很难控制.借用原生的场景切换加载效果,来实现我们游戏的加载效果. 没有做加载修改的原来的加载顺序:   黑乎乎界面->(游戏定制的加载)你的第一个场 ...

  8. 从网页psd到html的开发

    从网上下载了一张psd的网页设计稿,初学html+css,所以记录一下我的学习过程.原图是这个样子:             原图                                   ...

  9. easy UI demo 含数据库加载示例

    easyUI 部分代码在Googlecode 托管时而被抢此文件包含了所有官方demo,作为备份 下载地址http://pan.baidu.com/s/1pJ9hS5H

  10. PHPCMS实现文章置顶功能的方法

    我个人喜欢把PHPCMS当作博客来用,而作为一个博客,怎能少了文章置顶功能呢?其中用PHPCMS实现置顶功能非常简单,无非是修改下推荐位的名称为置顶,然后在文章列表中推送需要置顶的文章罢了. 不过博客 ...