title: CodeForces 786A Berzerk

data: 2018-3-3 10:29:40

tags:

  • 博弈论
  • bfs
  • 无限
  • with draw

    copyright: true

    categories:
  • 信息学竞赛
  • 题目

    description: 有一个环, 1的位置是黑洞, 有一个怪物在任意位置上, 两个人依次行动, 可以将怪物移动自己决策集合中的任意步, 谁先把它送进黑洞谁就赢, 请输出当怪物在任意位置和谁先手的各自的胜负情况, 有平局.

A. Berzerk

description

  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 #1

5

2 3 2

3 1 2 3

output #1

Lose Win Win Loop

Loop Win Win Win

input #2

8

4 6 2 3 4

2 3 6

output #2

Win Win Win Win Win Win Win

Lose Win Lose Lose Win Lose Lose

Way:

  这显然是一个无限博弈问题, 因为两人都选择最佳策略, 所以两个人有可能一直僵持下去谁也奈何不了谁, 这显然就是平局.

  显然可以用搜索搜出每个状态的所有后继状态, 根据必败态和必胜态的定义.

  • 终止状态是必败态.
  • 有一个后继状态是必败态的状态是必胜态.
  • 所以后继状态都是必胜态的状态是必败态.

  通过这样可以推出所有的必胜态和必败态.可是平局如何定义呢?后继状态有不确定状态的状态是不确定状态, 可以发现它构成了一个环.

Code

#include<algorithm>
#include<iostream>
#include<cstdlib>
#include<cstdio>
#include<queue>
#define N 7005
using namespace std; namespace Input{
inline int read(){
char ch=getchar();int s=0;
for(;!isdigit(ch);ch=getchar());
for(;isdigit(ch);s=s*10+ch-'0',ch=getchar());
return s;
}
inline void read(int &s){
char ch=getchar();
for(;!isdigit(ch);ch=getchar());
for(s=0;isdigit(ch);s=s*10+ch-'0',ch=getchar());
}
};using namespace Input;
struct Move{
int pos;
bool now;
Move(int _pos=0,bool _now=1){pos=_pos;now=_now;}
}; int mark[N][2],alt[N][2];
int vis[N][2],du[N][2];
int f[N][2],s[2],n; int mov(int pos,int step){
return (pos-step+n)%n;
}
void bfs(){
queue<Move>que;
f[0][1]=f[0][0]=2;
que.push((Move){0,0});
que.push((Move){0,1});
vis[0][1]=true;
vis[0][0]=true;
while(!que.empty()){
Move top=que.front();que.pop();
bool opt=top.now^1;
for(int i=0;i<s[opt];++i){
int to=mov(top.pos,alt[i][opt]);
if(f[top.pos][top.now]==1){
mark[to][opt]++;
if(!vis[to][opt]&&mark[to][opt]==s[opt]){
f[to][opt]=2;
vis[to][opt]=true;
que.push((Move){to,opt});
}
}
else if(!vis[to][opt]){
f[to][opt]=1;
vis[to][opt]=true;
que.push((Move){to,opt});
}
}
}
} int main(){
read(n);
read(s[0]);
for(int i=0;i<s[0];++i)read(alt[i][0]);
read(s[1]);
for(int i=0;i<s[1];++i)read(alt[i][1]);
bfs();int ans;
for(int i=1;i<n;++i){
ans=f[i][0];
printf(ans?ans==1?"Win":"Lose":"Loop");
putchar(' ');
}
putchar('\n');
for(int i=1;i<n;++i){
ans=f[i][1];
printf(ans?ans==1?"Win":"Lose":"Loop");
putchar(' ');
}
system("pause");
return 0;
}

cf786a的更多相关文章

  1. CF786A - Berzerk

    /* CF786A - Berzerk http://codeforces.com/contest/786/problem/A 博弈论 直接搜出NP状态图.记得要记忆化剪枝. * */ #includ ...

随机推荐

  1. bzoj 1135 [POI2009]Lyz 线段树+hall定理

    1135: [POI2009]Lyz Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 573  Solved: 280[Submit][Status][ ...

  2. lightoj 1245

    lightoj 1245 Harmonic Number (II) 题意:给定一个 n ,求 n/1 + n/2 + …… + n/n 的值(这里的 "/" 是计算机的整数除法,向 ...

  3. sgu 131 状压DP

    棋盘覆盖(二) 时间限制:1000 ms  |  内存限制:65535 KB     描述 The banquet hall of Computer Scientists' Palace has a ...

  4. 南阳ACM 题目8:一种排序 Java版

    一种排序 时间限制:3000 ms  |  内存限制:65535 KB 难度:3 描述 现在有很多长方形,每一个长方形都有一个编号,这个编号可以重复:还知道这个长方形的宽和长,编号.长.宽都是整数:现 ...

  5. 数据结构:Rope-区间翻转

    BZOJ1269 上一篇文章介绍了Rope的简单应用,这里多了一个操作,区间翻转 同时维护一正一反两个rope……反转即交换两个子串 下面给出代码: #include<cstdio> #i ...

  6. [洛谷P2365] 任务安排

    洛谷题目链接:任务安排 题目描述 N个任务排成一个序列在一台机器上等待完成(顺序不得改变),这N个任务被分成若干批,每批包含相邻的若干任务.从时刻0开始,这些任务被分批加工,第i个任务单独完成所需的时 ...

  7. [Luogu 2221] HAOI2012 高速公路

    [Luogu 2221] HAOI2012 高速公路 比较容易看出的线段树题目. 由于等概率,期望便转化为 子集元素和/子集个数. 每一段l..r中,子集元素和为: \(\sum w_{i}(i-l+ ...

  8. PHP扩展--Oracle客户端(oci8)安装

    下载Oracle客户端 官方下载地址: Linux X86-64 同意协议,下载以下文件: oracle-instantclient11.2-basic-11.2.0.4.0-1.x86_64.rpm ...

  9. Spring理论基础-面向切面编程

    AOP是Aspect-Oriented Programming的缩写,中文翻译是面向切面编程.作为Spring的特征之一,是要好好学习的. 首先面向切面编程这个名称很容易让人想起面向对象编程(OOP) ...

  10. 边缘检测:Canny算子,Sobel算子,Laplace算子

    1.canny算子 Canny边缘检测算子是John F.Canny于 1986 年开发出来的一个多级边缘检测算法.更为重要的是 Canny 创立了边缘检测计算理论(Computational the ...