目录链接:

力扣编程题-解法汇总_分享+记录-CSDN博客

GitHub同步刷题项目:

https://github.com/September26/java-algorithms

原题链接:力扣

描述:

一只猫和一只老鼠在玩一个叫做猫和老鼠的游戏。

它们所处的环境设定是一个 rows x cols 的方格 grid ,其中每个格子可能是一堵墙、一块地板、一位玩家(猫或者老鼠)或者食物。

玩家由字符 'C' (代表猫)和 'M' (代表老鼠)表示。
地板由字符 '.' 表示,玩家可以通过这个格子。
墙用字符 '#' 表示,玩家不能通过这个格子。
食物用字符 'F' 表示,玩家可以通过这个格子。
字符 'C' , 'M' 和 'F' 在 grid 中都只会出现一次。
猫和老鼠按照如下规则移动:

老鼠 先移动 ,然后两名玩家轮流移动。
每一次操作时,猫和老鼠可以跳到上下左右四个方向之一的格子,他们不能跳过墙也不能跳出 grid 。
catJump 和 mouseJump 是猫和老鼠分别跳一次能到达的最远距离,它们也可以跳小于最大距离的长度。
它们可以停留在原地。
老鼠可以跳跃过猫的位置。
游戏有 4 种方式会结束:

如果猫跟老鼠处在相同的位置,那么猫获胜。
如果猫先到达食物,那么猫获胜。
如果老鼠先到达食物,那么老鼠获胜。
如果老鼠不能在 1000 次操作以内到达食物,那么猫获胜。
给你 rows x cols 的矩阵 grid 和两个整数 catJump 和 mouseJump ,双方都采取最优策略,如果老鼠获胜,那么请你返回 true ,否则返回 false 。

示例 1:

输入:grid = ["####F","#C...","M...."], catJump = 1, mouseJump = 2
输出:true
解释:猫无法抓到老鼠,也没法比老鼠先到达食物。
示例 2:

输入:grid = ["M.C...F"], catJump = 1, mouseJump = 4
输出:true
示例 3:

输入:grid = ["M.C...F"], catJump = 1, mouseJump = 3
输出:false
示例 4:

输入:grid = ["C...#","...#F","....#","M...."], catJump = 2, mouseJump = 5
输出:false
示例 5:

输入:grid = [".M...","..#..","#..#.","C#.#.","...#F"], catJump = 3, mouseJump = 1
输出:true

提示:

rows == grid.length
cols = grid[i].length
1 <= rows, cols <= 8
grid[i][j] 只包含字符 'C' ,'M' ,'F' ,'.' 和 '#' 。
grid 中只包含一个 'C' ,'M' 和 'F' 。
1 <= catJump, mouseJump <= 8

来源:力扣(LeetCode)
链接:https://leetcode.cn/problems/cat-and-mouse-ii
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

解题思路:

没做出来,看官方题解的。

动态规划的方案超时了

代码:

