CodeForces 918D MADMAX(博弈+记忆化搜索)
1 second
256 megabytes
standard input
standard output
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.
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 vto 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.
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.
4 4
1 2 b
1 3 a
2 4 c
3 4 b
BAAA
ABAA
BBBA
BBBB
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
BABBB
BBBBB
AABBB
AAABA
AAAAB
题意:给出一个有向无环图,每条边的边权都是小写字母,两个人分别从i点和j点出发,每个人能走的边边权必须大于等于上一个人走的边权,如果一个人无路可走,他就输了。
打印i=1~n,j=1~n的胜负表。 题解:GG我又双叒叕被题意杀了,写了个bfs结果呵呵
第一次接触到博弈dp
dp[i][j][v]表示先手在i,后手在j,上一条边走的边权为v的胜负情况
0表示后手必胜,1表示先手必胜
开始考虑转移
假设u点是与i点相连,边权为c
则只要有满足状态dp[j][u][c]=0的v存在,那么dp[i][j][v]=1(对手必败,则我们必胜)否则dp[i][j][v]=0
然后记忆化搜索一下,就可以了 代码如下:
#include<cstdio>
#include<vector>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std; vector< pair<int,int> > g[];
int n,m;
int f[][][]; int dfs(int a,int b,int v)
{
if(f[a][b][v]!=-)
{
return f[a][b][v];
}
f[a][b][v]=;
for(int i=;i<g[a].size();i++)
{
int y=g[a][i].first;
int w=g[a][i].second;
if(w>=v)
{
if(!dfs(b,y,w))
{
f[a][b][v]=;
break;
}
}
}
return f[a][b][v];
} int main()
{
memset(f,-,sizeof(f));
scanf("%d%d",&n,&m);
for(int i=;i<=m;i++)
{
char c;
int from,to;
scanf("\n%d %d %c",&from,&to,&c);
g[from].push_back(make_pair(to,c-'a'));
}
for(int i=;i<=n;i++)
{
for(int j=;j<=n;j++)
{
if(dfs(i,j,))
{
printf("A");
}
else
{
printf("B");
}
}
printf("\n");
}
}
CodeForces 918D MADMAX(博弈+记忆化搜索)的更多相关文章
- Codeforces Round #459 (Div. 2):D. MADMAX(记忆化搜索+博弈论)
D. MADMAX time limit per test1 second memory limit per test256 megabytes Problem Description As we a ...
- CodeForces 173C Spiral Maximum 记忆化搜索 滚动数组优化
Spiral Maximum 题目连接: http://codeforces.com/problemset/problem/173/C Description Let's consider a k × ...
- CodeForces 398B 概率DP 记忆化搜索
题目:http://codeforces.com/contest/398/problem/B 有点似曾相识的感觉,记忆中上次那个跟这个相似的 我是用了 暴力搜索过掉的,今天这个肯定不行了,dp方程想了 ...
- CodeForces 132C Logo Turtle (记忆化搜索)
Description A lot of people associate Logo programming language with turtle graphics. In this case t ...
- BZOJ 3895 3895: 取石子 / Luogu SP9934 ALICE - Alice and Bob (博弈 记忆化搜索)
转自PoPoQQQ大佬博客 题目大意:给定n堆石子,两人轮流操作,每个人可以合并两堆石子或拿走一个石子,不能操作者输,问是否先手必胜 直接想很难搞,我们不妨来考虑一个特殊情况 假设每堆石子的数量都&g ...
- Codeforces 667C Reberland Linguistics 记忆化搜索
链接 Codeforces 667C Reberland Linguistics 题意 给你一个字符串,除去前5个字符串后,使剩下的串有长度为2或3的词根组成,相邻的词根不能重复.找到所有的词根 思路 ...
- UVaLive 5760 Alice and Bob (博弈 + 记忆化搜索)
题意:有 n 堆石子,有两种操作,一种是从一堆中拿走一个,另一种是把两堆合并起来,Alice 先拿,谁不能拿了谁输,问谁胜. 析:某些堆石子数量为 1 是特殊,石子数量大于 1 个的都合并起来,再拿, ...
- HDU 4111 Alice and Bob (博弈+记忆化搜索)
题意:给定 n 堆石头,然后有两种操作,一种是把从任意一堆拿走一个,另一种是把一个石子放到另一堆上. 析:整体看,这个题真是不好做,dp[a][b] 表示有 a 堆1个石子,b个操作,操作是指把其他的 ...
- Codeforces #564div2 E1(记忆化搜索)
虽然不是正解但毕竟自己做出来了还是记下来吧- 对每个人分别dfs得到其期望,某两维的组合情况有限所以Hash一下避免了MLE. #include <cstdio> #include < ...
随机推荐
- [poj2923]Relocation_状压dp_01背包
Relocation poj-2923 题目大意:给出n个物品,有两辆车,两辆车必须一起出动并且每辆车有单独的容量.问最少需要运输多少次才能运走所有货物. 注释:n<=10,容量,物品代价< ...
- <经验杂谈>C#使用AES加密解密的简单介绍
AES 算法是基于置换和代替的.置换是数据的重新排列,而代替是用一个单元数据替换另一个.AES 使用了几种不同的技术来实现置换和替换. 以下是我自己用c#研究出来算法Code: /// <sum ...
- 201621123060《JAVA程序设计》第十周学习总结
1. 本周学习总结 1.1 以你喜欢的方式(思维导图或其他)归纳总结异常相关内容. 2. 书面作业 本次PTA作业题集异常 1. 常用异常 结合题集题目7-1回答 1.1 自己以前编写的代码中经常出现 ...
- 代码中输入数字自动筛选出最大值,使用array,for loop and if (21.9.2017)
# include <stdio.h> # define N main(){ int a, b; ,,,,,,,,,,,,,,,,}; //array中输入需要排序的数字 ]; ; a & ...
- 翻译:CREATE FUNCTION语句(已提交到MariaDB官方手册)
本文为mariadb官方手册:CREATE FUNCTION的译文. 原文:https://mariadb.com/kb/en/library/create-function/我提交到MariaDB官 ...
- vue jsx 使用指南
vue jsx 使用指南 vue jsx 语法与 react jsx 还是有些不一样,在这里记录下. let component = null // if 语句 if (true) { compone ...
- jenkins 简单实现php集成上线部署
基于公司git版本控制,搭建jenkins实现php集成部署(没有用gitlab,测试服配置较低,gitlab卡的不要不要的了-) 一.安装jenkins相关依赖 wget -O /etc/yum.r ...
- Spring知识点回顾(08)spring aware
Spring知识点回顾(08)spring aware BeanNameAware 获得容器中的bean名称 BeanFactoryAware 获得当前的bean factory Applicatio ...
- Docker学习笔记 - Docker的远程访问
学习内容: 配置客户端与守护进程的远程访问 服务端配置-H选项: 使服务端支持远程被访问 客户端使用-H选项: 使客户端访问远程服务端 本地环境DOCKER_HOST设置客户端访问的默认服务端地址 准 ...
- Echarts 中国地图(包括china.js文件)
用Echarts写中国地图需要导入china.js(现在官方不提供下载,个人找的在最下面有),根据需要的效果如下.位置可以自己在option里面修改 <!DOCTYPE html> < ...