博弈论[leetocde913]
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]的更多相关文章
- IT人生知识分享:博弈论的理性思维
背景: 昨天看了<最强大脑>,由于节目比较有争议性,不知为什么,作为一名感性的人,就想试一下如果自己理性分析会是怎样的呢? 过程是这样的: 中国队(3人)VS英国队(4人). 1:李建东( ...
- [poj2348]Euclid's Game(博弈论+gcd)
Euclid's Game Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 9033 Accepted: 3695 Des ...
- 博弈论揭示了深度学习的未来(译自:Game Theory Reveals the Future of Deep Learning)
Game Theory Reveals the Future of Deep Learning Carlos E. Perez Deep Learning Patterns, Methodology ...
- TYVJ博弈论
一些比较水的博弈论...(为什么都没有用到那什么SG呢....) TYVJ 1140 飘飘乎居士拯救MM 题解: 歌德巴赫猜想 #include <cmath> #include < ...
- 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 ...
- 【POJ】2234 Matches Game(博弈论)
http://poj.org/problem?id=2234 博弈论真是博大精深orz 首先我们仔细分析很容易分析出来,当只有一堆的时候,先手必胜:两堆并且相同的时候,先手必败,反之必胜. 根据博弈论 ...
- 博弈论入门小结 分类: ACM TYPE 2014-08-31 10:15 73人阅读 评论(0) 收藏
文章原地址:http://blog.csdn.net/zhangxiang0125/article/details/6174639 博弈论:是二人或多人在平等的对局中各自利用对方的策略变换自己的对抗策 ...
- poj 3710 Christmas Game 博弈论
思路:首先用Tarjan算法找出树中的环,环为奇数变为边,为偶数变为点. 之后用博弈论的知识:某点的SG值等于子节点+1后的异或和. 代码如下: #include<iostream> #i ...
- hdoj 1404 Digital Deletions(博弈论)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1404 一看就是博弈论的题目,但并没有什么思路,看了题解,才明白 就是求六位数的SG函数,暴力一遍,打表 ...
- CodeForces 455B A Lot of Games (博弈论)
A Lot of Games 题目链接: http://acm.hust.edu.cn/vjudge/contest/121334#problem/J Description Andrew, Fedo ...
随机推荐
- 如何将SinglaR与MVC和Web Api集成
一:SinglaR与MVC结合 1:新建一个MVC的项目 2:新建一个SignalR"永久链接类"=>MyConnection1 3:主要的代码要写一个泛型继承Control ...
- SQL开窗函数用法
开窗函数分类: 根据使用的目的,开窗函数可以分为两类:聚合开窗函数和排序开窗函数. 下面主要解析四种常用的排序开窗函数: 1.ROW_NUMBER() OVER () : 对相等的值不进行区分,序号连 ...
- K8s validating data: the server could not find the requested resource ... with --validate=false
转载: https://blog.csdn.net/yangchao1125/article/details/106193107/
- 使用layui+jQuery实现点击数据修改,即点即改。
使用layui+jQuery实现点击数据修改即可修改 首先要用到layui的官网手册 地址:https://www.layui.com/ 注意1. 此功能是在使用layui展示数据的基础上实现 3. ...
- regex cheat sheet
regex pattern visualizer : regex101: build, test, and debug regex https://regex101.com/ regex regex ...
- 第二周day6
第二周day6,星期六 所花时间:0 代码量:0 搏客量:1 了解到的知识点:多锻炼.
- uniapp改变icon
<!DOCTYPE html><html lang="zh-CN"> <head> <meta charset="utf-8&q ...
- mysql慢sql监控
1.思路 之前用 mysql 一直没有考虑到这点,mysql 慢 sql 监控是很重要的,它能帮我们梳理我们的业务 sql 到底是哪里处了问题,那么慢 sql 监控怎么做呢? 有两种思路来实现: 在应 ...
- PCB Layout之EMMC_Flash走线总结@@@
PCB Layout之EMMC_Flash走线总结 1,数据线DATA[0-7]走线要(基本)等长(含芯片内部线长),线要短,线间距控制3W原则,参考面要完整(参考面下面最好不要走其它高速信号线),阻 ...
- excel 巧用功能
1. 分类汇总 数据-->分类汇总--> 解决问题:解决了我按字段分类并分页打印的问题,例如几十个村数据,要按村分页打印相关数据这时不能把村分别复制粘贴到一个一个工作薄,太麻烦了. 处理方 ...