博客
关于我
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--Redis实战:第四章:数据安全与性能保障:第4节:复制
查看>>
python--基本代码规范
查看>>
Python--抽象类接口类
查看>>
PYTHON--欧拉习题二
查看>>
python--高级应用总结
查看>>
python-520表白代码,我看谁说程序员不懂浪漫,送你几套表白代码,一步到位
查看>>
Python-Asyncio TypeError:无法在‘;等待‘;表达式中使用对象DICT
查看>>
Python-AttributeError:‘;dict&39;对象没有属性‘;序列‘;
查看>>
Python-ldap:是否可以在不显式写入密码的情况下进行绑定?
查看>>
python出现 unknown encoding: idna 的处理方法
查看>>
Python出现 RuntimeError: dictionary changed size during iteration 的解决方法
查看>>
Python-numpy(2)数组的操作
查看>>
Python-Numpy的tile函数用法
查看>>
python-open函数
查看>>
Python-Pandas-将特定函数应用于给定级别-多索引数据框架
查看>>
python-queue队列通信
查看>>
python-ros No module named PyKDL
查看>>
python-social-auth 是否需要会话
查看>>
Python再获年度编程语言,微软或成最大赢家
查看>>
python-week5
查看>>