[刷题]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
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个结点的环,第一个结点上是黑洞,其他为行星。现在有一个怪物可能在任意的行星上。甲乙两人可以顺时针驱赶怪物。甲每次可驱赶s11,s12,s13...个格子,共k1个可能。乙每次可驱赶s21,s22,s23...个格子,共k2种可能。谁把怪物刚好驱逐到黑洞里就赢。现在求:当怪物在第i个行星,甲(或乙)第一个驱赶时,甲(或乙)一定赢、一定输还是平局?
思路:DFS+动态规划。(这里只考虑甲开局的情况,乙一样求)从黑洞开始反向逆推,即分别倒退s11,s12,s13...个格子,只要从这些点驱逐,甲是必赢的;换句话说,乙如果把怪物驱逐到了这些点,乙就输了,所以当乙能驱赶的点都是必输点时,当前点也是乙的必输点。
那么对于当前点:若能够将怪物驱逐到对手的必输点,该点必赢,对当前点DFS;若能够将怪物驱逐到对手的必赢点,为当前点计数,当当前点到所有可驱逐的点都会导致失败时,当前点即为必输点。
使用sch数组保存点是否访问过,即DP的记忆化搜索的思想。
Code
#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;
}
[刷题]Codeforces 786A - Berzerk的更多相关文章
- [刷题]Codeforces 794C - Naming Company
http://codeforces.com/contest/794/problem/C Description Oleg the client and Igor the analyst are goo ...
- [刷题codeforces]650A.637A
650A Watchmen 637A Voting for Photos 点击查看原题 650A又是一个排序去重的问题,一定要注意数据范围用long long ,而且在写计算组合函数的时候注意也要用l ...
- [刷题codeforces]651B/651A
651B Beautiful Paintings 651A Joysticks 点击可查看原题 651B是一个排序题,只不过多了一步去重然后记录个数.每次筛一层,直到全为0.从这个题里学到一个正确姿势 ...
- [刷题]Codeforces 746G - New Roads
Description There are n cities in Berland, each of them has a unique id - an integer from 1 to n, th ...
- CF刷题-Codeforces Round #481-G. Petya's Exams
题目链接:https://codeforces.com/contest/978/problem/G 题目大意:n天m门考试,每门考试给定三个条件,分别为:1.可以开始复习的日期.2.考试日期.3.必须 ...
- CF刷题-Codeforces Round #481-F. Mentors
题目链接:https://codeforces.com/contest/978/problem/F 题目大意: n个程序员,k对仇家,每个程序员有一个能力值,当甲程序员的能力值绝对大于乙程序员的能力值 ...
- CF刷题-Codeforces Round #481-D. Almost Arithmetic Progression
题目链接:https://codeforces.com/contest/978/problem/D 题解: 题目的大意就是:这组序列能否组成等差数列?一旦构成等差数列,等差数列的公差必定确定,而且,对 ...
- Codeforces 786A Berzerk(博弈论)
[题目链接] http://codeforces.com/problemset/problem/786/A [题目大意] 有两个人,每个人有一个数集,里面有一些数,现在有一个环,有个棋子放在1, 有个 ...
- [刷题]Codeforces 785D - Anton and School - 2
Description As you probably know, Anton goes to school. One of the school subjects that Anton studie ...
随机推荐
- Jmeter新建用例图示
添加线程组 添加HTTP请求 编辑HTTP请求 添加HTTP信息头 编辑HTTP信息头 添加断言 添加查看结果树 添加聚合报告 添加响应时间 添加TPS 批量运行命令: ...
- IOS简单画板实现
先上效果图 设计要求 1.画笔能设置大小.颜色 2.有清屏.撤销.橡皮擦.导入照片功能 3.能将绘好的画面保存到相册 实现思路 1.画笔的实现,我们可以通过监听用户的 平移手势 中创建 UIBezie ...
- cent os 直接访问谷歌的脚本实现
https://github.com/DingGuodong/GoogleHostsFileForLinux/blob/master/replaceLocalHostsFileAgainstGfw.s ...
- Xshell连接本地 Virtualbo Ubuntu
1.打开Virtualbox软件,启动ubuntu虚拟机. Ctrl + Alt + T 打开终端输入一下命令: sudo apt-get update 然后安装ssh 输入:sudo apt-get ...
- 【转】AS3操作XML,增加、删除、修改
var i:Number=0;//用于下面循环 var webcontent:String="Sontin's Blog <b>Welcome to 终吾一生</b> ...
- SVN将一台服务器上的代码迁移到另一台服务器上
由于我们出差,需要把svn服务器上的代码同步到我电脑上,自己各种百度折腾了快一天才弄好,下面来分享下我的具体思路和操作步骤.有2种方式:第一种方式:直接将本地自己dowm下来的代码导入到本地svn服务 ...
- macos系统下共语言gopath变量的设置
一.问题 在macos下安装golang开发环境,想更改gopath路径,通过export GOPATH=/Volume/E/go 在vscode中通过go env命令查看GOPATH还是原始默认的, ...
- iOS标注和适配
很多项目一开始没有注意美术素材的规范,这在后期会引起混乱.假如有机会做一个新项目(旧项目会有自己的历史问题,一下子很难改过来),建议设计师和程序员一起坐下来.共同设立一套规范,之后共同遵守. 下面说说 ...
- opencv构建高斯卷积核
关于高斯核函数可以参见阮一峰老师的日志:高斯模糊的算法 如何使用高斯核进行高斯模糊可以参见我的另一篇日志:opencv构建自定义卷积 Mat Gaussian_kernal(int kernel_si ...
- 静态链表实现(A-B)+(B-A)【代码】
-----------------------------------------------第一次发代码,写在前面------------------------------------------ ...