public class Solution1728 {static final int MOUSE_TURN = 0, CAT_TURN = 1;static final int UNKNOWN = 0, MOUSE_WIN = 1, CAT_WIN = 2;static final int MAX_MOVES = 1000;int[][] dirs = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}};int rows, cols;String[] grid;int catJump, mouseJump;int food;int[][][] degrees;int[][][][] results;public boolean canMouseWin(String[] grid, int catJump, int mouseJump) {this.rows = grid.length;this.cols = grid[0].length();this.grid = grid;this.catJump = catJump;this.mouseJump = mouseJump;int startMouse = -1, startCat = -1;for (int i = 0; i < rows; i++) {for (int j = 0; j < cols; j++) {char c = grid[i].charAt(j);if (c == 'M') {startMouse = getPos(i, j);} else if (c == 'C') {startCat = getPos(i, j);} else if (c == 'F') {food = getPos(i, j);}}}int total = rows * cols;degrees = new int[total][total][2];results = new int[total][total][2][2];Queue<int[]> queue = new ArrayDeque<int[]>();// 计算每个状态的度for (int mouse = 0; mouse < total; mouse++) {int mouseRow = mouse / cols, mouseCol = mouse % cols;if (grid[mouseRow].charAt(mouseCol) == '#') {continue;}for (int cat = 0; cat < total; cat++) {int catRow = cat / cols, catCol = cat % cols;if (grid[catRow].charAt(catCol) == '#') {continue;}degrees[mouse][cat][MOUSE_TURN]++;degrees[mouse][cat][CAT_TURN]++;for (int[] dir : dirs) {for (int row = mouseRow + dir[0], col = mouseCol + dir[1], jump = 1; row >= 0 && row < rows && col >= 0 && col < cols && grid[row].charAt(col) != '#' && jump <= mouseJump; row += dir[0], col += dir[1], jump++) {int nextMouse = getPos(row, col), nextCat = getPos(catRow, catCol);degrees[nextMouse][nextCat][MOUSE_TURN]++;}for (int row = catRow + dir[0], col = catCol + dir[1], jump = 1; row >= 0 && row < rows && col >= 0 && col < cols && grid[row].charAt(col) != '#' && jump <= catJump; row += dir[0], col += dir[1], jump++) {int nextMouse = getPos(mouseRow, mouseCol), nextCat = getPos(row, col);degrees[nextMouse][nextCat][CAT_TURN]++;}}}}// 猫和老鼠在同一个单元格,猫获胜for (int pos = 0; pos < total; pos++) {int row = pos / cols, col = pos % cols;if (grid[row].charAt(col) == '#') {continue;}results[pos][pos][MOUSE_TURN][0] = CAT_WIN;results[pos][pos][MOUSE_TURN][1] = 0;results[pos][pos][CAT_TURN][0] = CAT_WIN;results[pos][pos][CAT_TURN][1] = 0;queue.offer(new int[]{pos, pos, MOUSE_TURN});queue.offer(new int[]{pos, pos, CAT_TURN});}// 猫和食物在同一个单元格,猫获胜for (int mouse = 0; mouse < total; mouse++) {int mouseRow = mouse / cols, mouseCol = mouse % cols;if (grid[mouseRow].charAt(mouseCol) == '#' || mouse == food) {continue;}results[mouse][food][MOUSE_TURN][0] = CAT_WIN;results[mouse][food][MOUSE_TURN][1] = 0;results[mouse][food][CAT_TURN][0] = CAT_WIN;results[mouse][food][CAT_TURN][1] = 0;queue.offer(new int[]{mouse, food, MOUSE_TURN});queue.offer(new int[]{mouse, food, CAT_TURN});}// 老鼠和食物在同一个单元格且猫和食物不在同一个单元格,老鼠获胜for (int cat = 0; cat < total; cat++) {int catRow = cat / cols, catCol = cat % cols;if (grid[catRow].charAt(catCol) == '#' || cat == food) {continue;}results[food][cat][MOUSE_TURN][0] = MOUSE_WIN;results[food][cat][MOUSE_TURN][1] = 0;results[food][cat][CAT_TURN][0] = MOUSE_WIN;results[food][cat][CAT_TURN][1] = 0;queue.offer(new int[]{food, cat, MOUSE_TURN});queue.offer(new int[]{food, cat, CAT_TURN});}// 拓扑排序while (!queue.isEmpty()) {int[] state = queue.poll();int mouse = state[0], cat = state[1], turn = state[2];int result = results[mouse][cat][turn][0];int moves = results[mouse][cat][turn][1];List<int[]> prevStates = getPrevStates(mouse, cat, turn);for (int[] prevState : prevStates) {int prevMouse = prevState[0], prevCat = prevState[1], prevTurn = prevState[2];if (results[prevMouse][prevCat][prevTurn][0] == UNKNOWN) {boolean canWin = (result == MOUSE_WIN && prevTurn == MOUSE_TURN) || (result == CAT_WIN && prevTurn == CAT_TURN);if (canWin) {results[prevMouse][prevCat][prevTurn][0] = result;results[prevMouse][prevCat][prevTurn][1] = moves + 1;queue.offer(new int[]{prevMouse, prevCat, prevTurn});} else {degrees[prevMouse][prevCat][prevTurn]--;if (degrees[prevMouse][prevCat][prevTurn] == 0) {int loseResult = prevTurn == MOUSE_TURN ? CAT_WIN : MOUSE_WIN;results[prevMouse][prevCat][prevTurn][0] = loseResult;results[prevMouse][prevCat][prevTurn][1] = moves + 1;queue.offer(new int[]{prevMouse, prevCat, prevTurn});}}}}}return results[startMouse][startCat][MOUSE_TURN][0] == MOUSE_WIN && results[startMouse][startCat][MOUSE_TURN][1] <= MAX_MOVES;}public List<int[]> getPrevStates(int mouse, int cat, int turn) {List<int[]> prevStates = new ArrayList<int[]>();int mouseRow = mouse / cols, mouseCol = mouse % cols;int catRow = cat / cols, catCol = cat % cols;int prevTurn = turn == MOUSE_TURN ? CAT_TURN : MOUSE_TURN;int maxJump = prevTurn == MOUSE_TURN ? mouseJump : catJump;int startRow = prevTurn == MOUSE_TURN ? mouseRow : catRow;int startCol = prevTurn == MOUSE_TURN ? mouseCol : catCol;prevStates.add(new int[]{mouse, cat, prevTurn});for (int[] dir : dirs) {for (int i = startRow + dir[0], j = startCol + dir[1], jump = 1; i >= 0 && i < rows && j >= 0 && j < cols && grid[i].charAt(j) != '#' && jump <= maxJump; i += dir[0], j += dir[1], jump++) {int prevMouseRow = prevTurn == MOUSE_TURN ? i : mouseRow;int prevMouseCol = prevTurn == MOUSE_TURN ? j : mouseCol;int prevCatRow = prevTurn == MOUSE_TURN ? catRow : i;int prevCatCol = prevTurn == MOUSE_TURN ? catCol : j;int prevMouse = getPos(prevMouseRow, prevMouseCol);int prevCat = getPos(prevCatRow, prevCatCol);prevStates.add(new int[]{prevMouse, prevCat, prevTurn});}}return prevStates;}public int getPos(int row, int col) {return row * cols + col;}
}

