Codeforces Round #459 (Div. 2):D. MADMAX(记忆化搜索+博弈论)
D. MADMAX
time limit per test1 second
memory limit per test256 megabytes
Problem Description
As we all know, Max is the best video game player among her friends. Her friends were so jealous of hers, that they created an actual game just to prove that she’s not the best at games. The game is played on a directed acyclic graph (a DAG) with n vertices and m edges. There’s a character written on each edge, a lowercase English letter.
Max and Lucas are playing the game. Max goes first, then Lucas, then Max again and so on. Each player has a marble, initially located at some vertex. Each player in his/her turn should move his/her marble along some edge (a player can move the marble from vertex v to vertex u if there’s an outgoing edge from v to u). If the player moves his/her marble from vertex v to vertex u, the “character” of that round is the character written on the edge from v to u. There’s one additional rule; the ASCII code of character of round i should be greater than or equal to the ASCII code of character of round i - 1 (for i > 1). The rounds are numbered for both players together, i. e. Max goes in odd numbers, Lucas goes in even numbers. The player that can’t make a move loses the game. The marbles may be at the same vertex at the same time.
Since the game could take a while and Lucas and Max have to focus on finding Dart, they don’t have time to play. So they asked you, if they both play optimally, who wins the game?
You have to determine the winner of the game for all initial positions of the marbles.
Input
The first line of input contains two integers n and m (2 ≤ n ≤ 100, ).
The next m lines contain the edges. Each line contains two integers v, u and a lowercase English letter c, meaning there’s an edge from v to u written c on it (1 ≤ v, u ≤ n, v ≠ u). There’s at most one edge between any pair of vertices. It is guaranteed that the graph is acyclic.
Output
Print n lines, a string of length n in each one. The j-th character in i-th line should be ‘A’ if Max will win the game in case her marble is initially at vertex i and Lucas’s marble is initially at vertex j, and ‘B’ otherwise.
Examples
input
4 4
1 2 b
1 3 a
2 4 c
3 4 b
output
BAAA
ABAA
BBBA
BBBB
input
5 8
5 3 h
1 2 c
3 1 c
3 2 r
5 1 r
4 3 z
5 4 r
5 2 h
output
BABBB
BBBBB
AABBB
AAABA
AAAAB
Note
Here’s the graph in the first sample test case:
Here’s the graph in the second sample test case:
解题心得:
- 其实是一个简单的记忆化搜索加博弈,难点就是dp需要开一个四维的数组来表示状态,dp[x1][x2][x3][x4]表示A在x1的位置,B在x2的位置,当前行走的路权值至少是x3,该轮到x4走。每次代表一个状态,这也是博弈论相关的思想,不管该怎么走,只要知道在这个状态所代表的输赢关系就可以了。
#include <bits/stdc++.h>
using namespace std;
const int maxn = 110;
vector <pair<int,int> > ve[maxn];
int n,m;
int dp[maxn][maxn][maxn][3];
void init()
{
memset(dp,-1,sizeof(dp));
for(int i=0;i<m;i++)
{
int a,b;
char s[10];
scanf("%d%d",&a,&b);
scanf("%s",s);
int c = s[0] - 'a';
ve[a].emplace_back(b,c);
}
}
int dfs(int x,int y,int va,int turn)
{
if(dp[x][y][va][turn] != -1)
return dp[x][y][va][turn];
if(turn == 0)//该A走
{
for(int i=0;i<ve[x].size();i++) {
pair<int, int> v = ve[x][i];
if (v.second >= va)
if (dfs(v.first, y, v.second, 1 - turn) == 0)
return dp[x][y][va][turn] = 0;
}
return dp[x][y][va][turn] = 1;
}
else
{
for(int i=0;i<ve[y].size();i++) {
pair<int,int> v = ve[y][i];
if(v.second >= va)
if(dfs(x,v.first,v.second,1 - turn) == 1)
return dp[x][y][va][turn] = 1;
}
return dp[x][y][va][turn] = 0;
}
}
int main()
{
scanf("%d%d",&n,&m);
init();
for(int i=1;i<=n;i++)
{
for(int j=1;j<=n;j++)
{
if(dfs(i,j,-1,0))
printf("B");
else
printf("A");
}
printf("\n");
}
return 0;
}
Codeforces Round #459 (Div. 2):D. MADMAX(记忆化搜索+博弈论)的更多相关文章
- Codeforces Round #459 (Div. 2):D. MADMAX(记忆化搜索+博弈论)
题意 在一个有向无环图上,两个人分别从一个点出发,两人轮流从当前点沿着某条边移动,要求经过的边权不小于上一轮对方经过的边权(ASCII码),如果一方不能移动,则判负.两人都采取最优策略,求两人分别从每 ...
- Codeforces Round #336 (Div. 2) D. Zuma 记忆化搜索
D. Zuma 题目连接: http://www.codeforces.com/contest/608/problem/D Description Genos recently installed t ...
- Codeforces Round #406 (Div. 1) A. Berzerk 记忆化搜索
A. Berzerk 题目连接: http://codeforces.com/contest/786/problem/A Description Rick and Morty are playing ...
- Codeforces Round #554 (Div. 2) D 贪心 + 记忆化搜索
https://codeforces.com/contest/1152/problem/D 题意 给你一个n代表合法括号序列的长度一半,一颗有所有合法括号序列构成的字典树上,选择最大的边集,边集的边没 ...
- Codeforces Round #459 (Div. 2) D. MADMAX DFS+博弈
D. MADMAX time limit per test 1 second memory limit per test 256 megabytes input standard input outp ...
- Codeforces Round #459 (Div. 2)
A. Eleven time limit per test 1 second memory limit per test 256 megabytes input standard input outp ...
- 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 ...
随机推荐
- CQRS之旅——旅程1(我们的领域:Contoso会议管理系统)
旅程1:我们的领域:Contoso会议管理系统 起点:我们从哪里来,我们带来了什么,谁将与我们同行?" 只要前进,我愿意去任何地方." --大卫•利文斯通 本章介绍了一个虚构的公司 ...
- <linux下extmail服务的搭建>
下载2个软件包: extmail-1.1.0.tar.gz extman-1.1.tar.gz 下载地址:http://www.cpan.org/ 创建extsuite目录 mkdir /va ...
- python复数
复数的概念在很久以前,数学家们被下面的等式困扰.x2=-1这是因为任何实数(无论正负)乘以自己总会得到一个非负数.一个数怎么可以乘以自己得到一负数?没有这样的实数存在.就这样18世纪,数学家们发了一个 ...
- oracle中scott用户下四个基本表SQL语句练习
--选择部门中30的雇员SELECT * from emp where DEPTNO=30;--列出所有办事员的姓名.部门.编号--采用内连接方式,也就是等值链接,也是最常用的链接SELECT ena ...
- Spring cloud Eureka 服务治理(注册服务提供者)
搭建完成服务注册中心,下一步可以创建服务提供者并向注册中心注册服务. 接下来我们创建Spring Boot 应用将其加入Eureka服务治理体系中去. 直接使用签名章节创建hello服务项目改造: 1 ...
- html5 知识总结
Meta基础知识: H5页面窗口自动调整到设备宽度,并禁止用户缩放页面 //一.HTML页面结构<meta name="viewport" content=" ...
- cf1040E. Network Safety(并查集)
题意 题目链接 一张图,n个点,m条边,每个点有个权值x,x<=1e18.如果一条边的两个端点不一样,那么这条边是安全的,开始时所有边都是安全的. 现在有一个病毒y,病毒可以入侵任意的点,入侵一 ...
- HttpURLConnection(http 1.1) 用法、状态码、状态描述
最近研究了java的HttpURLConnection的用法, 这里简单的做一下记录: Java中可以使用HttpURLConnection来请求WEB资源. 1. URL请求的类别 分为二类,GET ...
- iOS-加载数据的实现-MJRefresh
使用CocoaPods加载三方库: pod 'MJRefresh' MJRefresh类结构图: 具体实现方法和效果图: The drop-down refresh 01-Default self.t ...
- Python3基础12(collections、struct、itertools、chardet等的使用)
import struct import base64import itertoolsimport chardet from collections import namedtuple,default ...