Codeforces Round #406 (Div. 1) A. Berzerk 记忆化搜索
来源:互联网 发布:python 遥感贝叶斯 编辑:程序博客网 时间:2024/06/10 09:07
参考:https://www.cnblogs.com/qscqesze/p/6628793.html
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;#define LL long long#define N 8010string ans[3]={"Lose","Loop","Win"};int n;vector<int>Move[2];int Flag[2][N];int cnt[2][N];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(cnt[!x][(y+n-Move[!x][i])%n]<Move[!x].size()){ cnt[!x][(y+n-Move[!x][i])%n]++; } if(cnt[!x][(y+n-Move[!x][i])%n]==Move[!x].size()){ dfs(!x,(y+n-Move[!x][i])%n,-1); } } }}int main(){ //freopen("in.txt","r",stdin); scanf("%d",&n); for(int tp=0;tp<2;++tp){ int k; scanf("%d",&k); for(int i=0;i<k;++i){ int tmp; scanf("%d",&tmp); Move[tp].push_back(tmp); } } Flag[1][0]=-1; dfs(0,0,-1); dfs(1,0,-1); for(int tp=0;tp<2;++tp){ for(int i=1;i<n;++i){ cout<<ans[Flag[tp][i]+1]<<' '; }puts(""); }}
- Codeforces Round #406 (Div. 1) A. Berzerk 记忆化搜索
- Codeforces Round #406 (Div. 2):C. Berzerk(记忆化搜索解决博弈问题)
- Codeforces Round #406 C. Berzerk(搜索)
- Codeforces Round #345 (Div. 2)(A)(记忆化搜索)
- Codeforces Round #390 (Div. 2)(A,B,C(记忆化搜索),D(贪心,优先队列))
- Codeforces Round #208 (Div. 2) D(记忆化搜索)
- Codeforces Round #247 (Div. 2)(C)记忆化搜索
- Codeforces Round #427 (Div. 2)-D(记忆化搜索)
- Codeforces Round #230 (Div. 1) B. Tower of Hanoi 记忆化搜索
- Codeforces Round #313 (Div. 1) E. Gerald and Path dp 记忆化搜索 stl应用
- Codeforces Round #406 (Div. 2) C. Berzerk(有向图博弈)
- codeforces Codeforces Round #130 (Div. 2) B 208B Solitaire 记忆化搜索
- Codeforces Round #220 (Div. 2) C. Inna and Dima (记忆化搜索)
- Codeforces Round #224 (Div. 2) D 暴力搜索加记忆化
- Codeforces Round #220 (Div. 2)C题:Inna and Dima(记忆化搜索+DP)
- Codeforces Round #174 (Div. 2)---D. Cow Program(dp, 记忆化搜索)
- Codeforces Round #106(Div. 2) 149D. Coloring Brackets 区间DP 记忆化搜索
- Codeforces Round #389 (Div. 2) E. Santa Claus and Tangerines 二分+贪心+记忆化搜索
- Eclipse neon 安装SVN插件
- Haar-like、HoG 、LBP 三种描述方法在目标识别中的优劣
- 利用镜面反射让游戏闪耀起来
- 排序算法之堆排序--Java语言
- 关于Yii框架基础的增删查改
- Codeforces Round #406 (Div. 1) A. Berzerk 记忆化搜索
- [微信小程序]点击切换卡片动画效果
- 使用Python获取网站的图片(2.7和3版本)
- 二、开始Tango API(C语言版)
- 用函数编写计算m!
- spring boot 配置 swagger2
- 打开文档变肉鸡:潜伏17年的“噩梦公式”Office漏洞攻击分析
- Ubuntu系统修复
- mjz