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 ...
随机推荐
- MySQL导入大sql 文件大小限制问题的解决
解决过程如下: 1.由于mysql能解析sql的压缩文件,因此将200M压缩后为5M. 2.默认情况下:MySQL导入文件大小有限制的,最大为2M,所以当文件很大时候,直接无法导入,可修改php.in ...
- 《C#高效编程》读书笔记10-使用可选参数减少方法重载数量
C#现在支持调用者一方使用具名参数(named parameter).类型中的名称也成为了公有接口的一部分.修改公有参数名称将有可能破坏掉调用者的代码. public void SetName(str ...
- Teradata 认证系列 - 1. TCPP这是个啥
一看历史,好几年没发帖...正好最近在自学teradata认证(学也不一定学的完,最后也不一定去考,仅仅安慰一下不想碌碌无为的内心) 网上一搜,百度上的中文相关资料简直为0.这个不奇怪,毕竟都没什么人 ...
- CentOS 6.4系统中编译和升级内核
CentOS 6.4系统中编译和升级内核 [日期:2013-08-25] 来源:Linux社区 作者:vipshichg [字体:大 中 小] 可能因为以下几种原因,你可能需要对Linux kern ...
- python随笔---录入月份的值,输出对应的季节
首先获取一个输入,加判断,输入对应的月份,季节判定根据气象划分法(气象划分法:在气象部门,通常以阳历3-5月为春季,6-8月为夏季,9-11月为秋季,12月-来年2月为冬季,并且常常把1.4.7.10 ...
- 查看mysql表和数据库的大小
转自:http://xiaosu.blog.51cto.com/2914416/687835 1.查看数据库的大小 use 数据库名SELECT sum(DATA_LENGTH)+sum(INDEX_ ...
- meterpreter > ps
meterpreter > ps Process List============ PID PPID Name Arch Session User Path --- ---- ---- ---- ...
- 判断一个点是否在多边形区域内--C算法
/*函数的输入:(1)当前点的坐标p(2)区域顶点数组pt[]:(3)顶点数nCount 输出: 在区域内返回TRUE,否则返回FALSE. Point类型是一个结构: struct Point { ...
- SqlServer作业指定目标服务器
用SSMS生成数据库作业的创建脚本的时候,有一步是sp_add_jobserver操作: EXEC @ReturnCode = msdb.dbo.sp_add_jobserver @job_id = ...
- 又一例网卡mtu值引发的问题
通过php上传文件到云存储,很小的文件都无法上传,在别的服务器上测试可以,本机环境是ESXI虚机安装的centos 7版本 解决思路过程 1.让开发写一个单独测试上传的文件,不调php nginx配置 ...