A Chess Game

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 1123    Accepted Submission(s): 519

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
 

Recommend
LL
 

#include <iostream>
#include <cstdio>
#include <cstring>

using namespace std;

struct Edge
{
    int to,next;
}E[100000];

int Size,Adj[1100];

void Init()
{
    Size=0;
    memset(Adj,-1,sizeof(Adj));
}

void Add_Edge(int u,int v)
{
    E[Size].to=v;
    E[Size].next=Adj;
    Adj=Size++;
}

int SG[1100],N,n,m;

int SG_dfs(int x)
{
    if(SG[x]!=-1) return SG[x];
    bool flag[1100];int i;
    memset(flag,false,sizeof(flag));
    for(i=Adj[x];~i;i=E.next)
    {
        SG_dfs(E.to);
        flag[SG[E.to]]=true;
    }
    for(i=0;i<N;i++)
    {
        if(!flag) break;
    }
    return SG[x]=i;
}

int main()
{
    while(scanf("%d",&N)!=EOF)
    {
        Init();
        for(int i=0;i<N;i++)
        {
            scanf("%d",&n);
            if(n==0) continue;
            while(n--)
            {
                int v;
                scanf("%d",&v);
                Add_Edge(i,v);
            }
        }
        memset(SG,-1,sizeof(SG));
        while(scanf("%d",&m)&&m)
        {
            int XOR=0;
            while(m--)
            {
                int a;
                scanf("%d",&a);
                XOR^=SG_dfs(a);
            }
            if(XOR)
                puts("WIN");
            else
                puts("LOSE");
        }
    }
    return 0;
}

* This source code was highlighted by YcdoiT. ( style: Codeblocks )

HDOJ 1524 A Chess Game的更多相关文章

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

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1524 #include<stdio.h> #include<cstring> ...

  2. HDU 1524 A Chess Game【SG函数】

    题意:一个N个点的拓扑图,有M个棋子,两个人轮流操作,每次操作可以把一个点的棋子移动到它的一个后继点上(每个点可以放多个棋子),直到不能操作,问先手是否赢. 思路:DFS求每个点的SG值,没有后继的点 ...

  3. hdu 1524 A Chess Game 博弈论

    SG函数!! 代码如下: #include<stdio.h> #include<cstring> #define I(x) scanf("%d",& ...

  4. hdu 1524 A Chess Game 博弈

    题目链接 给出一个有向无环图, 上面放有一些棋子, 两个人轮流移动棋子, 当一个人无法移动时, 算输. 求出每一个点的sg值, 异或就可以.出度为0的点sg值为0. #include<bits/ ...

  5. (step 8.2.13)hdu 1524(A Chess Game)

    题目大意 : 在一个 有向无环图顶点上面有几个棋子, 2个人轮流操作, 每次操作就是找一个棋子往它能够移 动的地方移动一格, 不能操作的人输. 输入第一行 为一个 N , 表示有 N 个顶点 0 -& ...

  6. 【HDOJ】【4405】Aeroplane chess飞行棋

    概率DP/数学期望 kuangbin总结中的第4题 啊还是求期望嘛……(话说Aeroplane chess这个翻译怎么有种chinglish的赶脚……) 好像有点感觉了…… 首先不考虑直飞的情况: f ...

  7. 【HDOJ】3345 War Chess

    简单BFS.注意最后一组数据,每个初始点不考虑周围是否有敌人. /* 3345 */ #include <iostream> #include <cstdio> #includ ...

  8. A Chess Game HDU - 1524 (有向图博弈)

    题意:在一个有向无环图上有n个顶点,每一个顶点都只有一个棋子,有两个人,每次根据这个图只能将任意一颗棋子移动一步 ,如果到某一步玩家不能移动时,那么这个人就输. 分析:本题是最典型的有向无环图的博弈, ...

  9. HDOJ 1009. Fat Mouse' Trade 贪心 结构体排序

    FatMouse' Trade Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) ...

随机推荐

  1. windows网络编程的一些理论

    参考自<VC++深入详解> 这是我在看书时记录下来的东西. 注:下面的Socket其实都应该是socket 第14章网络编程 Socket是连接应用程序与网络驱动程序的桥梁,Socket在 ...

  2. WPF开发时光之痕日记本

       很久没有写东西了,新的一年新的开始吧. 很早就想自己开发一款日记本软件不仅自己使用,也可以让大家免费使用,最主要的是对自己有一个认可,自学WPF以来,感觉不很顺利,WPF的资料相对来说有点少,主 ...

  3. Scala 中的函数式编程基础(一)

    主要来自 Scala 语言发明人 Martin Odersky 教授的 Coursera 课程 <Functional Programming Principles in Scala>. ...

  4. Office2010 pro附+激活工具

    office2003经典的办公,office2010,很不错的办公工具配合Win7,就绝配! office2010,我也近几天接手她,慢慢熟悉... 00安装: 01激活: 内含office2010安 ...

  5. rational rose USE CASE

  6. sublime text 删除安装的插件

    ctrl+shift+p输入remove package选择要删掉的插件即可

  7. hdu1520 树形dp

    树形dp入门,在树上进行dp. 状态转移方程: dp[i][0] = max(dp[j][0], dp[j][1]);//i为j的上司 并且i不来 dp[i][1] = dp[j][0];//i来了 ...

  8. Java-set

    set public interface Set<E> extends Collection<E> 使用集合汇总 package 集合类.Set类; /** * Set不允许重 ...

  9. Session的异常

    既然这一天就这么废了,那就多说一些吧!其实session也是有潜在的问题的.Session销毁的三种情况: (1)超时:超过30分钟 (2)服务器非正常关闭,如果自己手动stop service而不是 ...

  10. BZOJ-3225 立方体覆盖 线段树+扫描线+乱搞

    看数据范围像是个暴力,而且理论复杂度似乎可行,然后被卡了两个点...然后来了个乱搞的线段树+扫描线.. 3225: [Sdoi2008]立方体覆盖 Time Limit: 2 Sec Memory L ...