Codeforces786A Berzerk 简单博弈

A. Berzerk
time limit per test4 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard 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 k2 elements. One of them goes first and the player changes alternatively. In each player’s turn, he should choose an arbitrary number like x from 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

这道题是非常典型的博弈论,因为是第一次做,所以做的很吃力。
题意:有顺时针1-n个点,其中1号点为黑洞,怪兽可能在除了1号点之外的任何一个点。有两个同学相约玩博弈游戏,每个人都有一个移动集合:在自己的回合,可以把怪兽顺时针移动x个单位,其中x属于这个集合。如果把怪兽移动到1号黑洞就赢了(对方就输了)。
问2个人分别先手,怪兽初始位于2-n格子,每个人都采用最优策略,会赢还是会输还是平局。

首先看一下cf官方题解,我觉得讲得很好,关键就是必胜态和必败态的转换:For this purpose, first each state with monster on 1 is lost. Then if we consider a graph that its vertices are our 2n states, we will recursively determine the answer for each vertex. If a state has an edge to a lost state, it’s won, and if all its edges are to won states, it’s lost. The vertices that are neither lost or won at the end have answer “loop”.

首先要理解的是为什么(1,0) (1,1)(第一个1代表位置,第二个数代表哪个人先手)是必败态——因为假设有一个状态可以转移到这2个状态中的一个,也就说明这个状态是必胜态。
很关键的一点就是对 loop状态的理解。我们可以这么理解:一开始,所有的状态初始都是loop状态;然后,从(1,0) (0,1)开始搜索,进行必胜态 必败态的判定。但是,必定有一些状态无法判断是必胜态还是必败态,因为他们通向的状态也有不确定的(实际上,这些不确定的状态会形成一个环,这也是loop这个单词的由来)。

具体怎么做呢?官方题解说是倒着做记忆化搜索,我试着dfs写了一下发现不好写,然后用bfs写了。具体的,维护每个状态的出度(分别就是k1和k2)。队列初始化就是(1,0) (0,1)两个状态,然后开始拓展搜索。关键点是,只有可以确定是必胜态或者必败态的状态才可以入队——在bfs过程中,根据出队元素是必胜态或者必败态分类讨论。假如是必败态,那么很简单,把可以转换到这个状态的状态(好绕口233)设置成必败态入队;假如是必胜态,首先把可以转移到这个状态的状态的出度减一(代表一个状态的出路又少了一条;一旦一个状态没有出路了,也就是它不管怎么转换都是必胜态那么这个状态就是必败态了);假如出度为0 那么设置成为必败态入队。

具体代码有几个注意点,一个是初始化,还有就是注意减了之后小于0的状态。

我遇到的一个坑点在于,当一个待选状态已经确定了必胜态或者必败态的时候,就不用入队了,但是它的出度一定要减一(如果需要减的话)

代码是java写的,蓝桥杯就要到了赶紧练一下java

import java.io.*;
import java.util.*;
import java.math.*;

public class Main
{
    public static int n,k1,k2;
    public static final int maxn=7000+10;
    public static int[]a=new int[maxn];
    public static int[]b=new int[maxn];
    public static int[][]cnt=new int[maxn][2];//cnt means how many roads are left from this status
    public static int[][]ans=new int[maxn][2];//0 for loop,-1 for lose,1 for win
    public static Queue<Status> q=new LinkedList<Status>();
    public static void bfs()
    {
        while(!q.isEmpty())
        {
            Status top=q.poll();
            int pre,now=top.pos;
            int id=top.id;
            if(id==0)//the pre one is 1
            {
                for(int i=0;i<k2;i++)
                {
                    pre=now-b[i];
                    if(pre<=0)
                        pre+=n;
                    if(ans[now][id]==1)
                        cnt[pre][1]--;
                    if(ans[pre][1]!=0)
                        continue;
                    if(ans[now][id]==1)//this is a win status
                    {
                        if(cnt[pre][1]==0)
                        {
                            ans[pre][1]=-1;
                            q.offer(new Status(pre,1));
                        }
                    }
                    else
                    {
                        ans[pre][1]=1;
                        q.offer(new Status(pre,1));
                    }
                }
            }
            else//the pre one is 0
            {
                for(int i=0;i<k1;i++)
                {
                    pre=now-a[i];
                    if(pre<=0)
                        pre+=n;
                    if(ans[now][id]==1)
                        cnt[pre][0]--;
                    if(ans[pre][0]!=0)
                        continue;
                    if(ans[now][id]==1)
                    {
                        if(cnt[pre][0]==0)
                        {
                            ans[pre][0]=-1;
                            q.offer(new Status(pre,0));
                        }

                    }
                    else
                    {
                        ans[pre][0]=1;
                        q.offer(new Status(pre,0));
                    }
                }
            }
        }
    }
    public static void main(String[] args)
    {
        Scanner cin=new Scanner(System.in);
        n=cin.nextInt();
        k1=cin.nextInt();
        for(int i=0;i<k1;i++)
            a[i]=cin.nextInt();
        k2=cin.nextInt();
        for(int i=0;i<k2;i++)
            b[i]=cin.nextInt();
        for(int i=1;i<=n;i++)
        {
            ans[i][0]=0;
            ans[i][1]=0;
            cnt[i][0]=k1;
            cnt[i][1]=k2;
        }
        q.clear();
        q.offer(new Status(1,0));
        q.offer(new Status(1,1));
        ans[1][0]=ans[1][1]=-1;//lose
        bfs();
        for(int i=2;i<=n;i++)
        {
            if(ans[i][0]==-1)
                System.out.print("Lose ");
            else if(ans[i][0]==0)
                System.out.print("Loop ");
            else
                System.out.print("Win ");
        }
        System.out.println();
        for(int i=2;i<=n;i++)
        {
            if(ans[i][1]==-1)
                System.out.print("Lose ");
            else if(ans[i][1]==0)
                System.out.print("Loop ");
            else
                System.out.print("Win ");
        }
    }
}
class Status
{
    public int pos,id;//pos is the position,id is 0 or 1
    public Status(int x,int y)
    {
        pos=x;
        id=y;
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值