<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>top-down Archives - Huahua&#039;s Tech Road</title>
	<atom:link href="https://zxi.mytechroad.com/blog/tag/top-down/feed/" rel="self" type="application/rss+xml" />
	<link>https://zxi.mytechroad.com/blog/tag/top-down/</link>
	<description></description>
	<lastBuildDate>Sun, 17 Jan 2021 22:11:41 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.0.8</generator>

<image>
	<url>https://zxi.mytechroad.com/blog/wp-content/uploads/2017/09/cropped-photo-32x32.jpg</url>
	<title>top-down Archives - Huahua&#039;s Tech Road</title>
	<link>https://zxi.mytechroad.com/blog/tag/top-down/</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>花花酱 LeetCode 1728. Cat and Mouse II</title>
		<link>https://zxi.mytechroad.com/blog/dynamic-programming/leetcode-1728-cat-and-mouse-ii/</link>
					<comments>https://zxi.mytechroad.com/blog/dynamic-programming/leetcode-1728-cat-and-mouse-ii/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Sun, 17 Jan 2021 22:10:36 +0000</pubDate>
				<category><![CDATA[Dynamic Programming]]></category>
		<category><![CDATA[dp]]></category>
		<category><![CDATA[memorization]]></category>
		<category><![CDATA[min-max]]></category>
		<category><![CDATA[top-down]]></category>
		<guid isPermaLink="false">https://zxi.mytechroad.com/blog/?p=7994</guid>

					<description><![CDATA[<p>A game is played by a cat and a mouse named Cat and Mouse. The environment is represented by a&#160;grid&#160;of size&#160;rows x cols, where each&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/dynamic-programming/leetcode-1728-cat-and-mouse-ii/">花花酱 LeetCode 1728. Cat and Mouse II</a> appeared first on <a rel="nofollow" href="https://zxi.mytechroad.com/blog">Huahua&#039;s Tech Road</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<p>A game is played by a cat and a mouse named Cat and Mouse.</p>



<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>



<ul><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>



<p>Mouse and Cat play according to the following rules:</p>



<ul><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>



<p>The game can end in 4 ways:</p>



<ul><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>



<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>



<p><strong>Example 1:</strong></p>



<figure class="wp-block-image"><img src="https://assets.leetcode.com/uploads/2020/09/12/sample_111_1955.png" alt=""/></figure>



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> grid = ["####F","#C...","M...."], catJump = 1, mouseJump = 2
<strong>Output:</strong> true
<strong>Explanation:</strong> Cat cannot catch Mouse on its turn nor can it get the food before Mouse.
</pre>



<p><strong>Example 2:</strong></p>



<figure class="wp-block-image"><img src="https://assets.leetcode.com/uploads/2020/09/12/sample_2_1955.png" alt=""/></figure>



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> grid = ["M.C...F"], catJump = 1, mouseJump = 4
<strong>Output:</strong> true
</pre>



<p><strong>Example 3:</strong></p>



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> grid = ["M.C...F"], catJump = 1, mouseJump = 3
<strong>Output:</strong> false
</pre>



<p><strong>Example 4:</strong></p>



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> grid = ["C...#","...#F","....#","M...."], catJump = 2, mouseJump = 5
<strong>Output:</strong> false
</pre>



<p><strong>Example 5:</strong></p>



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> grid = [".M...","..#..","#..#.","C#.#.","...#F"], catJump = 3, mouseJump = 1
<strong>Output:</strong> true
</pre>



<p><strong>Constraints:</strong></p>



<ul><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>



<h2><strong>Solution: MinMax + Memoization</strong></h2>



<p>Time complexity: O(m^3 * n^3 * max(n, m))<br>Space complexity: O(m^3 * n^3) </p>



<p>state: [mouse_pos, cat_pos, turn]</p>



<div class="responsive-tabs">
<h2 class="tabtitle">C++</h2>
<div class="tabcontent">

<pre class="crayon-plain-tag">// Author: Huahua
class Solution {
public:
  bool canMouseWin(vector&lt;string&gt;&amp; grid, int catJump, int mouseJump) {         
    const int m = grid.size(), n = grid[0].size();        
    int mouse, cat, food;
    for (int pos = 0; pos &lt; m * n; ++pos)      
      switch (grid[pos / n][ pos % n]) {
        case 'M': mouse = pos; break;
        case 'C': cat = pos; break;
        case 'F': food = pos; break;
        default: break;
      }
    
    const vector&lt;pair&lt;int, int&gt;&gt; dirs{{0, -1}, {0, 1}, {-1, 0}, {1, 0}};
    const int limit = m * n * 2;
    vector&lt;vector&lt;vector&lt;int&gt;&gt;&gt; seen(m*n, 
        vector&lt;vector&lt;int&gt;&gt;(m*n, vector&lt;int&gt;(limit + 1, -1)));
    function&lt;bool(int, int, int)&gt; dfs = 
      [&amp;](int mouse, int cat, int d) -&gt; bool {
        const bool isCat = d &amp; 1;
        if (cat == food || cat == mouse || d &gt;= limit) return isCat;
        if (mouse == food) return !isCat;
        int&amp; ans = seen[mouse][cat][d];
        if (ans != -1) return ans;
        const int cur = isCat ? cat : mouse;
        const int jumps = isCat ? catJump : mouseJump;
        for (const auto&amp; [dx, dy] : dirs)
          for (int j = 0; j &lt;= jumps; ++j) {
            const int x = (cur % n) + dx * j;
            const int y = (cur / n) + dy * j;
            const int pos = y * n + x;
            if (x &lt; 0 || x &gt;= n || y &lt; 0 || y &gt;= m || grid[y][x] == '#') break;            
            if (!dfs(isCat ? mouse : pos, isCat ? pos : cat, d + 1)) return ans = true; 
          }
      return ans = false;
    };
    return dfs(mouse, cat, 0);
  }
};</pre>
</div></div>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/dynamic-programming/leetcode-1728-cat-and-mouse-ii/">花花酱 LeetCode 1728. Cat and Mouse II</a> appeared first on <a rel="nofollow" href="https://zxi.mytechroad.com/blog">Huahua&#039;s Tech Road</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://zxi.mytechroad.com/blog/dynamic-programming/leetcode-1728-cat-and-mouse-ii/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
