{"id":7994,"date":"2021-01-17T14:10:36","date_gmt":"2021-01-17T22:10:36","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=7994"},"modified":"2021-01-17T14:11:41","modified_gmt":"2021-01-17T22:11:41","slug":"leetcode-1728-cat-and-mouse-ii","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/dynamic-programming\/leetcode-1728-cat-and-mouse-ii\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 1728. Cat and Mouse II"},"content":{"rendered":"\n<p>A game is played by a cat and a mouse named Cat and Mouse.<\/p>\n\n\n\n<p>The environment is represented by a&nbsp;<code>grid<\/code>&nbsp;of size&nbsp;<code>rows x cols<\/code>, where each element is a wall, floor, player (Cat, Mouse), or food.<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>Players are represented by the characters&nbsp;<code>'C'<\/code>(Cat)<code>,'M'<\/code>(Mouse).<\/li><li>Floors are represented by the character&nbsp;<code>'.'<\/code>&nbsp;and can be walked on.<\/li><li>Walls are represented by the character&nbsp;<code>'#'<\/code>&nbsp;and cannot be walked on.<\/li><li>Food is represented by the character&nbsp;<code>'F'<\/code>&nbsp;and can be walked on.<\/li><li>There is only one of each character&nbsp;<code>'C'<\/code>,&nbsp;<code>'M'<\/code>, and&nbsp;<code>'F'<\/code>&nbsp;in&nbsp;<code>grid<\/code>.<\/li><\/ul>\n\n\n\n<p>Mouse and Cat play according to the following rules:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>Mouse&nbsp;<strong>moves first<\/strong>, then they take turns to move.<\/li><li>During each turn, Cat and Mouse can jump in one of the four directions (left, right, up, down). They cannot jump over the wall nor outside of the&nbsp;<code>grid<\/code>.<\/li><li><code>catJump, mouseJump<\/code>&nbsp;are the maximum lengths Cat and Mouse can jump at a time, respectively. Cat and Mouse can jump less than the maximum length.<\/li><li>Staying in the same position is allowed.<\/li><li>Mouse can jump over Cat.<\/li><\/ul>\n\n\n\n<p>The game can end in 4 ways:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>If Cat occupies the same position as Mouse, Cat wins.<\/li><li>If Cat reaches the food first, Cat wins.<\/li><li>If Mouse reaches the food first, Mouse wins.<\/li><li>If Mouse cannot get to the food within 1000 turns, Cat wins.<\/li><\/ul>\n\n\n\n<p>Given a&nbsp;<code>rows x cols<\/code>&nbsp;matrix&nbsp;<code>grid<\/code>&nbsp;and two integers&nbsp;<code>catJump<\/code>&nbsp;and&nbsp;<code>mouseJump<\/code>, return&nbsp;<code>true<\/code><em>&nbsp;if Mouse can win the game if both Cat and Mouse play optimally, otherwise return&nbsp;<\/em><code>false<\/code>.<\/p>\n\n\n\n<p><strong>Example 1:<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/assets.leetcode.com\/uploads\/2020\/09\/12\/sample_111_1955.png\" alt=\"\"\/><\/figure>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> grid = [\"####F\",\"#C...\",\"M....\"], catJump = 1, mouseJump = 2\n<strong>Output:<\/strong> true\n<strong>Explanation:<\/strong> Cat cannot catch Mouse on its turn nor can it get the food before Mouse.\n<\/pre>\n\n\n\n<p><strong>Example 2:<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/assets.leetcode.com\/uploads\/2020\/09\/12\/sample_2_1955.png\" alt=\"\"\/><\/figure>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> grid = [\"M.C...F\"], catJump = 1, mouseJump = 4\n<strong>Output:<\/strong> true\n<\/pre>\n\n\n\n<p><strong>Example 3:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> grid = [\"M.C...F\"], catJump = 1, mouseJump = 3\n<strong>Output:<\/strong> false\n<\/pre>\n\n\n\n<p><strong>Example 4:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> grid = [\"C...#\",\"...#F\",\"....#\",\"M....\"], catJump = 2, mouseJump = 5\n<strong>Output:<\/strong> false\n<\/pre>\n\n\n\n<p><strong>Example 5:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> grid = [\".M...\",\"..#..\",\"#..#.\",\"C#.#.\",\"...#F\"], catJump = 3, mouseJump = 1\n<strong>Output:<\/strong> true\n<\/pre>\n\n\n\n<p><strong>Constraints:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>rows == grid.length<\/code><\/li><li><code>cols = grid[i].length<\/code><\/li><li><code>1 &lt;= rows, cols &lt;= 8<\/code><\/li><li><code>grid[i][j]<\/code>&nbsp;consist only of characters&nbsp;<code>'C'<\/code>,&nbsp;<code>'M'<\/code>,&nbsp;<code>'F'<\/code>,&nbsp;<code>'.'<\/code>, and&nbsp;<code>'#'<\/code>.<\/li><li>There is only one of each character&nbsp;<code>'C'<\/code>,&nbsp;<code>'M'<\/code>, and&nbsp;<code>'F'<\/code>&nbsp;in&nbsp;<code>grid<\/code>.<\/li><li><code>1 &lt;= catJump, mouseJump &lt;= 8<\/code><\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution: MinMax + Memoization<\/strong><\/h2>\n\n\n\n<p>Time complexity: O(m^3 * n^3 * max(n, m))<br>Space complexity: O(m^3 * n^3) <\/p>\n\n\n\n<p>state: [mouse_pos, cat_pos, turn]<\/p>\n\n\n\n<div class=\"responsive-tabs\">\n<h2 class=\"tabtitle\">C++<\/h2>\n<div class=\"tabcontent\">\n\n<pre lang=\"c++\">\n\/\/ Author: Huahua\nclass Solution {\npublic:\n  bool canMouseWin(vector<string>& grid, int catJump, int mouseJump) {         \n    const int m = grid.size(), n = grid[0].size();        \n    int mouse, cat, food;\n    for (int pos = 0; pos < m * n; ++pos)      \n      switch (grid[pos \/ n][ pos % n]) {\n        case 'M': mouse = pos; break;\n        case 'C': cat = pos; break;\n        case 'F': food = pos; break;\n        default: break;\n      }\n    \n    const vector<pair<int, int>> dirs{{0, -1}, {0, 1}, {-1, 0}, {1, 0}};\n    const int limit = m * n * 2;\n    vector<vector<vector<int>>> seen(m*n, \n        vector<vector<int>>(m*n, vector<int>(limit + 1, -1)));\n    function<bool(int, int, int)> dfs = \n      [&](int mouse, int cat, int d) -> bool {\n        const bool isCat = d & 1;\n        if (cat == food || cat == mouse || d >= limit) return isCat;\n        if (mouse == food) return !isCat;\n        int& ans = seen[mouse][cat][d];\n        if (ans != -1) return ans;\n        const int cur = isCat ? cat : mouse;\n        const int jumps = isCat ? catJump : mouseJump;\n        for (const auto& [dx, dy] : dirs)\n          for (int j = 0; j <= jumps; ++j) {\n            const int x = (cur % n) + dx * j;\n            const int y = (cur \/ n) + dy * j;\n            const int pos = y * n + x;\n            if (x < 0 || x >= n || y < 0 || y >= m || grid[y][x] == '#') break;            \n            if (!dfs(isCat ? mouse : pos, isCat ? pos : cat, d + 1)) return ans = true; \n          }\n      return ans = false;\n    };\n    return dfs(mouse, cat, 0);\n  }\n};\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>A game is played by a cat and a mouse named Cat and Mouse. The environment is represented by a&nbsp;grid&nbsp;of size&nbsp;rows x cols, where each&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[46],"tags":[18,688,25,689],"class_list":["post-7994","post","type-post","status-publish","format-standard","hentry","category-dynamic-programming","tag-dp","tag-memorization","tag-min-max","tag-top-down","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/7994","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/comments?post=7994"}],"version-history":[{"count":2,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/7994\/revisions"}],"predecessor-version":[{"id":7996,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/7994\/revisions\/7996"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=7994"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=7994"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=7994"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}