0%

Codeforces 786A - Berzerk

http://codeforces.com/problemset/problem/786/A

#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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
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

#Key 题意:一个有n个结点的环,第一个结点上是黑洞,其他为行星。现在有一个怪物可能在任意的行星上。甲乙两人可以顺时针驱赶怪物。甲每次可驱赶\(s1_1, s1_2,s1_3...\)个格子,共k1个可能。乙每次可驱赶\(s2_1, s2_2,s2_3...\)个格子,共k2种可能。谁把怪物刚好驱逐到黑洞里就赢。现在求:当怪物在第i个行星,甲(或乙)第一个驱赶时,甲(或乙)一定赢、一定输还是平局?

思路:DFS+动态规划。(这里只考虑甲开局的情况,乙一样求)从黑洞开始反向逆推,即分别倒退\(s1_1, s1_2,s1_3...\)个格子,只要从这些点驱逐,甲是必赢的;换句话说,乙如果把怪物驱逐到了这些点,乙就输了,所以当乙能驱赶的点都是必输点时,当前点也是乙的必输点。 那么对于当前点:若能够将怪物驱逐到对手的必输点,该点必赢,对当前点DFS;若能够将怪物驱逐到对手的必赢点,为当前点计数,当当前点到所有可驱逐的点都会导致失败时,当前点即为必输点。 使用sch数组保存点是否访问过,即DP的记忆化搜索的思想。

#Code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#include<cstdio>
#include<cstring>
#define N 7008
int n, k[2], s[2][N];
int win[2][N], sch[2][N], cnt[2][N];

void init(int who) {
memset(win[who], 0, sizeof(int)*n);
memset(sch[who], 0, sizeof(int)*n);
memset(cnt[who], 0, sizeof(int)*n);
}

void dp(int now, int who) {
sch[!who][now] = 1; // 能够允许dp递归下来的now都是已经确定输和赢的
/*
for (int i = 0;i != n;++i) {
printf("win{%d %d} sch{%d %d} cnt{%d %d}\n",
win[0][i], win[1][i],
sch[0][i], sch[1][i],
cnt[0][i], cnt[1][i]);
}
puts("-----");
*/
for (int i = 0;i != k[who];++i) {
int pre = (now - s[who][i] + n) % n; // who让怪物从pre跳到now
if (!pre) continue; // 不可能
if (sch[who][pre]) continue; // 记忆搜索
if (!win[!who][now]) { // 若从pre到now对面必输. (输的条件是sch[who][i]==1且win[who][i]==0)
win[who][pre] = 1; // 这一步一定赢
dp(pre, !who);
}
else if (win[!who][now]) { // 若从pre到now对面必赢
if (++cnt[who][pre] == k[who]) { // 当这一步怎么走都会导致对面赢
dp(pre, !who);
}
}
}
}

int main()
{
scanf("%d", &n);
for (int who = 0;who != 2;++who) {
scanf("%d", k + who);
for (int i = 0;i != k[who];++i)
scanf("%d", s[who] + i);
}
init(0), init(1);
dp(0, 0),dp(0, 1);
for (int who = 0;who != 2;++who) {
for (int i = 1;i != n;++i) {
if (win[who][i]) printf("Win");
else if (sch[who][i]) printf("Lose");
else printf("Loop");
printf("%c", " \n"[i == n - 1]);
}
}
return 0;
}