{"id":997,"date":"2017-11-28T07:42:17","date_gmt":"2017-11-28T15:42:17","guid":{"rendered":"http:\/\/zxi.mytechroad.com\/blog\/?p=997"},"modified":"2018-04-19T08:38:05","modified_gmt":"2018-04-19T15:38:05","slug":"leetcode-733-flood-fill","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/searching\/leetcode-733-flood-fill\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 733. Flood Fill"},"content":{"rendered":"<p><iframe loading=\"lazy\" title=\"\u82b1\u82b1\u9171 LeetCode 733. Flood Fill - \u5237\u9898\u627e\u5de5\u4f5c EP116\" width=\"500\" height=\"375\" src=\"https:\/\/www.youtube.com\/embed\/ln_mc5LtL5M?feature=oembed\" frameborder=\"0\" allow=\"accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share\" referrerpolicy=\"strict-origin-when-cross-origin\" allowfullscreen><\/iframe><\/p>\n<p><strong>Problem:<\/strong><\/p>\n<p>An\u00a0<code>image<\/code>\u00a0is represented by a 2-D array of integers, each integer representing the pixel value of the image (from 0 to 65535).<\/p>\n<p>Given a coordinate\u00a0<code>(sr, sc)<\/code>\u00a0representing the starting pixel (row and column) of the flood fill, and a pixel value\u00a0<code>newColor<\/code>, &#8220;flood fill&#8221; the image.<\/p>\n<p>To perform a &#8220;flood fill&#8221;, consider the starting pixel, plus any pixels connected 4-directionally to the starting pixel of the same color as the starting pixel, plus any pixels connected 4-directionally to those pixels (also with the same color as the starting pixel), and so on. Replace the color of all of the aforementioned pixels with the newColor.<\/p>\n<p>At the end, return the modified image.<\/p>\n<p><b>Example 1:<\/b><\/p>\n<pre class=\"theme:github lang:sh highlight:0 decode:true \">Input: \r\nimage = [[1,1,1],[1,1,0],[1,0,1]]\r\nsr = 1, sc = 1, newColor = 2\r\nOutput: [[2,2,2],[2,2,0],[2,0,1]]\r\nExplanation: \r\nFrom the center of the image (with position (sr, sc) = (1, 1)), all pixels connected \r\nby a path of the same color as the starting pixel are colored with the new color.\r\nNote the bottom corner is not colored 2, because it is not 4-directionally connected\r\nto the starting pixel.\r\n<\/pre>\n<p><b>Note:<\/b><\/p>\n<p>&nbsp;<\/p>\n<ul>\n<li>The length of\u00a0<code>image<\/code>\u00a0and\u00a0<code>image[0]<\/code>\u00a0will be in the range\u00a0<code>[1, 50]<\/code>.<\/li>\n<li>The given starting pixel will satisfy\u00a0<code>0 &lt;= sr &lt; image.length<\/code>\u00a0and\u00a0<code>0 &lt;= sc &lt; image[0].length<\/code>.<\/li>\n<li>The value of each color in\u00a0<code>image[i][j]<\/code>\u00a0and\u00a0<code>newColor<\/code>\u00a0will be an integer in\u00a0<code>[0, 65535]<\/code>.<\/li>\n<\/ul>\n<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><br \/>\n<script>\n     (adsbygoogle = window.adsbygoogle || []).push({});\n<\/script><\/p>\n<p><strong>Idea<\/strong><\/p>\n<p>DFS<\/p>\n<p><a href=\"http:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2017\/11\/733-ep116.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-1002\" src=\"http:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2017\/11\/733-ep116.png\" alt=\"\" width=\"960\" height=\"540\" srcset=\"https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2017\/11\/733-ep116.png 960w, https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2017\/11\/733-ep116-300x169.png 300w, https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2017\/11\/733-ep116-768x432.png 768w\" sizes=\"auto, (max-width: 960px) 100vw, 960px\" \/><\/a><\/p>\n<p><strong>Solution:<\/strong><\/p>\n<p>C++ \/ DFS<\/p>\n<p>Time complexity: O(mn)<\/p>\n<p>Space complexity: O(1)<\/p>\n<pre class=\"lang:c++ decode:true\">\/\/ Author: Huahua\r\n\/\/ Runtime: 33 ms\r\nclass Solution {\r\npublic:\r\n    vector&lt;vector&lt;int&gt;&gt; floodFill(vector&lt;vector&lt;int&gt;&gt;&amp; image, \r\n                                  int sr, int sc, int newColor) {\r\n        if (image[sr][sc] == newColor) return image;\r\n        int m = image.size();        \r\n        int n = image[0].size();        \r\n        floodFill(image, sc, sr, n, m, image[sr][sc], newColor);\r\n        return image;\r\n    }\r\nprivate:\r\n    void floodFill(vector&lt;vector&lt;int&gt;&gt;&amp; image, \r\n                   int x, int y, int n, int m, int orgColor, int newColor) {\r\n        if (x &lt; 0 || x &gt;= n || y &lt; 0 || y &gt;= m) return;\r\n        if (image[y][x] != orgColor) return;\r\n        \r\n        image[y][x] = newColor;\r\n        floodFill(image, x + 1, y, n, m, orgColor, newColor);\r\n        floodFill(image, x - 1, y, n, m, orgColor, newColor);\r\n        floodFill(image, x, y + 1, n, m, orgColor, newColor);\r\n        floodFill(image, x, y - 1, n, m, orgColor, newColor);\r\n    }\r\n};<\/pre>\n<p><strong>Related Problems:<\/strong><\/p>\n<ul>\n<li><a href=\"http:\/\/zxi.mytechroad.com\/blog\/searching\/leetcode-200-number-of-islands\/\">[\u89e3\u9898\u62a5\u544a] LeetCode 200. Number of Islands<\/a><\/li>\n<li><a href=\"http:\/\/zxi.mytechroad.com\/blog\/graph\/leetcode-695-max-area-of-island\/\">[\u89e3\u9898\u62a5\u544a] LeetCode 695. Max Area of Island<\/a><\/li>\n<li><a href=\"http:\/\/zxi.mytechroad.com\/blog\/graph\/leetcode-547-friend-circles\/\">[\u89e3\u9898\u62a5\u544a] LeetCode 547. Friend Circles<\/a><\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>Problem: An\u00a0image\u00a0is represented by a 2-D array of integers, each integer representing the pixel value of the image (from 0 to 65535). Given a coordinate\u00a0(sr,&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[163,76,44],"tags":[33],"class_list":["post-997","post","type-post","status-publish","format-standard","hentry","category-easy","category-graph","category-searching","tag-dfs","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/997","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/comments?post=997"}],"version-history":[{"count":8,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/997\/revisions"}],"predecessor-version":[{"id":2659,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/997\/revisions\/2659"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=997"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=997"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=997"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}