博客
关于我
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/

你可能感兴趣的文章
npm build报错Cannot find module ‘webpack‘解决方法
查看>>
npm ERR! ERESOLVE could not resolve报错
查看>>
npm ERR! fatal: unable to connect to github.com:
查看>>
npm ERR! Unexpected end of JSON input while parsing near '...on":"0.10.3","direc to'
查看>>
npm ERR! Unexpected end of JSON input while parsing near ‘...“:“^1.2.0“,“vue-html-‘ npm ERR! A comp
查看>>
npm error Missing script: “server“npm errornpm error Did you mean this?npm error npm run serve
查看>>
npm error MSB3428: 未能加载 Visual C++ 组件“VCBuild.exe”。要解决此问题,1) 安装
查看>>
npm install CERT_HAS_EXPIRED解决方法
查看>>
npm install digital envelope routines::unsupported解决方法
查看>>
npm install 卡着不动的解决方法
查看>>
npm install 报错 EEXIST File exists 的解决方法
查看>>
npm install 报错 ERR_SOCKET_TIMEOUT 的解决方法
查看>>
npm install 报错 Failed to connect to github.com port 443 的解决方法
查看>>
npm install 报错 fatal: unable to connect to github.com 的解决方法
查看>>
npm install 报错 no such file or directory 的解决方法
查看>>
npm install 权限问题
查看>>
npm install报错,证书验证失败unable to get local issuer certificate
查看>>
npm install无法生成node_modules的解决方法
查看>>
npm install的--save和--save-dev使用说明
查看>>
npm node pm2相关问题
查看>>