<?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>pruning Archives - Huahua&#039;s Tech Road</title>
	<atom:link href="https://zxi.mytechroad.com/blog/tag/pruning/feed/" rel="self" type="application/rss+xml" />
	<link>https://zxi.mytechroad.com/blog/tag/pruning/</link>
	<description></description>
	<lastBuildDate>Thu, 19 Apr 2018 15:39:12 +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>pruning Archives - Huahua&#039;s Tech Road</title>
	<link>https://zxi.mytechroad.com/blog/tag/pruning/</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>花花酱 LeetCode 37. Sudoku Solver</title>
		<link>https://zxi.mytechroad.com/blog/searching/leetcode-37-sudoku-solver/</link>
					<comments>https://zxi.mytechroad.com/blog/searching/leetcode-37-sudoku-solver/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Tue, 17 Oct 2017 02:56:40 +0000</pubDate>
				<category><![CDATA[Search]]></category>
		<category><![CDATA[DFS]]></category>
		<category><![CDATA[pruning]]></category>
		<guid isPermaLink="false">http://zxi.mytechroad.com/blog/?p=612</guid>

					<description><![CDATA[<p>Problem: Write a program to solve a Sudoku puzzle by filling the empty cells. Empty cells are indicated by the character '.'. You may assume that&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/searching/leetcode-37-sudoku-solver/">花花酱 LeetCode 37. Sudoku Solver</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/ucugbKwjtRs?feature=oembed" frameborder="0" gesture="media" allowfullscreen></iframe></p>
<p><strong>Problem:</strong></p>
<p>Write a program to solve a Sudoku puzzle by filling the empty cells.</p>
<p>Empty cells are indicated by the character <code>'.'</code>.</p>
<p>You may assume that there will be only one unique solution.</p>
<p><img src="https://upload.wikimedia.org/wikipedia/commons/thumb/f/ff/Sudoku-by-L2G-20050714.svg/250px-Sudoku-by-L2G-20050714.svg.png" /></p>
<p>A sudoku puzzle&#8230;</p>
<p><img src="https://upload.wikimedia.org/wikipedia/commons/thumb/3/31/Sudoku-by-L2G-20050714_solution.svg/250px-Sudoku-by-L2G-20050714_solution.svg.png" /></p>
<p>&#8230;and its solution numbers marked in red.</p>
<p><strong>Idea:</strong></p>
<p>DFS + backtracking</p>
<p><a href="http://zxi.mytechroad.com/blog/wp-content/uploads/2017/10/37-ep89.png"><img class="alignnone size-full wp-image-619" src="http://zxi.mytechroad.com/blog/wp-content/uploads/2017/10/37-ep89.png" alt="" width="960" height="540" srcset="https://zxi.mytechroad.com/blog/wp-content/uploads/2017/10/37-ep89.png 960w, https://zxi.mytechroad.com/blog/wp-content/uploads/2017/10/37-ep89-300x169.png 300w, https://zxi.mytechroad.com/blog/wp-content/uploads/2017/10/37-ep89-768x432.png 768w, https://zxi.mytechroad.com/blog/wp-content/uploads/2017/10/37-ep89-624x351.png 624w" sizes="(max-width: 960px) 100vw, 960px" /></a></p>
<p><strong>Solution:</strong></p>
<p>C++</p><pre class="crayon-plain-tag">// Author: Huahua
// Runtime: 3 ms
class Solution {
public:
    void solveSudoku(vector&lt;vector&lt;char&gt;&gt;&amp; board) {
        rows_ = vector&lt;vector&lt;int&gt;&gt;(9, vector&lt;int&gt;(10));
        cols_ = vector&lt;vector&lt;int&gt;&gt;(9, vector&lt;int&gt;(10));
        boxes_ = vector&lt;vector&lt;int&gt;&gt;(9, vector&lt;int&gt;(10));
        
        for(int i = 0; i &lt; 9; i++)
            for(int j = 0; j &lt; 9; j++) {
                const char c = board[i][j];                
                if ( c != '.') {
                    int n = c - '0';                    
                    int bx = j / 3;
                    int by = i / 3;
                    rows_[i][n] = 1;
                    cols_[j][n] = 1;
                    boxes_[by * 3 + bx][n] = 1;
                }
            }
        
        fill(board, 0, 0);
    }
    
private:
    vector&lt;vector&lt;int&gt;&gt; rows_, cols_, boxes_;
    
    bool fill(vector&lt;vector&lt;char&gt;&gt;&amp; board, int x, int y) {
        if (y == 9)
            return true;
        
        int nx = (x + 1) % 9;
        int ny = (nx == 0) ? y + 1 : y;
        
        if (board[y][x] != '.') return fill(board, nx, ny);
        
        for (int i = 1; i &lt;= 9; i++) {
            int bx = x / 3;
            int by = y / 3;
            int box_key = by * 3 + bx;
            if (!rows_[y][i] &amp;&amp; !cols_[x][i] &amp;&amp; !boxes_[box_key][i]) {
                rows_[y][i] = 1;
                cols_[x][i] = 1;
                boxes_[box_key][i] = 1;
                board[y][x] = i + '0';
                if (fill(board, nx, ny)) return true;
                board[y][x] = '.';
                boxes_[box_key][i] = 0;
                cols_[x][i] = 0;
                rows_[y][i] = 0;
            }
        }
        return false;
    }
};</pre><p>&nbsp;</p>
<p><strong>Related Problems:</strong></p>
<ul>
<li><a href="http://zxi.mytechroad.com/blog/searching/leetcode-51-n-queens/">[解题报告] LeetCode 51. N-Queens</a></li>
</ul>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/searching/leetcode-37-sudoku-solver/">花花酱 LeetCode 37. Sudoku Solver</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-37-sudoku-solver/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>花花酱 LeetCode 40. Combination Sum II</title>
		<link>https://zxi.mytechroad.com/blog/searching/leetcode-40-combination-sum-ii/</link>
					<comments>https://zxi.mytechroad.com/blog/searching/leetcode-40-combination-sum-ii/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Mon, 16 Oct 2017 03:45:51 +0000</pubDate>
				<category><![CDATA[Search]]></category>
		<category><![CDATA[combination]]></category>
		<category><![CDATA[pruning]]></category>
		<category><![CDATA[sum]]></category>
		<guid isPermaLink="false">http://zxi.mytechroad.com/blog/?p=607</guid>

					<description><![CDATA[<p>Problem: Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in C where the candidate numbers sums to T. Each number&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/searching/leetcode-40-combination-sum-ii/">花花酱 LeetCode 40. Combination Sum 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><iframe width="500" height="375" src="https://www.youtube.com/embed/RSatA4uVBDQ?feature=oembed" frameborder="0" gesture="media" allowfullscreen></iframe></p>
<p><strong>Problem:</strong></p>
<div class="question-description">
<p>Given a collection of candidate numbers (<b><i>C</i></b>) and a target number (<b><i>T</i></b>), find all unique combinations in <b><i>C</i></b> where the candidate numbers sums to <b><i>T</i></b>.</p>
<p>Each number in <b><i>C</i></b> may only be used <b>once</b> in the combination.</p>
<p><b>Note:</b></p>
<ul>
<li>All numbers (including target) will be positive integers.</li>
<li>The solution set must not contain duplicate combinations.</li>
</ul>
<p>For example, given candidate set <code>[10, 1, 2, 7, 6, 1, 5]</code> and target <code>8</code>,<br />
A solution set is:</p><pre class="crayon-plain-tag">[
  [1, 7],
  [1, 2, 5],
  [2, 6],
  [1, 1, 6]
]</pre><p>
</div>
<p>&nbsp;</p>
<p><script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script><br />
<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><br />
<script>
     (adsbygoogle = window.adsbygoogle || []).push({});
</script></p>
<p><strong>Idea:</strong></p>
<p>DFS</p>
<p><a href="http://zxi.mytechroad.com/blog/wp-content/uploads/2017/10/40-ep88.png"><img class="alignnone size-full wp-image-610" src="http://zxi.mytechroad.com/blog/wp-content/uploads/2017/10/40-ep88.png" alt="" width="960" height="540" srcset="https://zxi.mytechroad.com/blog/wp-content/uploads/2017/10/40-ep88.png 960w, https://zxi.mytechroad.com/blog/wp-content/uploads/2017/10/40-ep88-300x169.png 300w, https://zxi.mytechroad.com/blog/wp-content/uploads/2017/10/40-ep88-768x432.png 768w, https://zxi.mytechroad.com/blog/wp-content/uploads/2017/10/40-ep88-624x351.png 624w" sizes="(max-width: 960px) 100vw, 960px" /></a></p>
<p><strong>Solution:</strong></p>
<p>C++ / set</p><pre class="crayon-plain-tag">// Author: Huahua
// Runtime: 9 ms
class Solution {
public:
    vector&lt;vector&lt;int&gt;&gt; combinationSum2(vector&lt;int&gt;&amp; candidates, int target) {
        set&lt;vector&lt;int&gt;&gt; ans;
        std::sort(candidates.begin(), candidates.end());
        vector&lt;int&gt; curr;
        dfs(candidates, target, 0, ans, curr);
        return vector&lt;vector&lt;int&gt;&gt;(ans.begin(), ans.end());
    }
private:
    void dfs(const vector&lt;int&gt;&amp; candidates, 
             int target, int s, 
             set&lt;vector&lt;int&gt;&gt;&amp; ans,              
             vector&lt;int&gt;&amp; curr) {
        if (target == 0) {
            ans.insert(curr);
            return;
        }
        
        for (int i = s; i &lt; candidates.size(); ++i) {
            int num = candidates[i];
            if (num &gt; target) return;
            curr.push_back(num);
            dfs(candidates, target - num, i + 1, ans, curr);
            curr.pop_back();
        }
    }
};</pre><p>&nbsp;</p>
<p>C++ / vector</p><pre class="crayon-plain-tag">// Author: Huahua
// Runtime: 9ms

class Solution {
public:
    vector&lt;vector&lt;int&gt;&gt; combinationSum2(vector&lt;int&gt;&amp; candidates, int target) {
        vector&lt;vector&lt;int&gt;&gt; ans;
        std::sort(candidates.begin(), candidates.end());
        vector&lt;int&gt; curr;
        dfs(candidates, target, 0, ans, curr);
        return ans;
    }
private:
    void dfs(const vector&lt;int&gt;&amp; candidates, 
             int target, int s, 
             vector&lt;vector&lt;int&gt;&gt;&amp; ans,              
             vector&lt;int&gt;&amp; curr) {
        if (target == 0) {
            ans.push_back(curr);
            return;
        }
        
        for (int i = s; i &lt; candidates.size(); ++i) {
            int num = candidates[i];
            if (num &gt; target) return;
            if (i &gt; s &amp;&amp; candidates[i] == candidates[i - 1]) continue;
            curr.push_back(num);
            dfs(candidates, target - num, i + 1, ans, curr);
            curr.pop_back();
        }
    }
};</pre><p>&nbsp;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/searching/leetcode-40-combination-sum-ii/">花花酱 LeetCode 40. Combination Sum 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/searching/leetcode-40-combination-sum-ii/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
