博客
关于我
Codeforces Round #459 (Div. 1) B. MADMAX(博弈+DP+记忆化搜索)
阅读量:390 次
发布时间:2019-03-05

本文共 1328 字,大约阅读时间需要 4 分钟。

代码展示了一个用于图论问题的深度优先搜索(DFS)算法,采用记忆化搜索技术来优化性能。代码定义了一个三维数组dp,其中dp[i][j][k]表示在节点i作为先手,节点j作为后手,边权为k的情况下,当前玩家是否能够获胜。通过递归函数dfs,算法对每个状态进行分析并存储结果。代码的核心逻辑是遍历每个节点的所有邻接点,对于每个邻接点,检查其边权是否满足当前轮的要求,并递归调用dfs函数判断对手是否处于劣势。如果对手无法获胜,则当前玩家可以获胜,否则无法获胜。代码的初始化部分读取输入数据,构建图的邻接表,并通过调用dfs函数对所有可能的状态进行计算。最终,代码输出每个节点对应的胜负结果,若dp[i][j][0]为0则表示先手无法获胜,否则先手可以获胜。

#include #include 
using namespace std;const int maxn = 150;int dp[maxn][maxn][maxn];vector
> g[maxn];char c;int dfs(int x, int y, int now) { if (dp[x][y][now] >= 0) return dp[x][y][now]; for (auto it : g[x]) { if (it.second >= now && dfs(y, it.first, it.second) == 0) { dp[x][y][now] = 1; return 1; } } dp[x][y][now] = 0; return 0;}int main() { int n, m; scanf("%d%d", &n, &m); for (int i = 1; i <= m; ++i) { int u, v; char c; scanf("%d%d", &u, &v); scanf("%c", &c); g[u].push_back({v, c}); } memset(dp, -1, sizeof(dp)); for (int i = 1; i <= n; ++i) { for (int j = 1; j <= n; ++j) { dfs(i, j, 0); } } for (int i = 1; i <= n; ++i) { for (int j = 1; j <= n; ++j) { if (dp[i][j][0] == 0) { printf("B"); } else { printf("A"); } printf("\n"); } }}

转载地址:http://olewz.baihongyu.com/

你可能感兴趣的文章
python 控制 cmd 命令行颜色
查看>>
Python 操作 Azure Blob Storage
查看>>
Python 操作 Excel 全攻略 | 包括读取、写入、表格操作、图像输出和字体设置
查看>>
Python 操作 JMeter 探索:pymeter 实操指南
查看>>
Python 操作 Redis
查看>>
Python 操作Mysql(PyMysql)
查看>>
Python 操作Redis
查看>>
Python 操作sqlite数据库及保存查询numpy类型数据(一)
查看>>
Python 操作sqlite数据库及保存查询numpy类型数据(二)
查看>>
Python 操作数据库
查看>>
Python 数据分析与可视化实战
查看>>
python 数据可视化 -- matplotlib02
查看>>
python 数据可视化利器
查看>>
Python 数据可视化详解
查看>>
python 数据库查询返回list或tuple
查看>>
Python 数据库连接池管理的基本知识 (附Demo)
查看>>
Python学习记录-2016-12-17
查看>>
Python 数据库骚操作 -- MySQL
查看>>
Python 数据操作详解
查看>>
Python 数据文件与网络数据序列化存储详解
查看>>