hdu 1524 A Chess Game 博弈论

来源:互联网 发布:听觉音乐 淘宝 编辑:程序博客网 时间:2024/05/17 03:34

题意:

两个人在一个有向五环图上面走棋子,每次只能走一步,最后谁

* 没有棋子可走就败,然后棋子可以重叠,并且有n个棋子。要求判断

* 先手的胜负。

纠结了好长时间一直在想为什么sg函数要呢么定义然后看了各种博客但是只是讲了,定义的内容却很少有讲为什么的。。。。

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

42 1 201 301 02 0 2041 11 2002 0 12 1 13 0 1 30
 

Sample Output

WINWINWINLOSEWIN
 

#include<cstdio>#include<iostream>#include<algorithm>#include<cstring>using namespace std;int Map[1005][1005];int sg[1005],n;int dfs(int a){    if(sg[a]!=-1)        return sg[a];    bool vis[2000]={0};    for(int i=0;i<n;i++)    {        if(Map[a][i]==1)        {            vis[dfs(i)]=1;        }    }    for(int i=0;i<n;i++)        if(vis[i]==0)        return sg[a]=i;}int main(){    while(~scanf("%d",&n))    {        memset(sg,-1,sizeof(sg));        memset(Map,0,sizeof(Map));        for(int i=0; i<n; i++)        {            int a;            scanf("%d",&a);            if(a==0) //注意这个地方,这个地方是当没有后继的时候他的sg函数为0            {                sg[i]=0;                continue;            }            while(a--)            {                int b;                scanf("%d",&b);                Map[i][b]=1;            }        }        int m;        while(~scanf("%d",&m)&&m)        {            int ans=0;            for(int i=0;i<m;i++)            {                int a;                scanf("%d",&a);                if(sg[a]!=-1)                    ans^=sg[a];                else                    ans^=dfs(a);            }            if(ans==0)                printf("LOSE\n");            else                printf("WIN\n");        }    }    return 0;}


0 0
原创粉丝点击