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. Linux下线程池的理解与简单实现

    首先,线程池是什么?顾名思义,就是把一堆开辟好的线程放在一个池子里统一管理,就是一个线程池. 其次,为什么要用线程池,难道来一个请求给它申请一个线程,请求处理完了释放线程不行么?也行,但是如果创建线程 ...

  2. 好玩的Prim算法

    这段时间学算法,用JS实现了一个Prim,用于在连通图中找出最小树,具体内容.代码解析周末会不上,现在先把源码献上: <!DOCTYPE html> <html charset='G ...

  3. 大数据:从开源告诉你身边的IT故事

    最近我们Team利用Dream分布式计算平台,做了这样一件事情,将Github的大量数据通过爬虫抓取下来,通过分析后,我们抽取最近一年中部分的开发者和项目信息,得到了如下有趣的信息,故分享之,数据原汁 ...

  4. AngularJS开发指南15:AngularJS的创建服务,将服务注入到控制器,管理服务依赖详解

    创建服务 虽然AngularJS提供了很多有用的服务,但是如果你要创建一个很棒的应用,你可能还是要写自己的服务.你可以通过在模块中注册一个服务工厂函数,或者通过Module#factory api或者 ...

  5. 每天一个linux命令(54):sftp命令

    sftp 是一个交互式文件传输程式.它类似于 ftp, 但它进行加密传输,比FTP有更高的安全性.下边就简单介绍一下如何远程连接主机,进行文件的上传和下载,以及一些相关操作. 举例,如远程主机的 IP ...

  6. Qt学习思考

    对各个部件基本了解,初步理解GUI应用程序的创建 2D图形文字绘制,3D图形(openGL)等 模型/视图框架编程,处理复杂的数据 多媒体框架 数据库,xml,文件读写等 网络编程 做出比较美观的界面 ...

  7. iOS边练边学--iOS中的json数据解析

    JSON数据(NSData) -> OC对照表 {} -> NSDictionary @{} [] -> NSArray @[] "jack" -> NSS ...

  8. Xcode7企业版打包

    今天才发现Xcode7企业账号打包竟然和以前稍微不一样了,一时手残,先把公司服务器以前的ipa包删了,吓得我的小心脏呢 首先选中然后选archive然后点export然后然后选中include man ...

  9. linux更改文件所有者命令chown命令的使用困惑

    [berry@berry:practice] ls -lrt total -rwxrwxrwx berry berry Dec : f1.txt -rwxrwxrwx berry berry Dec ...

  10. 常用JQuery插件

    虽然自己也写过插件,但JQuery插件种类的繁多,大多时候,我还是使用别人写好的插件,这些都是我用了同类插件里较为不错的一些,今天就整理一下公开放出来. UI: jquery.HooRay(哈哈,自己 ...