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): 2554    Accepted Submission(s): 1153


 

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

题意:在一个有向无环图上右n枚棋子。两个人轮流移动一枚棋子,直到一方不能在移动任何棋子,判定为输。假设双方都能够采取最优的策略,如果第一个移动棋子的人获胜输出“WIN”,否则输出“LOST”。

思路:sg函数的使用。

必胜点N总有一种方式能够通往必败点P;

必败点P只能通往必胜点N;

必败点P的sg函数的值为0;

首先了解到一个集合mex{},它的值表示不属于该集合的最小整数。例如mex{1}=0;mex{0,1}=2;mex{0,1,2}=3;mex{0,1,3}=2;

sg(now)表示当前状态now,他可以通过不同的方式移动当前的棋子,来达到不同的状态next(是一个集合)。因此sg(now)的值等于mex{next{}}。如果它的值不等于0,表示它能够使得当前的状态now,变成下一个状态next(i),使得sg(next(i))=0。也就是让自身处于必胜点N,让对手处于必败点P;如果它的值为0,说明当前点为必败点,无论怎么移动只会通往必胜点,因为双方都是采取的最优策略。

将每个棋子的sg值做异或(这是证明必胜点和必败点说法的正确性,百度百科上有证明过程:https://baike.baidu.com/item/Nim%E6%B8%B8%E6%88%8F/6737105?fr=aladdin),如果sg的值为0则败北,sg的值不为0则胜利。

AC代码:

#include <cstdio>
#include <cstring>
using namespace std;
const int MAXN = 1e3+5;
int head[MAXN],sg[MAXN];

int cnt;
struct Edge{
    int next,to;
}edge[MAXN*MAXN];

void add(int u,int v){
    edge[cnt]=(Edge){head[u],v};
    head[u]=cnt++;
}

int getSG(int x){
    if(sg[x]!=-1) return sg[x];
    bool vis[MAXN];
    memset(vis,0,sizeof(vis));
    //为求的sg[x],得先求的x的后继节点的sg值
    //利用递归的回溯(向上更新)。
    for(int i=head[x];~i;i=edge[i].next){
        int v=edge[i].to;
        sg[v]=getSG(v);
        vis[sg[v]]=1;
    }
    for(int i=0;;i++)
        if(!vis[i]) return i;
}

int main(){
    int n,m;
    while(~scanf("%d",&n)){
        cnt=0;
        memset(head,-1,sizeof(head));
        memset(edge,0,sizeof(edge));
        memset(sg,-1,sizeof(sg));
        for(int i=0;i<n;i++){
            scanf("%d",&m);
            while(m--){
                int v;
                scanf("%d",&v);
                add(i,v);
            }
        }
        int q;
        while(scanf("%d",&q),q){
            int ans=0;
            while(q--){
                int u;
                scanf("%d",&u);
                ans^=getSG(u);
            }
            printf("%s\n",ans==0?"LOSE":"WIN");
        }
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/Rainbow_storm/article/details/82733493