[刷题]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 ...
随机推荐
- Spring MVC 处理异常的3种方式
使用Spring MVC开发的博客网站时,遇到了如何处理业务层抛出的异常的问题,查阅到了spring官方博客-spring MVC中异常的处理,以下将会以登录模块为示例. 愚蠢的处理方式 处理异常遵循 ...
- 用js实现图片的无缝滚动效果
实现图片的无缝滚动就是要让你的图片集在一定时间里自动切换,那就需要js里的定时器来控制时间. js中关于定时器的方法有两种:setTimeout和setInterval.它们接收的参数是一样的,第一个 ...
- 无线接收信号强度(RSSI)那些事儿
本文由嵌入式企鹅圈原创团队成员黄鑫供稿. 本文所述的原理适用于所有无线传输技术,只是用蓝牙来举例.应该说,嵌入式企鹅圈更加偏重于嵌入式和物联网.安卓技术原理方面的知识分享和传播,其次才是实践,尽管很多 ...
- OC--Runtime知识点整理
1.Runtime简介 因为Objc是一门动态语言,所以它总是想办法把一些决定工作从编译连接推迟到运行时.也就是说只有编译器是不够的,还需要一个运行时系统 (runtime system) 来执行编译 ...
- Legendary Items-微软实习生笔试第一题
题目如下: 这道题难点不仅在于正确理解题意,判断递归条件,更在于用数学方法推出解决公式.因为N最大为1百万,而内存只有256MB, 所以暴力递归肯定会超时,超空间. 不过,我才疏学浅,又没有大量时间去 ...
- 性能调优案例分享:jvm crash的原因 1
性能调优案例分享:jvm crash的原因 poptest是国内唯一一家培养测试开发工程师的培训机构,以学员能胜任自动化测试,性能测试,测试工具开发等工作为目标.如果对课程感兴趣,请大家咨询qq: ...
- 采用Spring AOP+Log4j记录项目日志
转载请注明出处:http://www.cnblogs.com/Joanna-Yan/p/6567672.html 项目日志记录是项目开发.运营必不可少的内容,有了它可以对系统有整体的把控,出现任何问题 ...
- State模式学习笔记
选用了一个假设需要用户验证的例子进行State模式学习,这个例子并不恰当.无所谓了,只要能学习到其中的内容即可. 适用性: 1,一个对象的行为取决于他的状态,并且它必须在运行时刻依据状态改变他的行为. ...
- JavaWeb总结(三)—JSP
一.JSP简介 1.基本认识 (1)JSP页面是由HTML语句和嵌套在其中的Java代码组成的一个普通文本文件,JSP 页面的文件扩展名必须为.jsp. (2)在JSP页面中编写的Java代码需要嵌套 ...
- centos7.2部署最新ELK 5.3
## 安装elasticsearch服务> 安装jdk 1.8 ```rpm -ivh jdk-8u101-linux-x64.rpmjava -version``` > 配置rpm `` ...