class Solution {
static final int MOUSE_WIN = 1;
static final int CAT_WIN = 2;
static final int DRAW = 0; int n;
int[][][] dp; // dp[mouse][cat][turns]:表示在经历turns轮次,老鼠在mouse位置,猫在cat位置时游戏的状态
int[][] graph; public int catMouseGame(int[][] graph) {
this.n = graph.length;
this.graph = graph;
this.dp = new int[n][n][n*2]; // 初始化
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
Arrays.fill(dp[i][j], -1);
}
} return getResult(1, 2, 0);
} private int getResult(int mouse, int cat, int turns) {
if (turns == 2*n) {
return DRAW;
} // 未分出比赛状态
if (dp[mouse][cat][turns] == -1) {
if (mouse == 0) {
dp[mouse][cat][turns] = MOUSE_WIN;
} else if (mouse == cat) {
dp[mouse][cat][turns] = CAT_WIN;
} else {
getNextResult(mouse, cat, turns);
}
} return dp[mouse][cat][turns];
} private void getNextResult(int mouse, int cat, int turns) {
// turns 为偶数时,老鼠先走
int curMove = turns % 2 == 0? mouse: cat;
int notHopeResult = curMove == mouse? CAT_WIN: MOUSE_WIN; int result = notHopeResult;
int[] nextNodes = graph[curMove];
for (int next: nextNodes) {
if (curMove == cat && next == 0) continue; // 猫无法移动到0
int nextCat = curMove == cat? next: cat;
int nextMouse = curMove == mouse? next: mouse;
int nextResult = getResult(nextMouse, nextCat, turns + 1); if (nextResult != notHopeResult) { // 对于老鼠来说:他不希望nextResult为猫赢
result = nextResult;
if (result != DRAW) break; // 已经分出胜负,则无需再比赛;若为平局则一直试探下一个next
}
} dp[mouse][cat][turns] = result;
}
}

题解见官方

博弈论[leetocde913]的更多相关文章

  1. IT人生知识分享:博弈论的理性思维

    背景: 昨天看了<最强大脑>,由于节目比较有争议性,不知为什么,作为一名感性的人,就想试一下如果自己理性分析会是怎样的呢? 过程是这样的: 中国队(3人)VS英国队(4人). 1:李建东( ...

  2. [poj2348]Euclid's Game(博弈论+gcd)

    Euclid's Game Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 9033   Accepted: 3695 Des ...

  3. 博弈论揭示了深度学习的未来(译自:Game Theory Reveals the Future of Deep Learning)

    Game Theory Reveals the Future of Deep Learning Carlos E. Perez Deep Learning Patterns, Methodology ...

  4. TYVJ博弈论

    一些比较水的博弈论...(为什么都没有用到那什么SG呢....) TYVJ 1140  飘飘乎居士拯救MM 题解: 歌德巴赫猜想 #include <cmath> #include < ...

  5. Codeforces 549C. The Game Of Parity[博弈论]

    C. The Game Of Parity time limit per test 1 second memory limit per test 256 megabytes input standar ...

  6. 【POJ】2234 Matches Game(博弈论)

    http://poj.org/problem?id=2234 博弈论真是博大精深orz 首先我们仔细分析很容易分析出来,当只有一堆的时候,先手必胜:两堆并且相同的时候,先手必败,反之必胜. 根据博弈论 ...

  7. 博弈论入门小结 分类: ACM TYPE 2014-08-31 10:15 73人阅读 评论(0) 收藏

    文章原地址:http://blog.csdn.net/zhangxiang0125/article/details/6174639 博弈论:是二人或多人在平等的对局中各自利用对方的策略变换自己的对抗策 ...

  8. poj 3710 Christmas Game 博弈论

    思路:首先用Tarjan算法找出树中的环,环为奇数变为边,为偶数变为点. 之后用博弈论的知识:某点的SG值等于子节点+1后的异或和. 代码如下: #include<iostream> #i ...

  9. hdoj 1404 Digital Deletions(博弈论)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1404 一看就是博弈论的题目,但并没有什么思路,看了题解,才明白 就是求六位数的SG函数,暴力一遍,打表 ...

  10. CodeForces 455B A Lot of Games (博弈论)

    A Lot of Games 题目链接: http://acm.hust.edu.cn/vjudge/contest/121334#problem/J Description Andrew, Fedo ...

随机推荐

  1. TM1621断码液晶驱动IC的原理、驱动代码

    TM1621是一个多功能的LCD驱动器,带有蜂鸣器驱动功能.通讯采用四线串行接口 TM1621的难点在于字节序和显存跟屏幕的映射关系上,下面是写寄存器的代码 void Delay_us(uint8_t ...

  2. 【C++】【纯代码】获取电脑的mac地址

    bool GetMacAddress(CString &LanMAC) { #define MAX_ADAPTER_NAME_LENGTH 256 #define MAX_ADAPTER_DE ...

  3. db2存储过程 动态拼接sql 、输出数据集示例

    *****部分都是表名.因为隐私关系,替换为*了. 1 CREATE PROCEDURE "BI_DM"."SP_GCYP_REPORT" ( 2 startd ...

  4. redis的linux下安装

    Linux 源码安装 下载地址:http://redis.io/download,下载最新稳定版本. 本教程使用的最新文档版本为 2.8.17,下载并安装: # wget http://downloa ...

  5. Win10桌面点鼠标右键一直转圈的解决方法

    win10桌面右键菜单就一直转圈,无法调出桌面右键菜单,下面给大家分享一下怎么解决的问题,一起了解一下!            步骤阅读   工具/原料   win10 企业版 HT1902 方法/步 ...

  6. return chain.filter(exchange); 这句啥意思

    答:继续往后执行过滤器,如果不调用这句代码,请求就不会发给控制器了,如果当前执行的过滤器后面还有过滤器,执行那个过滤器,如果没有,就执行控制器. 那我此时想一个请求取消token校验,得在这里加吗? ...

  7. 【git】git子模块操作-从子模块的远端拉取上游修改 & 从项目远端拉取更改

    1.从子模块的远端拉取上游修改 1.1 在项目子模块目录中,运行git fetch与git merge更新本地代码 (1) 这种方法在获取子模块非master分支的更新时,没成功获取更新,没搞清楚原因 ...

  8. notepad++设置

    1.背景颜色 [设置]-[语言格式设置] 选择主题:Solarized-light

  9. R语言MCMC-GARCH、风险价值VaR模型股价波动分析上证指数时间序列

    全文链接:http://tecdat.cn/?p=31717 原文出处:拓端数据部落公众号 分析师:Ke Liu 随着金融市场全球化的发展,金融产品逐渐受到越来越多的关注,而金融产品的风险度量成为投资 ...

  10. Unity2018 用vs2017打开脚本时,不兼容不能加载工程

    修改    <TargetFrameworkVersion>v4.7.1</TargetFrameworkVersion> 为 <TargetFrameworkVersi ...