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 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

Source

PKU Monthly

题意是给出一个有向无环图,在图上有若干棋子,轮流移动棋子,轮到谁不能动了,他就输了。

简单的求sg函数,dfs搞一搞。

#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
vector<int> r[1010];
int g[1010];
void init() {
    memset(g, -1, sizeof(g));
    for(int i = 0; i <= 1005; i++) {
        r[i].clear();
    }
}
int getSG(int x) {
    if(g[x] != -1) return g[x];
    if(r[x].size() == 0) return 0;
    int vis[1010];
    memset(vis, 0, sizeof(vis));
    for(int i = 0; i < r[x].size(); i++) {
        g[r[x][i]] = getSG(r[x][i]);
        vis[g[r[x][i]]] = 1;
        //puts("check");
    }
    for(int i = 0; ; i++) if(!vis[i])
        return i;
    //puts("check");
}
int main() {
    int n;
    while(~scanf("%d", &n)) {
        init();
        for(int i = 0; i < n; i++) {
            int m; scanf("%d", &m);
            for(int j = 1; j <= m; j++) {
                int v;
                scanf("%d", &v);
                r[i].push_back(v);
            }
        }
        int m, v, flag;
        while(scanf("%d", &m)) {
            if(m == 0) break;
            flag = 0;
            while(m--) {
                scanf("%d", &v);
                if(g[v] == -1)
                    g[v] = getSG(v);
                flag ^= g[v];
            }
            if(flag == 0) puts("LOSE");
            else puts("WIN");
        }
    }
}
时间: 2024-12-25 09:29:56

HDU 1524 - A Chess Game(sg函数 + dfs)的相关文章

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 connec

hdu 1524 A Chess Game (SG)

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

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