hdu 1524 A Chess Game (SG)

题意:在一个有向无环图上有n个顶点,每一个顶点都只有一个棋子,有两个人,每次根据这个图只能将任意一颗棋子移动一步

,如果到某一步玩家不能移动时,那么这个人就输.

分析:本题是最典型的有向无环图的博弈,利用dfs把所有顶点的SG值都计算出来,然后对每个棋子的SG值进行异或运算,如果

为0就是先手必败,否则就是先手必胜.

如果某个人移动到出度为0的顶点,那么他必败,在这里首先介绍一下什么是SG函数.

对于给定的有向无环图,定义图中每个顶点的Sprague-Grundy函数g如下:g(x) = mex{ g(y) | y是x的后继 }。

mex(x)表示最小的不属于这个集合的非负整数。例如:mex{0,1,2,4} = 3、mex{2,3,5} = 0、mex{ } = 0。

SG函数的性质:首先,所有终结点所对应的顶点,也就是出度为0的顶点,其SG值为0,因为它的后继集合是空集。然后对于一

个g(x) = 0的顶点x,它的所有后继y都满足g(y)!=0。对于一个g(x)!=0的顶点,必定存在一个后继y满足g(y)=0.

而求整个SG函数值的过程就是一个对有向无环图进行深搜过程.

# include <stdio.h>
# include <algorithm>
# include <iostream>
# include <string.h>
#include <vector>
using namespace std;
vector<int>g[1010];
int sg[1010];

int GetSG(int x)
{
    int i;
    if(sg[x]!=-1)
        return sg[x];
    if(g[x].size()==0)
        return sg[x]=0;
    int vis[1010];//
    memset(vis,0,sizeof(vis));
    for(i=0; i<g[x].size(); i++)
        vis[GetSG(g[x][i])]=1;
    for(i=0;; i++)
        if(vis[i]==0)
            return sg[x]=i;
}
int main()
{
    int n,q,i,x,a;
    while(~scanf("%d",&n))
    {
        memset(sg,-1,sizeof(sg));
        for(i=0; i<n; i++)
        {
            g[i].clear();
            scanf("%d",&x);
            while(x--)
            {
                scanf("%d",&a);
                g[i].push_back(a);
            }
        }
        while(~scanf("%d",&q))
        {
            if(q==0)
                break;
            int ret=0;
            while(q--)
            {
                scanf("%d",&a);
                ret^=GetSG(a);
            }
            if(ret==0)
                printf("LOSE\n");
            else
                printf("WIN\n");
        }
    }
    return 0;
}

时间: 2024-08-10 19:06:41

hdu 1524 A Chess Game (SG)的相关文章

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函数 + 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 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 4405 Aeroplane chess (概率DP求期望)

题意:有一个n个点的飞行棋,问从0点掷骰子(1~6)走到n点需要步数的期望 其中有m个跳跃a,b表示走到a点可以直接跳到b点. dp[ i ]表示从i点走到n点的期望,在正常情况下i点可以到走到i+1,i+2,i+3,i+4,i+5,i+6 点且每个点的概率都为1/6 所以dp[i]=(dp[i+1]+dp[i+2]+dp[i+3]+dp[i+4]+dp[i+5]+dp[i+6])/6  + 1(步数加一). 而对于有跳跃的点直接为dp[a]=dp[b]; #include<stdio.h>

hdu 3345 War Chess (bfs+优先队列)

War Chess Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 1732    Accepted Submission(s): 416 Problem Description War chess is hh's favorite game: In this game, there is an N * M battle map, an

hdu 4405 Aeroplane chess

题意: hzz一开始在0位置,然后hzz掷骰子,骰子为i,就往前走i步,当hzz位置大于等于n的时候结束,求掷骰子次数的期望 有m个直达点 (x,y),走到x时可以直接到y 求期望一般从后往前推 当 i不等于任何一个x时 dp[i]=seg(1/6*dp[i+k])+1 否则 dp[i]=dp[y] 1 #include<iostream> 2 #include<string> 3 #include<cstdio> 4 #include<vector> 5

[ACM] hdu 4405 Aeroplane chess (概率DP)

Aeroplane chess Problem Description Hzz loves aeroplane chess very much. The chess map contains N+1 grids labeled from 0 to N. Hzz starts at grid 0. For each step he throws a dice(a dice have six faces with equal probability to face up and the number

HDU ACM 1524 A Chess Game-&gt;博弈(SG函数)

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

【百题留念】hdoj 1524 A Chess Game(dfs + SG函数应用)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1524 1 #include<stdio.h> 2 #include<cstring> 3 using namespace std; 4 const int M = 10000000;// 5 const int N = 1005; 6 int sg[N], head[N]; 7 int cnt; 8 9 struct node{ 10 int from; 11 int to; 12 i