<?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>spiral Archives - Huahua&#039;s Tech Road</title>
	<atom:link href="https://zxi.mytechroad.com/blog/tag/spiral/feed/" rel="self" type="application/rss+xml" />
	<link>https://zxi.mytechroad.com/blog/tag/spiral/</link>
	<description></description>
	<lastBuildDate>Mon, 13 Aug 2018 21:09:10 +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>spiral Archives - Huahua&#039;s Tech Road</title>
	<link>https://zxi.mytechroad.com/blog/tag/spiral/</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>花花酱 LeetCode 889. Spiral Matrix III</title>
		<link>https://zxi.mytechroad.com/blog/simulation/leetcode-889-spiral-matrix-iii/</link>
					<comments>https://zxi.mytechroad.com/blog/simulation/leetcode-889-spiral-matrix-iii/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Mon, 13 Aug 2018 21:05:08 +0000</pubDate>
				<category><![CDATA[Simulation]]></category>
		<category><![CDATA[matrix]]></category>
		<category><![CDATA[medium]]></category>
		<category><![CDATA[simulation]]></category>
		<category><![CDATA[spiral]]></category>
		<guid isPermaLink="false">https://zxi.mytechroad.com/blog/?p=3520</guid>

					<description><![CDATA[<p>Problem On a 2 dimensional grid with R rows and C columns, we start at (r0, c0) facing east. Here, the north-west corner of the grid is at the first row and&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/simulation/leetcode-889-spiral-matrix-iii/">花花酱 LeetCode 889. Spiral Matrix III</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 a 2 dimensional grid with <code>R</code> rows and <code>C</code> columns, we start at <code>(r0, c0)</code> facing east.</p>
<p>Here, the north-west corner of the grid is at the first row and column, and the south-east corner of the grid is at the last row and column.</p>
<p>Now, we walk in a clockwise spiral shape to visit every position in this grid.</p>
<p>Whenever we would move outside the boundary of the grid, we continue our walk outside the grid (but may return to the grid boundary later.)</p>
<p>Eventually, we reach all <code>R * C</code> spaces of the grid.</p>
<p>Return a list of coordinates representing the positions of the grid in the order they were visited.</p>
<p>&nbsp;</p>
<p><strong>Example 1:</strong></p>
<pre class="crayon:false"><strong>Input: </strong>R = <span id="example-input-1-1">1</span>, C = <span id="example-input-1-2">4</span>, r0 = <span id="example-input-1-3">0</span>, c0 = <span id="example-input-1-4">0</span>
<strong>Output: </strong><span id="example-output-1">[[0,0],[0,1],[0,2],[0,3]]</span>

<img src="https://image.ibb.co/b8y6zT/example_1.png" alt="" />
</pre>
<p>&nbsp;</p>
<p><strong>Example 2:</strong></p>
<pre class="crayon:false"><strong>Input: </strong>R = <span id="example-input-2-1">5</span>, C = <span id="example-input-2-2">6</span>, r0 = <span id="example-input-2-3">1</span>, c0 = <span id="example-input-2-4">4</span>
<strong>Output: </strong><span id="example-output-2">[[1,4],[1,5],[2,5],[2,4],[2,3],[1,3],[0,3],[0,4],[0,5],[3,5],[3,4],[3,3],[3,2],[2,2],[1,2],[0,2],[4,5],[4,4],[4,3],[4,2],[4,1],[3,1],[2,1],[1,1],[0,1],[4,0],[3,0],[2,0],[1,0],[0,0]]</span>

<img src="https://image.ibb.co/bGVEm8/example_2.png" alt="" />

</pre>
<div>
<div>
<p><strong>Note:</strong></p>
<ol>
<li><code>1 &lt;= R &lt;= 100</code></li>
<li><code>1 &lt;= C &lt;= 100</code></li>
<li><code>0 &lt;= r0 &lt; R</code></li>
<li><code>0 &lt;= c0 &lt; C</code></li>
</ol>
<h1><strong>Solution: Simulation</strong></h1>
<p>We can find out the moving sequence is ESWWNNEEESSSWWWWNNNN.</p>
<p>The pattern is 1,1,2,2,3,3,4,4,&#8230; steps in one direction, and turn right for 90 degrees.</p>
<p>directions are E,S,W,N,E,S,W,N&#8230;</p>
<p>Time complexity: O(max(R,C)^2)</p>
<p>Space complexity: O(1) or O(RC) if ans included.</p><pre class="crayon-plain-tag">// Author: Huahua
// Running time: 52 ms
class Solution {
public:
  vector&lt;vector&lt;int&gt;&gt; spiralMatrixIII(int R, int C, int r0, int c0) {
    vector&lt;vector&lt;int&gt;&gt; ans;
    int k = 0;
    int dirs[][2]{{1, 0}, {0, 1}, {-1, 0}, {0, -1}}; // ESWN
    int d = 0; // E
    ans.push_back({r0, c0});
    if (ans.size() == R * C) return ans;
    while (++k) {
      for (int i = 0; i &lt; 2; ++i) {
        for (int j = 0; j &lt; k; ++j) {
          c0 += dirs[d][0];
          r0 += dirs[d][1];          
          if (c0 &lt; 0 || c0 &gt;= C || r0 &lt; 0 || r0 &gt;= R) continue;
          ans.push_back({r0, c0});
          if (ans.size() == R * C) return ans;
        }
      d = (d + 1) % 4;
      }
    }
    return {};
  }
};</pre><p>&nbsp;</p>
</div>
</div>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/simulation/leetcode-889-spiral-matrix-iii/">花花酱 LeetCode 889. Spiral Matrix III</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-889-spiral-matrix-iii/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
