Codeforces Round #406 (Div. 1) A. Berzerk 记忆化搜索
A. Berzerk
题目连接:
http://codeforces.com/contest/786/problem/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.
Sample Input
5
2 3 2
3 1 2 3
Sample Output
Lose Win Win Loop
Loop Win Win Win
Hint
题意
有两个人在一个环上玩游戏,由n个格子组成的环,其中0环是洞。
现在A,B两个人各自拥有K[i]个选项,第i个选项是让怪兽顺时针走s[i]步。
两个人轮流让怪兽走,谁让怪兽走进洞里面谁就胜利。
现在问你考虑所有情况,胜利的结果是什么。
题解:
记忆化搜索。
倒着来。dp[i][j]表示现在i先手位置在j的胜负情况。
显然如果转移到dp[i][j]的状态全是对手胜利的话,那么dp[i][j]就是失败。
如果转移到dp[i][j]的状态存在对手失败,那么dp[i][j]就是胜利。
其他都是无限循环。
代码
#include<bits/stdc++.h>
using namespace std;
const int maxn = 1e5+7;
string Ans[3]={"Lose","Loop","Win"};
int n,Cnt[2],Flag[2][maxn],Times[2][maxn];
vector<int>Move[2];
void dfs(int x,int y,int val){
Flag[x][y]=val;
if(val==-1){
for(int i=0;i<Move[!x].size();i++){
if(!Flag[!x][(y+n-Move[!x][i])%n])
dfs(!x,(y+n-Move[!x][i])%n,1);
}
}else{
for(int i=0;i<Move[!x].size();i++){
if(Flag[!x][(y+n-Move[!x][i])%n]){
continue;
}
if(Times[!x][(y+n-Move[!x][i])%n]<Move[!x].size()){
Times[!x][(y+n-Move[!x][i])%n]++;
}
if(Times[!x][(y+n-Move[!x][i])%n]==Move[!x].size()){
dfs(!x,(y+n-Move[!x][i])%n,-1);
}
}
}
}
int main(){
scanf("%d",&n);
for(int type=0;type<2;type++){
scanf("%d",&Cnt[type]);
for(int i=0;i<Cnt[type];i++){
int tmp;
scanf("%d",&tmp);
Move[type].push_back(tmp);
}
}
Flag[1][0]=-1;
dfs(0,0,-1);
dfs(1,0,-1);
for(int type=0;type<2;type++){
for(int i=1;i<n;i++){
cout<<Ans[Flag[type][i]+1]<<" ";
}
cout<<endl;
}
}
Codeforces Round #406 (Div. 1) A. Berzerk 记忆化搜索的更多相关文章
- Codeforces Round #336 (Div. 2) D. Zuma 记忆化搜索
D. Zuma 题目连接: http://www.codeforces.com/contest/608/problem/D Description Genos recently installed t ...
- Codeforces Round #554 (Div. 2) D 贪心 + 记忆化搜索
https://codeforces.com/contest/1152/problem/D 题意 给你一个n代表合法括号序列的长度一半,一颗有所有合法括号序列构成的字典树上,选择最大的边集,边集的边没 ...
- Codeforces Round #459 (Div. 2):D. MADMAX(记忆化搜索+博弈论)
题意 在一个有向无环图上,两个人分别从一个点出发,两人轮流从当前点沿着某条边移动,要求经过的边权不小于上一轮对方经过的边权(ASCII码),如果一方不能移动,则判负.两人都采取最优策略,求两人分别从每 ...
- 【动态规划】Codeforces Round #406 (Div. 2) C.Berzerk
有向图博弈问题. 能转移到一个必败态的就是必胜态. 能转移到的全是必胜态的就是必败态. 转移的时候可以用队列维护. 可以看这个 http://www.cnblogs.com/quintessence/ ...
- codeforces 793 D. Presents in Bankopolis(记忆化搜索)
题目链接:http://codeforces.com/contest/793/problem/D 题意:给出n个点m条边选择k个点,要求k个点是联通的而且不成环,而且选的边不能包含选过的边不能包含以前 ...
- 牛客假日团队赛5 F 随机数 BZOJ 1662: [Usaco2006 Nov]Round Numbers 圆环数 (dfs记忆化搜索的数位DP)
链接:https://ac.nowcoder.com/acm/contest/984/F 来源:牛客网 随机数 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 32768K,其他语言6 ...
- Codeforces Gym 191033 E. Explosion Exploit (记忆化搜索+状压)
E. Explosion Exploit time limit per test 2.0 s memory limit per test 256 MB input standard input out ...
- CodeForces - 632E Thief in a Shop (FFT+记忆化搜索)
题意:有N种物品,每种物品有价值\(a_i\),每种物品可选任意多个,求拿k件物品,可能损失的价值分别为多少. 分析:相当于求\((a_1+a_2+...+a_n)^k\)中,有哪些项的系数不为0.做 ...
- Codeforces Round #406 (Div. 1)
B题打错调了半天,C题想出来来不及打,还好没有挂题 AC:AB Rank:96 Rating:2125+66->2191 A.Berzerk 题目大意:有一个东东在长度为n的环上(环上点编号0~ ...
随机推荐
- GitHub提交代码后不显示用户名只显示邮箱
提交完代码如图: 解决方案: 右键git bash here 输入命令如下: git config --global user.name "username" git config ...
- Java中日期格式化SimpleDateFormat类包含时区的处理方法
1.前言 需要把格式为“2017-02-23T08:04:02+01:00”转化成”23-02-2017-T15:04:02“格式(中国时区为+08:00所以是15点),通过网上查找答案,发现没有我需 ...
- npm下载速度过慢的解决办法
第一种方式: 在cmd 输入指令:npm config set registry https://registry.npm.taobao.org 不建议使用cnpm! 设置完后,注意检查:输入指令:n ...
- OCM_第十三天课程:Section6 —》数据库性能调优 _结果缓存 /多列数据信息采集统计/采集数据信息保持游标有效
注:本文为原著(其内容来自 腾科教育培训课堂).阅读本文注意事项如下: 1:所有文章的转载请标注本文出处. 2:本文非本人不得用于商业用途.违者将承当相应法律责任. 3:该系列文章目录列表: 一:&l ...
- 测试开发之前端——No8.HTML5中的媒介事件
媒介事件 由视频.图像以及音频等媒介触发的事件. 适用于所有 HTML 5 元素,不过在媒介元素(诸如 audio.embed.img.object 以及 video)中最常用: 属性 值 描述 on ...
- python 全栈开发,Day124(MongoDB初识,增删改查操作,数据类型,$关键字以及$修改器,"$"的奇妙用法,Array Object 的特殊操作,选取跳过排序,客户端操作)
一.MongoDB初识 什么是MongoDB MongoDB 是一个基于分布式文件存储的数据库.由 C++ 语言编写.旨在为 WEB 应用提供可扩展的高性能数据存储解决方案. MongoDB 是一个介 ...
- GItlab作CI/CD时,想快点,有啥招?
如果希望.m2文件有存缓,或是不要每次从dockerhub上找镜像(有的是本地镜像,远程没有的) 那么,gitlab-runner的config.toml初步优化文件如下: concurrent = ...
- thinkphp自定义分页类
先来看下这个分页的样式,没写css,确实丑 什么时候写样式再来上传下css吧...... 就是多一个页面跳转功能 先把这个代码贴一下 <?php namespace Component; cla ...
- openstack网络服务Neutron(六)
一.Neutron控制节点安装 1.Neutron安装 [root@linux-node1 ~]# yum install -y openstack-neutron openstack-neutron ...
- BZOJ3377 [Usaco2004 Open]The Cow Lineup 奶牛序列 其他
欢迎访问~原文出处——博客园-zhouzhendong 去博客园看该题解 题目传送门 - BZOJ3377 题意概括 给出一个序列,序列中的数字为1~k中的. 让你求最短的非子序列长度. 题解 我们把 ...