<?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>tiling Archives - Huahua&#039;s Tech Road</title>
	<atom:link href="https://zxi.mytechroad.com/blog/tag/tiling/feed/" rel="self" type="application/rss+xml" />
	<link>https://zxi.mytechroad.com/blog/tag/tiling/</link>
	<description></description>
	<lastBuildDate>Tue, 04 Sep 2018 15:31:26 +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>tiling Archives - Huahua&#039;s Tech Road</title>
	<link>https://zxi.mytechroad.com/blog/tag/tiling/</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>花花酱 LeetCode 790. Domino and Tromino Tiling</title>
		<link>https://zxi.mytechroad.com/blog/dynamic-programming/leetcode-790-domino-and-tromino-tiling/</link>
					<comments>https://zxi.mytechroad.com/blog/dynamic-programming/leetcode-790-domino-and-tromino-tiling/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Sun, 25 Feb 2018 04:27:41 +0000</pubDate>
				<category><![CDATA[Dynamic Programming]]></category>
		<category><![CDATA[counting]]></category>
		<category><![CDATA[dp]]></category>
		<category><![CDATA[medium]]></category>
		<category><![CDATA[tiling]]></category>
		<guid isPermaLink="false">http://zxi.mytechroad.com/blog/?p=1871</guid>

					<description><![CDATA[<p>题目大意：有两种不同形状的骨牌(1&#215;2长条形，L型）无限多块。给你一个2xN的板子，问一共有多少不同的方式可以完全覆盖。 We have two types of tiles: a 2&#215;1 domino shape, and an &#8220;L&#8221; tromino shape. These shapes may be rotated. [crayon-663c9904f1bb4360597282/] Given N, how&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/dynamic-programming/leetcode-790-domino-and-tromino-tiling/">花花酱 LeetCode 790. Domino and Tromino Tiling</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><iframe width="500" height="375" src="https://www.youtube.com/embed/S-fUTfqrdq8?feature=oembed" frameborder="0" allow="autoplay; encrypted-media" allowfullscreen></iframe></p>
<p>题目大意：有两种不同形状的骨牌(1&#215;2长条形，L型）无限多块。给你一个2xN的板子，问一共有多少不同的方式可以完全覆盖。</p>
<p>We have two types of tiles: a 2&#215;1 domino shape, and an &#8220;L&#8221; tromino shape. These shapes may be rotated.</p><pre class="crayon-plain-tag">XX  &lt;- domino

XX  &lt;- "L" tromino
X</pre><p>Given N, how many ways are there to tile a 2 x N board? <strong>Return your answer modulo 10^9 + 7</strong>.</p>
<p>(In a tiling, every square must be covered by a tile. Two tilings are different if and only if there are two 4-directionally adjacent cells on the board such that exactly one of the tilings has both squares occupied by a tile.)</p><pre class="crayon-plain-tag">Example:
Input: 3
Output: 5
Explanation: 
The five different ways are listed below, different letters indicates different tiles:
XYZ XXZ XYY XXY XYY
XYZ YYZ XZZ XYY XXY</pre><p><img class="alignnone size-full wp-image-1904" src="http://zxi.mytechroad.com/blog/wp-content/uploads/2018/02/790-ep171.png" alt="" width="960" height="540" srcset="https://zxi.mytechroad.com/blog/wp-content/uploads/2018/02/790-ep171.png 960w, https://zxi.mytechroad.com/blog/wp-content/uploads/2018/02/790-ep171-300x169.png 300w, https://zxi.mytechroad.com/blog/wp-content/uploads/2018/02/790-ep171-768x432.png 768w" sizes="(max-width: 960px) 100vw, 960px" /></p>
<p><img class="alignnone size-full wp-image-1903" src="http://zxi.mytechroad.com/blog/wp-content/uploads/2018/02/790-ep171-2.png" alt="" width="960" height="540" srcset="https://zxi.mytechroad.com/blog/wp-content/uploads/2018/02/790-ep171-2.png 960w, https://zxi.mytechroad.com/blog/wp-content/uploads/2018/02/790-ep171-2-300x169.png 300w, https://zxi.mytechroad.com/blog/wp-content/uploads/2018/02/790-ep171-2-768x432.png 768w" sizes="(max-width: 960px) 100vw, 960px" /></p>
<p><ins class="adsbygoogle" style="display: block; text-align: center;" data-ad-layout="in-article" data-ad-format="fluid" data-ad-client="ca-pub-2404451723245401" data-ad-slot="7983117522"> </ins></p>
<h1><strong>Idea: DP</strong></h1>
<p>dp[i][0]: ways to cover i cols, both rows of i-th col are covered<br />
dp[i][1]:  ways to cover i cols, only top row of i-th col is covered<br />
dp[i][2]:  ways to cover i cols, only bottom row of i-th col is covered</p>
<h1><strong>Solution 1: DP</strong></h1>
<p>Time complexity: O(N)</p>
<p>Space complexity: O(N)</p>
<p><div class="responsive-tabs">
<h2 class="tabtitle">C++</h2>
<div class="tabcontent">
</p><pre class="crayon-plain-tag">// Author: Huahua
// Running time: 4 ms
class Solution {
public:
  int numTilings(int N) {
    constexpr int kMod = 1000000007;
    vector&lt;vector&lt;long&gt;&gt; dp(N + 1, vector&lt;long&gt;(3, 0));    
    dp[0][0] = dp[1][0] = 1;
    for (int i = 2; i &lt;= N; ++i) {
      dp[i][0] = (dp[i - 1][0] + dp[i - 2][0] + dp[i - 1][1] + dp[i - 1][2]) % kMod;
      dp[i][1] = (dp[i - 2][0] + dp[i - 1][2]) % kMod;
      dp[i][2] = (dp[i - 2][0] + dp[i - 1][1]) % kMod;
    }
    
    return dp[N][0];
  }
};</pre><p></div><h2 class="tabtitle">C++ V2</h2>
<div class="tabcontent">
</p><pre class="crayon-plain-tag">Since dp[i][1] always equals to dp[i][2], we can simplify a bit.

// Author: Huahua
// Running time: 4 ms
class Solution {
public:
  int numTilings(int N) {
    constexpr int kMod = 1000000007;
    vector&lt;vector&lt;long&gt;&gt; dp(N + 1, vector&lt;long&gt;(2, 0));    
    dp[0][0] = dp[1][0] = 1;
    for (int i = 2; i &lt;= N; ++i) {
      dp[i][0] = (dp[i - 1][0] + dp[i - 2][0] + 2 * dp[i - 1][1]) % kMod;
      dp[i][1] = (dp[i - 2][0] + dp[i - 1][1]) % kMod;      
    }
    
    return dp[N][0];
  }
};</pre><p></div></div></p>
<h1><strong>Solution 2: DP</strong></h1>
<p>Another way to think about this problem</p>
<p>define: dp[i] ways to completely covert the i*2 board.</p><pre class="crayon-plain-tag">dp[0] = 1 # {}
dp[1] = 1 # {|}
dp[2] = 2 # {||, =}
dp[3] = 5 # {|||, |=, =|,&nbsp;&lfloor;&rceil;,&nbsp;&lceil;&rfloor;} = dp[2] &otimes; {|} + dp[1] &otimes; {=} + dp[0] &otimes; {&lfloor;&rceil;,&nbsp;&lceil;&rfloor;}
dp[4] = 11 # dp[3] &otimes; {|} + dp[2] &otimes; {=} + dp[1] &otimes; {&lfloor;&rceil;,&nbsp;&lceil;&rfloor;} + dp[0] &otimes; {&lfloor;&macr;&rfloor;,&lceil;_&rceil;}
dp[5] = 24 # dp[4] &otimes; {|} + dp[3] &otimes; {=} + 2*(dp[2] + dp[1] + dp[0])
...
dp[n] = dp[n-1] + dp[n-2] + 2*(dp[n-3] + ... + dp[0])
      = dp[n-1] + dp[n-3] + [dp[n-2] + dp[n-3] + 2*(dp[n-4] + ... + dp[0])]
      = dp[n-1] + dp[n-3] + dp[n-1]
      = 2*dp[n-1] + dp[n-3]</pre><p><div class="responsive-tabs">
<h2 class="tabtitle">C++</h2>
<div class="tabcontent">
</p><pre class="crayon-plain-tag">// Author: Huahua
// Running time: 3 ms
class Solution {
public:
  int numTilings(int N) {
    constexpr int kMod = 1000000007;
    vector&lt;long&gt; dp(N + 1, 1);
    dp[2] = 2;
    for (int i = 3; i &lt;= N; ++i)
      dp[i] = (dp[i - 3] + dp[i - 1] * 2) % kMod;
    return dp[N];
  }
};</pre><p></div></div></p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/dynamic-programming/leetcode-790-domino-and-tromino-tiling/">花花酱 LeetCode 790. Domino and Tromino Tiling</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-790-domino-and-tromino-tiling/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
