zoukankan      html  css  js  c++  java
  • (难)Codeforces Round #406 (Div. 2) C题Berzerk(有向图博弈)解题报告

    C. Berzerk
    time limit per test
    4 seconds
    memory limit per test
    256 megabytes
    input
    standard input
    output
    standard output

    Rick and Morty are playing their own version of Berzerk (which has nothing in common with the famous Berzerk game). This game needs a huge space, so they play it with a computer.

    In this game there are n objects numbered from 1 to n arranged in a circle (in clockwise order). Object number 1 is a black hole and the others are planets. There's a monster in one of the planet. Rick and Morty don't know on which one yet, only that he's not initially in the black hole, but Unity will inform them before the game starts. But for now, they want to be prepared for every possible scenario.

    Each one of them has a set of numbers between 1 and n - 1 (inclusive). Rick's set is s1 with k1 elements and Morty's is s2 with k2elements. One of them goes first and the player changes alternatively. In each player's turn, he should choose an arbitrary number like xfrom his set and the monster will move to his x-th next object from its current position (clockwise). If after his move the monster gets to the black hole he wins.

    Your task is that for each of monster's initial positions and who plays first determine if the starter wins, loses, or the game will stuck in an infinite loop. In case when player can lose or make game infinity, it more profitable to choose infinity game.

    Input

    The first line of input contains a single integer n (2 ≤ n ≤ 7000) — number of objects in game.

    The second line contains integer k1 followed by k1 distinct integers s1, 1, s1, 2, ..., s1, k1 — Rick's set.

    The third line contains integer k2 followed by k2 distinct integers s2, 1, s2, 2, ..., s2, k2 — Morty's set

    1 ≤ ki ≤ n - 1 and 1 ≤ si, 1, si, 2, ..., si, ki ≤ n - 1 for 1 ≤ i ≤ 2.

    Output

    In the first line print n - 1 words separated by spaces where i-th word is "Win" (without quotations) if in the scenario that Rick plays first and monster is initially in object number i + 1 he wins, "Lose" if he loses and "Loop" if the game will never end.

    Similarly, in the second line print n - 1 words separated by spaces where i-th word is "Win" (without quotations) if in the scenario that Morty plays first and monster is initially in object number i + 1 he wins, "Lose" if he loses and "Loop" if the game will never end.

    Examples
    input
    5
    2 3 2
    3 1 2 3
    output
    Lose Win Win Loop
    Loop Win Win Win
    input
    8
    4 6 2 3 4
    2 3 6
    output
    Win Win Win Win Win Win Win
    Lose Win Lose Lose Win Lose Lose

    依旧是一道让人深受启发的题目。

    怪兽有n个起始位置(包括起始就在黑洞中),每个位置2个人都有可能作为先手,所以总共有2n种情况,用2n个点代表它们。显然它们之中存在一些“到达”关系,作为有向图连线。

    对于每一点总共3种可能。先手必胜,先手必负,绝不出胜负。

    假设某些结点的“可能状态”已经被确定,那么对于任意其他结点,如果从它连出的线中存在能到达某个先手必负的结点的,那么显然这个点是先手必胜的。(只要进行这条线所对应的操作即可)。

    如果从它连出的线全是先手必胜,那么这个点是先手必输的。不是这两种情况的点就是绝不出胜负的点。

    而我们起始时已知先手怪兽已经在黑洞的是必败状态,从这个起始状态推就能将所有点的结果推出。

     1 #include<cstdio>
     2 #include<bits/stdc++.h>
     3 #include <iostream>
     4 using namespace std;
     5 typedef long long ll;
     6 typedef unsigned long long ull;
     7 typedef pair<int,int> pii;
     8 queue<pii>que;
     9 const int MAX=7e3+5;
    10 int n;
    11 int len[2];
    12 int st[2][MAX];
    13 int a[2][MAX];
    14 int nxtwho,nxtpos,who,pos;
    15 int main()
    16 {
    17     scanf("%d",&n);
    18     scanf("%d",&len[0]);
    19     for(int i=1;i<=len[0];i++)
    20         scanf("%d",&a[0][i]);
    21     scanf("%d",&len[1]);
    22     for(int i=1;i<=len[1];i++)
    23         scanf("%d",&a[1][i]);
    24     st[1][1]=st[0][1]=-2;
    25     que.push(make_pair(0,1));que.push(make_pair(1,1));
    26     while(!que.empty())
    27     {
    28         pii tem=que.front();que.pop();
    29         nxtwho=tem.first;nxtpos=tem.second;
    30         who=nxtwho^1;
    31         for(int i=1;i<=len[who];i++)
    32         {
    33             pos=nxtpos-a[who][i];
    34             if(pos<=0)
    35                 pos+=n;
    36             if(st[nxtwho][nxtpos]==-2)
    37             {
    38                 if(st[who][pos]!=-1)
    39                 st[who][pos]=-1,que.push(make_pair(who,pos));
    40             }
    41             else
    42             {
    43                 if(st[who][pos]<0)
    44                     continue;
    45                 st[who][pos]++;
    46                 if(st[who][pos]==len[who])
    47                     st[who][pos]=-2,que.push(make_pair(who,pos));
    48             }
    49         }
    50     }
    51     for(int i=2;i<=n;i++)
    52     {
    53         if(st[0][i]==-2)
    54             printf("Lose ");
    55         else if(st[0][i]==-1)
    56             printf("Win ");
    57         else
    58             printf("Loop ");
    59     }
    60     printf("
    ");
    61     for(int i=2;i<=n;i++)
    62     {
    63         if(st[1][i]==-2)
    64             printf("Lose ");
    65         else if(st[1][i]==-1)
    66             printf("Win ");
    67         else
    68             printf("Loop ");
    69     }
    70     printf("
    ");
    71 
    72 }
  • 相关阅读:
    性能分析
    thymeleaf和spring的整合
    Java中二叉树的建立
    面试题
    (转)structs2的相关配置问题
    ==与equal()的区别
    java的基本类型和其包装类
    Ajax调用返回json数组,对象 (JSONArray.fromObject)
    (转)在JSP中调用JAVA类和使用JavaBean有什么区别?
    用log4j查看详细错误信息
  • 原文地址:https://www.cnblogs.com/quintessence/p/6618640.html
Copyright © 2011-2022 走看看