HDU 1524 A Chess Game(SG函数)

Problem 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

#include <iostream>
#include <cstring>
#include <cstdlib>
#include <cstdio>
#include <cmath>
#include <vector>
#include <map>
#include <set>
using namespace std;
const int MAXN = 1000 + 10;
vector<int>G[MAXN];
int SG[MAXN];
int mex(int x)
{
    if(SG[x] != -1)
        return SG[x];
    bool vis[MAXN];
    memset(vis, 0, sizeof(vis));
    int sz = G[x].size();
    for(int i=0;i<sz;i++)
        vis[mex(G[x][i])] = 1;
    for(int i=0;;i++) if(!vis[i])
    {
        SG[x] = i;
        break;
    }
    return SG[x];
}
int main()
{
    int n;
    while(cin>>n)
    {
        memset(SG, -1, sizeof(SG));
        for(int i=0;i<n;i++) G[i].clear();
        for(int i=0;i<n;i++)
        {
            int m;
            cin>>m;
            while(m--)
            {
                int x;
                cin>>x;
                G[i].push_back(x);
            }
        }
        int Q;
        while(cin>>Q)
        {
            if(Q == 0)
                break;
            int ans = 0;
            while(Q--)
            {
                int x;
                cin>>x;
                ans ^= mex(x);
            }
            if(ans) puts("WIN");
            else puts("LOSE");
        }
    }
    return 0;
}
时间: 2024-10-12 17:10:33

HDU 1524 A Chess Game(SG函数)的相关文章

hdu 1524 A Chess Game (SG)

题意:在一个有向无环图上有n个顶点,每一个顶点都只有一个棋子,有两个人,每次根据这个图只能将任意一颗棋子移动一步 ,如果到某一步玩家不能移动时,那么这个人就输. 分析:本题是最典型的有向无环图的博弈,利用dfs把所有顶点的SG值都计算出来,然后对每个棋子的SG值进行异或运算,如果 为0就是先手必败,否则就是先手必胜. 如果某个人移动到出度为0的顶点,那么他必败,在这里首先介绍一下什么是SG函数. 对于给定的有向无环图,定义图中每个顶点的Sprague-Grundy函数g如下:g(x) = mex

HDU 1524 - A Chess Game(sg函数 + dfs)

A Chess Game Problem 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

HDU 5724 Chess(SG函数)

Chess Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 2605    Accepted Submission(s): 1092 Problem Description Alice and Bob are playing a special chess game on an n × 20 chessboard. There are s

HDU 2897-邂逅明下(sg函数)

邂逅明下 Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Submit Status Practice HDU 2897 Appoint description:  System Crawler  (2015-03-13) Description 当日遇到月,于是有了明.当我遇到了你,便成了侣. 那天,日月相会,我见到了你.而且,大地失去了光辉,你我是否成侣?这注定是个凄美的故事.(以上是废

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

题目大意:一个n(n<=1000)行,20列的棋盘上有一些棋子,两个人下棋,每回合可以把任意一个棋子向右移动到这一行的离这个棋子最近的空格上(注意这里不一定是移动最后一个棋子),不能移动到棋盘外,不能移动了就算输,两个人都用最优策略,问先手是否有必胜策略. 这题显然就是SG函数了吧.行与行之间互不影响,所以可以看成n个子游戏,求出它们各自的SG函数然后异或一下就可以了.我们发现只有20列,2^21=2097152,所以我们可以先把行的所有情况的SG函数预处理出来,然后每次询问O(1)就行了. 代

hdu 1524 A Chess Game 博弈之,SG函数简单题

A Chess Game Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 1489    Accepted Submission(s): 679 Problem Description Let's design a new chess game. There are N positions to hold M chesses in th

hdu 1536 S-Nim (简单sg函数)

题意:首先输入K 表示一个集合的大小  之后输入集合 表示对于这对石子只能去这个集合中的元素的个数 之后输入 一个m 表示接下来对于这个集合要进行m次询问 之后m行 每行输入一个n 表示有n个堆  每堆有n1个石子  问这一行所表示的状态是赢还是输 如果赢输入W否则L 思路:sg打表一下 #include <iostream> #include <cstring> using namespace std; const int maxn=10008; int f[maxn],n;//

HDU 3032-Nim or not Nim?(sg函数打表)

Nim or not Nim? Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 1099    Accepted Submission(s): 547 Problem Description Nim is a two-player mathematic game of strategy in which players take tur

hdu 3032 Nim or not Nim? sg函数

Nim or not Nim? Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 1056    Accepted Submission(s): 523 Problem Description Nim is a two-player mathematic game of strategy in which players take turn