[刷题]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 ...
随机推荐
- 初学canvas,遇到width和height显示问题和用excanvas.js兼容IE问题
/*-----------------------ITEYE 祈祷幸福博客原创,转载请注明.-------------------*/ 第一次认真写技术博客文~~~若有不严谨的地方,望指正. 今天是第 ...
- python try/except/finally
稍微总结一下,否则总是忘. [python] view plaincopyprint? x = 'abc' def fetcher(obj, index): return obj[index] fet ...
- js面向对象-原型链
var Person = function (name) { this.name = name; } Person.prototype.say = function () { console.log( ...
- HANA CDS与ABAP CDS
如果你在网络或者SCN上面搜索CDS,即SAP的Core Data Services,你会很容易地找到类似“Core Data Services(CDS)是一个在SAP HANA中用于定义和消费富语义 ...
- [SinGuLaRiTy] 最短路计算代码库
[SinGuLaRiTy-1002] Copyright (c) SinGuLaRiTy 2017. All Rights Reserved. Dijkstra: 题目描述 有向图的单源点最短路问题( ...
- Spring框架下的单元测试
一.使用spring中对Junit框架的整合功能 除了junit4和spring的jar包,还需要spring-test.jar.引入如下依赖: <dependency> <grou ...
- es6编写reactjs事件处理函数绑定this三种方式
第一种:官方推荐的: class LoginControl extends React.Component { constructor(props) { super(props); this.hand ...
- 老李分享:JDK,JRE,JVM区别与联系
poptest是国内唯一一家培养测试开发工程师的培训机构,以学员能胜任自动化测试,性能测试,测试工具开发等工作为目标.如果对课程感兴趣,请大家咨询qq:908821478,咨询电话010-845052 ...
- 拍照、本地图片工具类(兼容至Android7.0)
拍照.本地图片工具类:解决了4.4以上剪裁会提示"找不到文件"和6.0动态授予权限,及7.0报FileUriExposedException异常问题. package com.hb ...
- 第三章 PL/SQL编程
3.1 PL/SQL基础知识 3.1.1 什么是PL/SQL? PL/SQL是结合Oracle过程语言和结构化查询语言的一种扩展语言 3.1.1.1 PL/SQL体系 ...