​力扣解法汇总1728-猫和老鼠 II相关推荐

  1. ​力扣解法汇总731-我的日程安排表 II

    目录链接: 力扣编程题-解法汇总_分享+记录-CSDN博客 GitHub同步刷题项目: https://github.com/September26/java-algorithms 原题链接:力扣 描 ...

  2. ​力扣解法汇总522-最长特殊序列 II

    目录链接: 力扣编程题-解法汇总_分享+记录-CSDN博客 GitHub同步刷题项目: https://github.com/September26/java-algorithms 原题链接:力扣 描 ...

  3. ​力扣解法汇总-剑指 Offer II 114. 外星文字典

    目录链接: 力扣编程题-解法汇总_分享+记录-CSDN博客 GitHub同步刷题项目: https://github.com/September26/java-algorithms 原题链接:力扣 描 ...

  4. ​力扣解法汇总954-二倍数对数组

    目录链接: 力扣编程题-解法汇总_分享+记录-CSDN博客 GitHub同步刷题项目: https://github.com/September26/java-algorithms 原题链接:力扣 描 ...

  5. ​力扣解法汇总728-自除数

    目录链接: 力扣编程题-解法汇总_分享+记录-CSDN博客 GitHub同步刷题项目: https://github.com/September26/java-algorithms 原题链接: 力扣 ...

  6. 力扣解法汇总2043-简易银行系统

    目录链接: 力扣编程题-解法汇总_分享+记录-CSDN博客 GitHub同步刷题项目: https://github.com/September26/java-algorithms https://g ...

  7. ​力扣解法汇总2315. 统计星号

    目录链接: 力扣编程题-解法汇总_分享+记录-CSDN博客 GitHub同步刷题项目: https://github.com/September26/java-algorithms 原题链接:力扣 描 ...

  8. 力扣解法汇总720-词典中最长的单词

    目录链接: 力扣编程题-解法汇总_分享+记录-CSDN博客 GitHub同步刷题项目: https://github.com/September26/java-algorithms https://g ...

  9. ​力扣解法汇总764. 最大加号标志

    目录链接: 力扣编程题-解法汇总_分享+记录-CSDN博客 GitHub同步刷题项目: https://github.com/September26/java-algorithms 原题链接:力扣 描 ...

最新文章

  1. 机器学习(三)--- scala学习笔记
  2. pytorch 加载不对齐预训练
  3. MyBatis构建sql时动态传入表名以及字段名
  4. Winform中在使用VS+svn进行协同开发时添加引用时的相对路径和绝对路径的问题
  5. mysql5.7是测试版本吗_mysql免安装版本测试(mysql-5.7.18-winx64)
  6. Servlet案例6:显示用户的上次访问时间
  7. OpenHarmony 1.1.0 LTS 版本发布,十六大性能全面提升
  8. py2exe将python打包成exe
  9. jdbc mysql innodb 死锁 deadlock_Mysql InnoDB 数据更新/删除导致锁表
  10. 谷歌翻译器-谷歌翻译器软件批量自动翻译
  11. utils工具 常见函数
  12. 《ARM 嵌入式系统编程与优化》之 Linux/ARM 嵌入式平台
  13. JAVA计算机毕业设计大学生旅游拼团网站计算机(附源码、数据库)
  14. 【python】52周存钱法改进
  15. 为什么用Win32forth编程的程序员不多
  16. mysql tp5时间倒叙_tp5(thinkPHP5框架)时间查询操作实例分析
  17. 阿里云产品推荐——云数据库RDS MySQL 版
  18. 微信文章临时链接变永久链接
  19. iPhone12、iPhone12 Pro、iPhone12 Max、iPhone12 Pro Max有几个颜色
  20. 马云除了发20亿玩红包,支付宝还干了一件轰动全球的大事儿!

热门文章

  1. Android 图片下载或上传时请求失败。
  2. 如何把PDF文档转换成TXT格式文件
  3. 《读《金色的鱼钩》有感》
  4. Oracle时间戳类型
  5. 计算机图形动画处理实训报告,西安石油大学计算机图形学 实验报告.doc
  6. 抖音中的搞笑BGM如何提取,MP3转换器怎么使用
  7. 如何引流购物粉?如何引流网购粉?怎么吸购物粉?
  8. 工时管理:按工作时间还是完成的任务来跟踪员工的生产力?
  9. AAL模板及脑区功能介绍
  10. 如何精确评估开发时间?