poj 2425 A Chess Game(SG函数)
Time Limit: 3000MS | Memory Limit: 65536K | |
Total Submissions: 3551 | Accepted: 1440 |
Description
Do you want to challenge me? Just write your program to show your qualification!
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
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
Source
【思路】
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函数)的更多相关文章
- POJ 2425 A Chess Game 博弈论 sg函数
http://poj.org/problem?id=2425 典型的sg函数,建图搜sg函数预处理之后直接求每次游戏的异或和.仍然是因为看不懂题目卡了好久. 这道题大概有两个坑, 1.是搜索的时候vi ...
- POJ 2425 A Chess Game(有向图SG函数)题解
题意:给一个有向图,然后个m颗石头放在图上的几个点上,每次只能移动一步,如果不能移动者败 思路:dfs打表sg函数,然后求异或和 代码: #include<queue> #include& ...
- HDU 5724 Chess(SG函数)
Chess Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submi ...
- poj 2425 A Chess Game 博弈论
思路:SG函数应用!! 代码如下: #include<iostream> #include<cstdio> #include<cmath> #include< ...
- HDU 5724 Chess(SG函数+状态压缩)
http://acm.split.hdu.edu.cn/showproblem.php?pid=5724 题意: 现在有一个n*20的棋盘,上面有一些棋子,双方每次可以选择一个棋子把它移动到其右边第一 ...
- 2016多校联合训练1 B题Chess (博弈论 SG函数)
题目大意:一个n(n<=1000)行,20列的棋盘上有一些棋子,两个人下棋,每回合可以把任意一个棋子向右移动到这一行的离这个棋子最近的空格上(注意这里不一定是移动最后一个棋子),不能移动到棋盘外 ...
- POJ 2425 A Chess Game#树形SG
http://poj.org/problem?id=2425 #include<iostream> #include<cstdio> #include<cstring&g ...
- poj 2425 A Chess Game_sg函数
题意:给你一个有向无环图,再给你图上的棋子,每人每次只能移动一个棋子,当轮到你不能移动棋子是就输了,棋子可以同时在一个点 比赛时就差这题没ak,做了几天博弈终于搞懂了. #include <io ...
- HDU 5724 Chess (sg函数)
Chess 题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5724 Description Alice and Bob are playing a s ...
随机推荐
- Mybatis的学习总结:mybatis的入门介绍
一.myBatis简述 MyBatis是一个支持普通SQL查询,存储过程和高级映射的优秀持久层框架.MyBatis消除了几乎所有的JDBC代码和参数的手工设置以及对结果集的检索封装.MyBatis可以 ...
- ExtJs中处理时间,出现NaN-NaN-NaN的解决方式
关键字: extjs日期格式问题(二) 一般我们的前台代码Ext.grid.ColumnModel里会这样写,以便显示日期格式: Js代码 1..... 2.{header:"birth ...
- ListPreference之entries和entryValues
在使用PreferenceActivity时,碰到配置文件的ListPreference有两个属性android:entries,android:entryValues.这两个属性其实就和html的o ...
- python学习笔记--随时更新
# coding=GBK score = 90 if score >= 80: print("好") elif score >= 60: print("及格& ...
- Qt程序开机启动的怪现象————无法正常显示程序皮肤
事情很简单:最近公司项目在做即时通讯软件,类似QQ.该软件应该支持开机启动这样的常用功能.但是实际上开发该功能的时候碰到了个问题:开机启动程序无法正常加载皮肤文件. 这个问题让我头疼了很久啊.最终确定 ...
- 也谈Excel导出
吐槽 Excel导出在天朝的软件大环境下,差点成为软件开发必备.俺就遇到过,所有报表不提供导出功能,就能不验收的囧事.报表能查看能打印能形成图表已经完美,实在搞不懂导出excel有个毛用,但是公司依靠 ...
- 利用青瓷布局自定义加载的场景,而不是自己改写qici-loading
加载界面如果全部通过自己手动布局不仅不美观,还很难控制.借用原生的场景切换加载效果,来实现我们游戏的加载效果. 没有做加载修改的原来的加载顺序: 黑乎乎界面->(游戏定制的加载)你的第一个场 ...
- 从网页psd到html的开发
从网上下载了一张psd的网页设计稿,初学html+css,所以记录一下我的学习过程.原图是这个样子: 原图 ...
- easy UI demo 含数据库加载示例
easyUI 部分代码在Googlecode 托管时而被抢此文件包含了所有官方demo,作为备份 下载地址http://pan.baidu.com/s/1pJ9hS5H
- PHPCMS实现文章置顶功能的方法
我个人喜欢把PHPCMS当作博客来用,而作为一个博客,怎能少了文章置顶功能呢?其中用PHPCMS实现置顶功能非常简单,无非是修改下推荐位的名称为置顶,然后在文章列表中推送需要置顶的文章罢了. 不过博客 ...