博弈论[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 ...
随机推荐
- 计算机视觉——SSD和YOLO简介
前言 本文记录用,防止遗忘 计算机视觉--SSD和YOLO简介 课件(单发多框检测SSD) 生成锚框 对每个像素,生成多个以它为中心的锚框 给定n个大小 s1, ...,s2,和m个高宽比,那么生成 ...
- Windows 11安装etcd
一.从官方网站找到Windows版的安装包下载 https://etcd.io/ 把etcd的压缩包解压到D:/soft/etcd文件夹下,首先运行etcd.exe,这是启动etcd服务的,接着就可以 ...
- &以及&&的用法总结
&有两种用法 1.取地址 2.引用 取地址和引用没有任何关系,不要瞎联系! 1.取地址: // 很常规,仅此而已 std::string *p = &s; 2.引用: a)引用是某一个 ...
- vue vant3上传图片文件以流的形式上传
axios.post("/fjt_fast/sys/comm/upload", { file: param.file}, { headers: { 'Content-Type': ...
- 【未完】【DDR系列文章收集】
资料来源 1.https://zhuanlan.zhihu.com/p/343262874 (1)主要讲DRAM刷新的内容: 为什么需要刷新(漏电流导致电容电荷的流失)? 刷新的本质(对存储数据的电容 ...
- 嵌入式C设计模式 - 观察者模式
当对象间存在一对多关系时,则使用观察者模式(Observer Pattern).比如,当一个对象被修改时,则会自动通知依赖它的对象.观察者模式属于行为型模式. 1.观察者模式解决一对多的问题,例如一个 ...
- Linux LVM分区相关知识
Linux分区有多种方式,一种是LVM格式的比较方便,另一种是标准分区扩容比较麻烦,麻烦的事情那么出错的概率也就越大,所以建议生产环境上分区都使用LVM格式硬盘分区. 一. 什么叫LVM? L ...
- yolov5学习遇到的困难
本文观看目标检测 YOLOv5 开源代码项目调试与讲解实战[土堆 x 布尔艺数]_哔哩哔哩_bilibili视频来部署yolov5环境. 问题1:pycocotools工具包安装问题,参照视频2解决W ...
- 配置全局路由表和VRF路由表之间的路由泄漏
1.拓扑图 2.R1配置 R1#sho run Building configuration... Current configuration : 1360 bytes ! upgrade fpd a ...
- 【Windows】IP 地址变为 169.254.x.x 的解决方法
最近每次开机 IP 地址都会自动跳至 169.254.x.x 段,连接不上网络,设置了固定 IP 也会跳段.因此找到以下解决方法: 『IP地址变成169.254.x.x 和固定ip两个IP地址的解决办 ...