<?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>board Archives - Huahua&#039;s Tech Road</title>
	<atom:link href="https://zxi.mytechroad.com/blog/tag/board/feed/" rel="self" type="application/rss+xml" />
	<link>https://zxi.mytechroad.com/blog/tag/board/</link>
	<description></description>
	<lastBuildDate>Sun, 29 Dec 2019 09:53:33 +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>board Archives - Huahua&#039;s Tech Road</title>
	<link>https://zxi.mytechroad.com/blog/tag/board/</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>花花酱 LeetCode 1301. Number of Paths with Max Score</title>
		<link>https://zxi.mytechroad.com/blog/dynamic-programming/leetcode-1301-number-of-paths-with-max-score/</link>
					<comments>https://zxi.mytechroad.com/blog/dynamic-programming/leetcode-1301-number-of-paths-with-max-score/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Sat, 28 Dec 2019 18:20:16 +0000</pubDate>
				<category><![CDATA[Dynamic Programming]]></category>
		<category><![CDATA[board]]></category>
		<category><![CDATA[dp]]></category>
		<category><![CDATA[hard]]></category>
		<category><![CDATA[O(n^2)]]></category>
		<guid isPermaLink="false">https://zxi.mytechroad.com/blog/?p=6001</guid>

					<description><![CDATA[<p>You are given a square&#160;board&#160;of characters. You can move on the board starting at the bottom right square marked with the character&#160;'S'. You need&#160;to reach&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/dynamic-programming/leetcode-1301-number-of-paths-with-max-score/">花花酱 LeetCode 1301. Number of Paths with Max Score</a> appeared first on <a rel="nofollow" href="https://zxi.mytechroad.com/blog">Huahua&#039;s Tech Road</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<figure class="wp-block-embed-youtube wp-block-embed is-type-video is-provider-youtube wp-embed-aspect-4-3 wp-has-aspect-ratio"><div class="wp-block-embed__wrapper">
<iframe title="花花酱 LeetCode 1301. Number of Paths with Max Score - 刷题找工作 EP289" width="500" height="375" src="https://www.youtube.com/embed/WwdjLkWmDPs?feature=oembed" frameborder="0" allow="accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe>
</div></figure>



<p>You are given a square&nbsp;<code>board</code>&nbsp;of characters. You can move on the board starting at the bottom right square marked with the character&nbsp;<code>'S'</code>.</p>



<p>You need&nbsp;to reach the top left square marked with the character&nbsp;<code>'E'</code>. The rest of the squares are labeled either with a numeric character&nbsp;<code>1, 2, ..., 9</code>&nbsp;or with an obstacle&nbsp;<code>'X'</code>. In one move you can go up, left or up-left (diagonally) only if there is no obstacle there.</p>



<p>Return a list of two integers: the first integer is the maximum sum of numeric characters you can collect, and the second is the number of such paths that you can take to get that maximum sum,&nbsp;<strong>taken modulo&nbsp;<code>10^9 + 7</code></strong>.</p>



<p>In case there is no path, return&nbsp;<code>[0, 0]</code>.</p>



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



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> board = ["E23","2X2","12S"]
<strong>Output:</strong> [7,1]
</pre>



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



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> board = ["E12","1X1","21S"]
<strong>Output:</strong> [4,2]
</pre>



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



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> board = ["E11","XXX","11S"]
<strong>Output:</strong> [0,0]
</pre>



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



<ul><li><code>2 &lt;= board.length == board[i].length &lt;= 100</code></li></ul>



<h2><strong>Solution: DP</strong></h2>



<figure class="wp-block-image"><img width="960" height="540" src="https://zxi.mytechroad.com/blog/wp-content/uploads/2019/12/1301-ep299.png" alt="" class="wp-image-6007" srcset="https://zxi.mytechroad.com/blog/wp-content/uploads/2019/12/1301-ep299.png 960w, https://zxi.mytechroad.com/blog/wp-content/uploads/2019/12/1301-ep299-300x169.png 300w, https://zxi.mytechroad.com/blog/wp-content/uploads/2019/12/1301-ep299-768x432.png 768w" sizes="(max-width: 960px) 100vw, 960px" /></figure>



<p>dp[i][j] := max score when reach (j, i)<br>count[i][j] := path to reach (j, i) with max score<br><br>m = max(dp[i + 1][j], dp[i][j+1], dp[i+1][j+1])<br>dp[i][j] = board[i][j] + m<br>count[i][j] += count[i+1][j] if dp[i+1][j] == m<br>count[i][j] += count[i][j+1] if dp[i][j+1] == m<br>count[i][j] += count[i+1][j+1] if dp[i+1][j+1] == m</p>



<p>Time complexity: O(n^2)<br>Space complexity: O(n^2)<br></p>



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

<pre class="crayon-plain-tag">// Author: Huahua
class Solution {
public:
  vector&lt;int&gt; pathsWithMaxScore(vector&lt;string&gt;&amp; board) {
    constexpr int kMod = 1e9 + 7;
    const int n = board.size();
    vector&lt;vector&lt;int&gt;&gt; dp(n + 1, vector&lt;int&gt;(n + 1));
    vector&lt;vector&lt;int&gt;&gt; cc(n + 1, vector&lt;int&gt;(n + 1, 0));
    board[n - 1][n - 1] = board[0][0] = '0';
    cc[n - 1][n - 1] = 1;
    for (int i = n - 1; i &gt;= 0; --i)
      for (int j = n - 1; j &gt;= 0; --j) {
        if (board[i][j] != 'X') {          
          int m = max({dp[i + 1][j], dp[i][j + 1], dp[i + 1][j + 1]});
          dp[i][j] = (board[i][j] - '0') + m;
          if (dp[i + 1][j] == m) 
            cc[i][j] = (cc[i][j] + cc[i + 1][j]) % kMod;
          if (dp[i][j + 1] == m)
            cc[i][j] = (cc[i][j] + cc[i][j + 1]) % kMod;
          if (dp[i + 1][j + 1] == m)
            cc[i][j] = (cc[i][j] + cc[i + 1][j + 1]) % kMod;
        }
      }
    return {cc[0][0] ? dp[0][0] : 0, cc[0][0]};
  }
};</pre>
</div></div>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/dynamic-programming/leetcode-1301-number-of-paths-with-max-score/">花花酱 LeetCode 1301. Number of Paths with Max Score</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-1301-number-of-paths-with-max-score/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>花花酱 LeetCode 130. Surrounded Regions</title>
		<link>https://zxi.mytechroad.com/blog/searching/leetcode-130-surrounded-regions/</link>
					<comments>https://zxi.mytechroad.com/blog/searching/leetcode-130-surrounded-regions/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Thu, 10 Oct 2019 04:44:39 +0000</pubDate>
				<category><![CDATA[Search]]></category>
		<category><![CDATA[board]]></category>
		<category><![CDATA[DFS]]></category>
		<category><![CDATA[medium]]></category>
		<category><![CDATA[O(mn)]]></category>
		<guid isPermaLink="false">https://zxi.mytechroad.com/blog/?p=5743</guid>

					<description><![CDATA[<p>Given a 2D board containing&#160;'X'&#160;and&#160;'O'&#160;(the letter O), capture all regions surrounded by&#160;'X'. A region is captured by flipping all&#160;'O's into&#160;'X's in that surrounded region. Example:&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/searching/leetcode-130-surrounded-regions/">花花酱 LeetCode 130. Surrounded Regions</a> appeared first on <a rel="nofollow" href="https://zxi.mytechroad.com/blog">Huahua&#039;s Tech Road</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<figure class="wp-block-embed-youtube wp-block-embed is-type-video is-provider-youtube wp-embed-aspect-4-3 wp-has-aspect-ratio"><div class="wp-block-embed__wrapper">
<iframe title="花花酱 LeetCode 130. Surrounded Regions - 刷题找工作 EP274" width="500" height="375" src="https://www.youtube.com/embed/kyvGkcXs_rE?feature=oembed" frameborder="0" allow="accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe>
</div></figure>



<p>Given a 2D board containing&nbsp;<code>'X'</code>&nbsp;and&nbsp;<code>'O'</code>&nbsp;(<strong>the letter O</strong>), capture all regions surrounded by&nbsp;<code>'X'</code>.</p>



<p>A region is captured by flipping all&nbsp;<code>'O'</code>s into&nbsp;<code>'X'</code>s in that surrounded region.</p>



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



<pre class="wp-block-preformatted;crayon:false">X X X X
X O O X
X X O X
X O X X
</pre>



<p>After running your function, the board should be:</p>



<pre class="wp-block-preformatted;crayon:false">X X X X
X X X X
X X X X
X O X X
</pre>



<p><strong>Explanation:</strong></p>



<p>Surrounded regions shouldn’t be on the border, which means that any&nbsp;<code>'O'</code>&nbsp;on the border of the board are not flipped to&nbsp;<code>'X'</code>. Any&nbsp;<code>'O'</code>&nbsp;that is not on the border and it is not connected to an&nbsp;<code>'O'</code>&nbsp;on the border will be flipped to&nbsp;<code>'X'</code>. Two cells are connected if they are adjacent cells connected horizontally or vertically.</p>



<h2><strong>Solution: DFS</strong></h2>



<p>Time complexity: O(m*n)<br>Space complexity: O(m*n)</p>



<p>Only starts DFS at border cells of O.</p>



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

<pre class="crayon-plain-tag">// Author: Huahua
class Solution {
public:
  void solve(vector&lt;vector&lt;char&gt;&gt;&amp; board) {
    const int m = board.size();
    if (m == 0) return;
    const int n = board[0].size();    
    
    function&lt;void(int, int)&gt; dfs = [&amp;](int x, int y) {
      if (x &lt; 0 || x &gt;= n || y &lt; 0 || y &gt;= m || board[y][x] != 'O') return;
      board[y][x] = 'G'; // mark it as good      
      dfs(x - 1, y);
      dfs(x + 1, y);
      dfs(x, y - 1);
      dfs(x, y + 1);
    };
    
    for (int y = 0; y &lt; m; ++y)
      dfs(0, y), dfs(n - 1, y);    
    
    for (int x = 0; x &lt; n; ++x)
      dfs(x, 0), dfs(x, m - 1);    
    
    map&lt;char, char&gt; v{{'G','O'},{'O','X'}, {'X','X'}};
    for (int y = 0; y &lt; m; ++y)
      for (int x = 0; x &lt; n; ++x)
        board[y][x] = v[board[y][x]];
  }
};</pre>
</div></div>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/searching/leetcode-130-surrounded-regions/">花花酱 LeetCode 130. Surrounded Regions</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/searching/leetcode-130-surrounded-regions/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>花花酱 LeetCode 1001. Grid Illumination</title>
		<link>https://zxi.mytechroad.com/blog/hashtable/leetcode-1001-grid-illumination/</link>
					<comments>https://zxi.mytechroad.com/blog/hashtable/leetcode-1001-grid-illumination/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Sun, 24 Feb 2019 18:18:24 +0000</pubDate>
				<category><![CDATA[Hashtable]]></category>
		<category><![CDATA[board]]></category>
		<category><![CDATA[hard]]></category>
		<category><![CDATA[hashtable]]></category>
		<category><![CDATA[nqueen]]></category>
		<guid isPermaLink="false">https://zxi.mytechroad.com/blog/?p=4896</guid>

					<description><![CDATA[<p>On a&#160;N x N&#160;grid of cells, each cell&#160;(x, y)&#160;with&#160;0 &#60;= x &#60; N&#160;and&#160;0 &#60;= y &#60; N&#160;has a lamp. Initially, some number of lamps are&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/hashtable/leetcode-1001-grid-illumination/">花花酱 LeetCode 1001. Grid Illumination</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>On a&nbsp;<code>N x N</code>&nbsp;grid of cells, each cell&nbsp;<code>(x, y)</code>&nbsp;with&nbsp;<code>0 &lt;= x &lt; N</code>&nbsp;and&nbsp;<code>0 &lt;= y &lt; N</code>&nbsp;has a lamp.</p>



<p>Initially, some number of lamps are on.&nbsp;&nbsp;<code>lamps[i]</code>&nbsp;tells us the location of the&nbsp;<code>i</code>-th lamp that is on.&nbsp; Each lamp that is on illuminates every square on its x-axis, y-axis, and both diagonals (similar to a Queen in chess).</p>



<p>For the i-th query&nbsp;<code>queries[i] = (x, y)</code>, the answer to the query is 1 if the cell (x, y) is illuminated, else 0.</p>



<p>After each query&nbsp;<code>(x, y)</code>&nbsp;[in the order given by&nbsp;<code>queries</code>], we turn off any&nbsp;lamps that are at cell&nbsp;<code>(x, y)</code>&nbsp;or are adjacent 8-directionally (ie., share a corner or edge with cell&nbsp;<code>(x, y)</code>.)</p>



<p>Return an array of answers.&nbsp; Each&nbsp;value&nbsp;<code>answer[i]</code>&nbsp;should be equal to the answer of the&nbsp;<code>i</code>-th query&nbsp;<code>queries[i]</code>.</p>



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



<pre class="wp-block-preformatted crayon:false"><strong>Input: </strong>N = 5, lamps = [[0,0],[4,4]], queries = [[1,1],[1,0]]
<strong>Output: </strong>[1,0]
<strong>Explanation: </strong>
Before performing the first query we have both lamps [0,0] and [4,4] on.
The grid representing which cells are lit looks like this, where [0,0] is the top left corner, and [4,4] is the bottom right corner:
1 1 1 1 1
1 1 0 0 1
1 0 1 0 1
1 0 0 1 1
1 1 1 1 1
Then the query at [1, 1] returns 1 because the cell is lit.  After this query, the lamp at [0, 0] turns off, and the grid now looks like this:
1 0 0 0 1
0 1 0 0 1
0 0 1 0 1
0 0 0 1 1
1 1 1 1 1
Before performing the second query we have only the lamp [4,4] on.  Now the query at [1,0] returns 0, because the cell is no longer lit.
</pre>



<p><strong>Note:</strong></p>



<ol><li><code>1 &lt;= N &lt;= 10^9</code></li><li><code>0 &lt;= lamps.length &lt;= 20000</code></li><li><code>0 &lt;= queries.length &lt;= 20000</code></li><li><code>lamps[i].length == queries[i].length == 2</code></li></ol>



<h2><strong>Solution: HashTable</strong></h2>



<p>use lx, ly, lp, lq to track the # of lamps that covers each row, col, diagonal, antidiagonal</p>



<p>Time complexity: O(|L| + |Q|)<br>Space complexity: O(|L|)</p>



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

<pre class="crayon-plain-tag">// Author: Huahua, running time: 460 ms, 82.2 MB
class Solution {
public:
  vector&lt;int&gt; gridIllumination(int N, vector&lt;vector&lt;int&gt;&gt;&amp; lamps, vector&lt;vector&lt;int&gt;&gt;&amp; queries) {
    unordered_set&lt;long&gt; s;
    unordered_map&lt;int, int&gt; lx, ly, lp, lq;
    for (const auto&amp; lamp : lamps) {
      const int x = lamp[0];
      const int y = lamp[1];
      s.insert(static_cast&lt;long&gt;(x) &lt;&lt; 32 | y);
      ++lx[x];
      ++ly[y];
      ++lp[x + y];
      ++lq[x - y];
    }
    vector&lt;int&gt; ans;
    for (const auto&amp; query : queries) {
      const int x = query[0];
      const int y = query[1];      
      if (lx.count(x) || ly.count(y) || lp.count(x + y) || lq.count(x - y)) {
        ans.push_back(1);       
        for (int tx = x - 1; tx &lt;= x + 1; ++tx)
          for (int ty = y - 1; ty &lt;= y + 1; ++ty) {
            if (tx &lt; 0 || tx &gt;= N || ty &lt; 0 || ty &gt;= N) continue;
            const long key = static_cast&lt;long&gt;(tx) &lt;&lt; 32 | ty;
            if (!s.count(key)) continue;
            s.erase(key);
            if (--lx[tx] == 0) lx.erase(tx);
            if (--ly[ty] == 0) ly.erase(ty);
            if (--lp[tx + ty] == 0) lp.erase(tx + ty);
            if (--lq[tx - ty] == 0) lq.erase(tx - ty);
          }
      } else {
        ans.push_back(0);
      }
    }
    return ans;
  }
};</pre>

</div><h2 class="tabtitle">C++ v2</h2>
<div class="tabcontent">

<pre class="crayon-plain-tag">// Author: Huahua, running time: 444 ms, 82.2 MB
class Solution {
public:
  vector&lt;int&gt; gridIllumination(int N, vector&lt;vector&lt;int&gt;&gt;&amp; lamps, vector&lt;vector&lt;int&gt;&gt;&amp; queries) {
    unordered_set&lt;long&gt; s;
    unordered_map&lt;int, int&gt; lx, ly, lp, lq;
    for (const auto&amp; lamp : lamps) {
      const int x = lamp[0];
      const int y = lamp[1];
      s.insert(static_cast&lt;long&gt;(x) &lt;&lt; 32 | y);
      ++lx[x];
      ++ly[y];
      ++lp[x + y];
      ++lq[x - y];
    }
    vector&lt;int&gt; ans;
    auto dec = [](unordered_map&lt;int, int&gt;&amp; m, int key) {
      auto it = m.find(key);
      if (it-&gt;second == 1)
        m.erase(it);
      else
        --it-&gt;second;
    };
    for (const auto&amp; query : queries) {
      const int x = query[0];
      const int y = query[1];      
      if (lx.count(x) || ly.count(y) || lp.count(x + y) || lq.count(x - y)) {
        ans.push_back(1);       
        for (int tx = x - 1; tx &lt;= x + 1; ++tx)
          for (int ty = y - 1; ty &lt;= y + 1; ++ty) {
            if (tx &lt; 0 || tx &gt;= N || ty &lt; 0 || ty &gt;= N) continue;
            const long key = static_cast&lt;long&gt;(tx) &lt;&lt; 32 | ty;
            auto it = s.find(key);
            if (it == s.end()) continue;
            s.erase(it);
            dec(lx, tx);
            dec(ly, ty);
            dec(lp, tx + ty);
            dec(lq, tx - ty);            
          }
      } else {
        ans.push_back(0);
      }
    }
    return ans;
  }
};</pre>
</div></div>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/hashtable/leetcode-1001-grid-illumination/">花花酱 LeetCode 1001. Grid Illumination</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/hashtable/leetcode-1001-grid-illumination/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>花花酱 LeetCode 999. Available Captures for Rook</title>
		<link>https://zxi.mytechroad.com/blog/simulation/leetcode-999-available-captures-for-rook/</link>
					<comments>https://zxi.mytechroad.com/blog/simulation/leetcode-999-available-captures-for-rook/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Sun, 24 Feb 2019 17:45:03 +0000</pubDate>
				<category><![CDATA[Simulation]]></category>
		<category><![CDATA[board]]></category>
		<category><![CDATA[chess]]></category>
		<category><![CDATA[easy]]></category>
		<category><![CDATA[simulation]]></category>
		<guid isPermaLink="false">https://zxi.mytechroad.com/blog/?p=4892</guid>

					<description><![CDATA[<p>On an 8 x 8 chessboard, there is one white rook.&#160; There also may be empty squares, white bishops, and black pawns.&#160; These are given&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/simulation/leetcode-999-available-captures-for-rook/">花花酱 LeetCode 999. Available Captures for Rook</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>On an 8 x 8 chessboard, there is one white rook.&nbsp; There also may be empty squares, white bishops, and black pawns.&nbsp; These are given as characters &#8216;R&#8217;, &#8216;.&#8217;, &#8216;B&#8217;, and &#8216;p&#8217; respectively. Uppercase characters represent white pieces, and lowercase characters represent black pieces.</p>



<p>The rook moves as in the rules of Chess: it chooses one of four cardinal directions (north, east, west, and south), then moves in that direction until it chooses to stop, reaches the edge of the board, or captures an opposite colored pawn by moving to the same square it occupies.&nbsp; Also, rooks cannot move into the same square as other friendly bishops.</p>



<p>Return the number of pawns the rook can capture in one move.</p>



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



<figure class="wp-block-image is-resized"><img src="https://assets.leetcode.com/uploads/2019/02/20/1253_example_1_improved.PNG" alt="" width="299" height="303"/></figure>



<pre class="wp-block-preformatted crayon:false"><strong>Input: </strong>[[".",".",".",".",".",".",".","."],[".",".",".","p",".",".",".","."],[".",".",".","R",".",".",".","p"],[".",".",".",".",".",".",".","."],[".",".",".",".",".",".",".","."],[".",".",".","p",".",".",".","."],[".",".",".",".",".",".",".","."],[".",".",".",".",".",".",".","."]]
<strong>Output: </strong>3
<strong>Explanation: </strong>
In this example the rook is able to capture all the pawns.
</pre>



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



<figure class="wp-block-image is-resized"><img src="https://assets.leetcode.com/uploads/2019/02/19/1253_example_2_improved.PNG" alt="" width="298" height="304"/></figure>



<pre class="wp-block-preformatted crayon:false"><strong>Input: </strong>[[".",".",".",".",".",".",".","."],[".","p","p","p","p","p",".","."],[".","p","p","B","p","p",".","."],[".","p","B","R","B","p",".","."],[".","p","p","B","p","p",".","."],[".","p","p","p","p","p",".","."],[".",".",".",".",".",".",".","."],[".",".",".",".",".",".",".","."]]
<strong>Output: </strong>0
<strong>Explanation: </strong>
Bishops are blocking the rook to capture any pawn.
</pre>



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



<figure class="wp-block-image is-resized"><img src="https://assets.leetcode.com/uploads/2019/02/20/1253_example_3_improved.PNG" alt="" width="294" height="299"/></figure>



<pre class="wp-block-preformatted crayon:false"><strong>Input: </strong>[[".",".",".",".",".",".",".","."],[".",".",".","p",".",".",".","."],[".",".",".","p",".",".",".","."],["p","p",".","R",".","p","B","."],[".",".",".",".",".",".",".","."],[".",".",".","B",".",".",".","."],[".",".",".","p",".",".",".","."],[".",".",".",".",".",".",".","."]]
<strong>Output: </strong>3
<strong>Explanation: </strong>
The rook can capture the pawns at positions b5, d6 and f5.
</pre>



<p><strong>Note:</strong></p>



<ol><li><code>board.length == board[i].length == 8</code></li><li><code>board[i][j]</code>&nbsp;is either&nbsp;<code>'R'</code>,&nbsp;<code>'.'</code>,&nbsp;<code>'B'</code>, or&nbsp;<code>'p'</code></li><li>There is exactly one cell with&nbsp;<code>board[i][j] == 'R'</code></li></ol>



<h2><strong>Solution: Simulation</strong></h2>



<p>Time complexity: O(1)<br>Space complexity: O(1)</p>



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

<pre class="crayon-plain-tag">class Solution {
public:
  int numRookCaptures(vector&lt;vector&lt;char&gt;&gt;&amp; board) {        
    int ans = 0;
    auto check = [&amp;board](int x, int y, int dx, int dy) {
      x += dx;
      y += dy;
      while (x &gt;= 0 &amp;&amp; x &lt; 8 &amp;&amp; y &gt;= 0 &amp;&amp; y &lt; 8) {
        if (board[y][x] == 'p') return 1;
        if (board[y][x] != '.') break;
        x += dx;
        y += dy;
      }
      return 0;
    };
    
    array&lt;int, 5&gt; dirs{1, 0, -1, 0, 1};
    for (int i = 0; i &lt; 8; ++i)
      for (int j = 0; j &lt; 8; ++j)
        if (board[i][j] == 'R')
          for (int d = 0; d &lt; 4; ++d)          
            ans += check(j, i, dirs[d], dirs[d + 1]);
    return ans;
  }
};</pre>
</div></div>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/simulation/leetcode-999-available-captures-for-rook/">花花酱 LeetCode 999. Available Captures for Rook</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/simulation/leetcode-999-available-captures-for-rook/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>花花酱 LeetCode 909. Snakes and Ladders</title>
		<link>https://zxi.mytechroad.com/blog/searching/leetcode-909-snakes-and-ladders/</link>
					<comments>https://zxi.mytechroad.com/blog/searching/leetcode-909-snakes-and-ladders/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Wed, 26 Sep 2018 06:21:21 +0000</pubDate>
				<category><![CDATA[Search]]></category>
		<category><![CDATA[BFS]]></category>
		<category><![CDATA[board]]></category>
		<category><![CDATA[medium]]></category>
		<guid isPermaLink="false">https://zxi.mytechroad.com/blog/?p=4076</guid>

					<description><![CDATA[<p>Problem On an N x N board, the numbers from 1 to N*N are written boustrophedonically starting from the bottom left of the board, and alternating direction each row.  For example, for a&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/searching/leetcode-909-snakes-and-ladders/">花花酱 LeetCode 909. Snakes and Ladders</a> appeared first on <a rel="nofollow" href="https://zxi.mytechroad.com/blog">Huahua&#039;s Tech Road</a>.</p>
]]></description>
										<content:encoded><![CDATA[<h1><strong>Problem</strong></h1>
<p>On an N x N <code>board</code>, the numbers from <code>1</code> to <code>N*N</code> are written <em>boustrophedonically</em> <strong>starting from the bottom left of the board</strong>, and alternating direction each row.  For example, for a 6 x 6 board, the numbers are written as follows:</p>
<p><img src="https://assets.leetcode.com/uploads/2018/09/23/snakes.png" alt="" /></p>
<p>You start on square <code>1</code> of the board (which is always in the last row and first column).  Each move, starting from square <code>x</code>, consists of the following:</p>
<ul>
<li>You choose a destination square <code>S</code> with number <code>x+1</code>, <code>x+2</code>, <code>x+3</code>, <code>x+4</code>, <code>x+5</code>, or <code>x+6</code>, provided this number is <code>&lt;= N*N</code>.
<ul>
<li>(This choice simulates the result of a standard 6-sided die roll: ie., there are always at most 6 destinations.)</li>
</ul>
</li>
<li>If <code>S</code> has a snake or ladder, you move to the destination of that snake or ladder.  Otherwise, you move to <code>S</code>.</li>
</ul>
<p>A board square on row <code>r</code> and column <code>c</code> has a &#8220;snake or ladder&#8221; if <code>board[r][c] != -1</code>.  The destination of that snake or ladder is <code>board[r][c]</code>.</p>
<p>Note that you only take a snake or ladder at most once per move: if the destination to a snake or ladder is the start of another snake or ladder, you do <strong>not</strong> continue moving.  (For example, if the board is <code>[[4,-1],[-1,3]]</code>, and on the first move your destination square is <code>2</code>, then you finish your first move at <code>3</code>, because you do <strong>not</strong>continue moving to <code>4</code>.)</p>
<p>Return the least number of moves required to reach square <span style="font-family: monospace;">N*N</span>.  If it is not possible, return <code>-1</code>.</p>
<p><strong>Example 1:</strong></p>
<pre class="crayon:false"><strong>Input: </strong>[
[-1,-1,-1,-1,-1,-1],
[-1,-1,-1,-1,-1,-1],
[-1,-1,-1,-1,-1,-1],
[-1,35,-1,-1,13,-1],
[-1,-1,-1,-1,-1,-1],
[-1,15,-1,-1,-1,-1]]
<strong>Output: </strong>4
<strong>Explanation: </strong>
At the beginning, you start at square 1 [at row 5, column 0].
You decide to move to square 2, and must take the ladder to square 15.
You then decide to move to square 17 (row 3, column 5), and must take the snake to square 13.
You then decide to move to square 14, and must take the ladder to square 35.
You then decide to move to square 36, ending the game.
It can be shown that you need at least 4 moves to reach the N*N-th square, so the answer is 4.
</pre>
<p><strong>Note:</strong></p>
<ol>
<li><code>2 &lt;= board.length = board[0].length &lt;= 20</code></li>
<li><code>board[i][j]</code> is between <code>1</code> and <code>N*N</code> or is equal to <code>-1</code>.</li>
<li>The board square with number <code>1</code> has no snake or ladder.</li>
<li>The board square with number <code>N*N</code> has no snake or ladder.</li>
</ol>
<h1><strong>Solution: BFS</strong></h1>
<p>Time complexity: O(n*n)</p>
<p>Space complexity: O(n*n)</p>
<p><div class="responsive-tabs">
<h2 class="tabtitle">C++</h2>
<div class="tabcontent">
</p><pre class="crayon-plain-tag">// Author: Huahua, 8 ms (beats 100%)
class Solution {
public:
  int snakesAndLadders(vector&lt;vector&lt;int&gt;&gt;&amp; board) {
    const int N = board.size();
    int steps = 0;
    vector&lt;int&gt; seen(N * N + 1, 0);
    queue&lt;int&gt; q;
    q.push(1);
    seen[1] = 1;
    while (!q.empty()) {
      int size = q.size();
      ++steps;
      while (size--) {
        int s = q.front(); q.pop();        
        for (int x = s + 1; x &lt;= min(s + 6, N * N); ++x) {          
          int r, c;
          getRC(x, N, &amp;r, &amp;c);
          int nx = board[r][c] == -1 ? x : board[r][c];          
          if (seen[nx]) continue;
          if (nx == N * N) return steps;
          q.push(nx);
          seen[nx] = 1;
        } 
      }      
    }
    return -1;
  }
private:
  void getRC(int s, int N, int* r, int* c) {
    int y = (s - 1) / N;
    int x = (s - 1) % N;
    *r = N - 1 - y;
    *c = (y % 2) ?  N - 1 - x : x;
  }
};</pre><p></div></div></p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/searching/leetcode-909-snakes-and-ladders/">花花酱 LeetCode 909. Snakes and Ladders</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/searching/leetcode-909-snakes-and-ladders/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>花花酱 Leetcode 419. Battleships in a Board</title>
		<link>https://zxi.mytechroad.com/blog/leetcode/leetcode-battleships-in-a-board/</link>
					<comments>https://zxi.mytechroad.com/blog/leetcode/leetcode-battleships-in-a-board/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Mon, 13 Mar 2017 01:37:35 +0000</pubDate>
				<category><![CDATA[Leetcode]]></category>
		<category><![CDATA[board]]></category>
		<category><![CDATA[counting]]></category>
		<guid isPermaLink="false">http://zxi.mytechroad.com/blog/?p=9</guid>

					<description><![CDATA[<p>[crayon-663c41bb90709399504556/] &#160;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/leetcode/leetcode-battleships-in-a-board/">花花酱 Leetcode 419. Battleships in a Board</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></p><pre class="crayon-plain-tag">class Solution {
public:
    int countBattleships(vector&lt;vector&lt;char&gt;&gt;&amp; board) {
        int count = 0;
        for(int i=0;i&lt;board.size();++i)
            for(int j=0;j&lt;board[i].size();++j) {
                if(board[i][j] == 'X'
                   &amp;&amp; (i==0 || board[i-1][j] == '.')
                   &amp;&amp; (j==0 || board[i][j-1] == '.')) ++count;
            }
        return count;
    }
};</pre><p>&nbsp;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/leetcode/leetcode-battleships-in-a-board/">花花酱 Leetcode 419. Battleships in a Board</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/leetcode/leetcode-battleships-in-a-board/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
