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. mongodb下载和安装

    首先我们去官网下载压缩包 https://www.mongodb.com/download-center/community 然后把文件解压出来 解压出来的文件可以看到里面是没有data和logs,m ...

  2. 「SOL」打扫笛卡尔cartesian (模拟赛)

    为什么会有人推得出来第三题想不出来签到题啊 (⊙_⊙)? 题面 有一棵有根树 \(T\).从根节点出发,在点 \(u\) 时,设点 \(u\) 还有 \(d\) 个未访问过的儿子,则有 \(\frac ...

  3. Java语言打印空心菱形

    打印空心菱形如果一开始觉得比较困难的话,那么不妨分成几个步骤走完成菱形的打印. 1.先打印一个直角三角形(都是由星星组成的) 代码如下: import java.util.Scanner;//导入Sc ...

  4. C#笔记誊录<二>

    c#编译器把源码编译成IL(中间)代码,再由CLR通过即时编译器把IL代码编译成本机机器语言(平台语言) www.webkaka.com//测试服务器的网速 ctrl+k+d 代码对其 CTRL+K+ ...

  5. Go--解析yaml文件

    yaml 文件是目前最常用的配置文件,使用go语言编写代码和工具时,也会用到yaml文件,将服务配置及中间件等信息定义到yaml文件中,后续可根据实际场景来选用. //先下载外部包 go get -u ...

  6. (0409) Pycharm 的设置--参数设置(运行.py文件带参数,例如argument) 比如: demo.py -prj xxx

    1)  https://www.cnblogs.com/yksgzlyh/p/10221960.html 点击"Run"菜单下的"Edit Configurations. ...

  7. 配dns

    cat /etc/resolv.conf

  8. ThinkPHP获取当前url

    $httpType = ((isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == 'on') || (isset($_SERVER['HTT ...

  9. history 添加 时间戳和ip,用户

    vim  /etc/profile 在文件最后加入 HISTFILESIZE=4000 # 默认保存命令是1000条,这里修改为4000条 HISTSIZE=4000 USER_IP=`who -u ...

  10. 如何快速下载xcode等官方app

    为了避免xcode ghost类的风险,用app store又是如此的卡,如何解决呢? https://developer.apple.com/downloads/ 这里各种版本的下载. 绝对官方签名 ...