{"id":659,"date":"2020-09-26T11:08:11","date_gmt":"2020-09-26T03:08:11","guid":{"rendered":"http:\/\/39.96.58.60\/?p=659"},"modified":"2022-10-18T16:38:24","modified_gmt":"2022-10-18T08:38:24","slug":"leetcode-994-%e8%85%90%e7%83%82%e7%9a%84%e6%a9%98%e5%ad%90","status":"publish","type":"post","link":"http:\/\/www.yatenglg.cn\/blog\/?p=659","title":{"rendered":"Leetcode 994. \u8150\u70c2\u7684\u6a58\u5b50"},"content":{"rendered":"<p>\u5728\u7ed9\u5b9a\u7684\u7f51\u683c\u4e2d\uff0c\u6bcf\u4e2a\u5355\u5143\u683c\u53ef\u4ee5\u6709\u4ee5\u4e0b\u4e09\u4e2a\u503c\u4e4b\u4e00\uff1a<\/p>\n<ul>\n<li>\u503c&nbsp;<code>0<\/code>&nbsp;\u4ee3\u8868\u7a7a\u5355\u5143\u683c\uff1b<\/li>\n<li>\u503c&nbsp;<code>1<\/code>&nbsp;\u4ee3\u8868\u65b0\u9c9c\u6a58\u5b50\uff1b<\/li>\n<li>\u503c&nbsp;<code>2<\/code>&nbsp;\u4ee3\u8868\u8150\u70c2\u7684\u6a58\u5b50\u3002<\/li>\n<\/ul>\n<p>\u6bcf\u5206\u949f\uff0c\u4efb\u4f55\u4e0e\u8150\u70c2\u7684\u6a58\u5b50\uff08\u5728 4 \u4e2a\u6b63\u65b9\u5411\u4e0a\uff09\u76f8\u90bb\u7684\u65b0\u9c9c\u6a58\u5b50\u90fd\u4f1a\u8150\u70c2\u3002<\/p>\n<p>\u8fd4\u56de\u76f4\u5230\u5355\u5143\u683c\u4e2d\u6ca1\u6709\u65b0\u9c9c\u6a58\u5b50\u4e3a\u6b62\u6240\u5fc5\u987b\u7ecf\u8fc7\u7684\u6700\u5c0f\u5206\u949f\u6570\u3002\u5982\u679c\u4e0d\u53ef\u80fd\uff0c\u8fd4\u56de&nbsp;<code>-1<\/code>\u3002<\/p>\n<p><strong>\u793a\u4f8b 1\uff1a<\/strong><\/p>\n<p><strong><img alt=\"\" src=\"https:\/\/assets.leetcode-cn.com\/aliyun-lc-upload\/uploads\/2019\/02\/16\/oranges.png\" style=\"height: 150px; width: 712px;\"><\/strong><\/p>\n<pre><strong>\u8f93\u5165\uff1a<\/strong>[[2,1,1],[1,1,0],[0,1,1]]\n\n<strong>\u8f93\u51fa\uff1a<\/strong>4\n\n<\/pre>\n<p><strong>\u793a\u4f8b 2\uff1a<\/strong><\/p>\n<pre><strong>\u8f93\u5165\uff1a<\/strong>[[2,1,1],[0,1,1],[1,0,1]]\n\n<strong>\u8f93\u51fa\uff1a<\/strong>-1\n\n<strong>\u89e3\u91ca\uff1a<\/strong>\u5de6\u4e0b\u89d2\u7684\u6a58\u5b50\uff08\u7b2c 2 \u884c\uff0c \u7b2c 0 \u5217\uff09\u6c38\u8fdc\u4e0d\u4f1a\u8150\u70c2\uff0c\u56e0\u4e3a\u8150\u70c2\u53ea\u4f1a\u53d1\u751f\u5728 4 \u4e2a\u6b63\u5411\u4e0a\u3002\n\n<\/pre>\n<p><strong>\u793a\u4f8b 3\uff1a<\/strong><\/p>\n<pre><strong>\u8f93\u5165\uff1a<\/strong>[[0,2]]\n\n<strong>\u8f93\u51fa\uff1a<\/strong>0\n\n<strong>\u89e3\u91ca\uff1a<\/strong>\u56e0\u4e3a 0 \u5206\u949f\u65f6\u5df2\u7ecf\u6ca1\u6709\u65b0\u9c9c\u6a58\u5b50\u4e86\uff0c\u6240\u4ee5\u7b54\u6848\u5c31\u662f 0 \u3002\n\n<\/pre>\n<p><strong>\u63d0\u793a\uff1a<\/strong><\/p>\n<ol>\n<li><code>1 &lt;= grid.length &lt;= 10<\/code><\/li>\n<li><code>1 &lt;= grid[0].length &lt;= 10<\/code><\/li>\n<li><code>grid[i][j]<\/code> \u4ec5\u4e3a&nbsp;<code>0<\/code>\u3001<code>1<\/code>&nbsp;\u6216&nbsp;<code>2<\/code><\/li>\n<\/ol>\n<p>**\u96be\u5ea6**: Medium<\/p>\n<p>**\u6807\u7b7e**: \u5e7f\u5ea6\u4f18\u5148\u641c\u7d22\u3001<\/p>\n<div class=\"hcb_wrap\">\n<pre class=\"prism undefined-numbers lang-python\" data-lang=\"Python\"><code>\n# -*- coding: utf-8 -*-\n# @Author  : LG\n\n\"\"\"\n\u6267\u884c\u7528\u65f6\uff1a56 ms, \u5728\u6240\u6709 Python3 \u63d0\u4ea4\u4e2d\u51fb\u8d25\u4e8693.91% \u7684\u7528\u6237\n\u5185\u5b58\u6d88\u8017\uff1a13.3 MB, \u5728\u6240\u6709 Python3 \u63d0\u4ea4\u4e2d\u51fb\u8d25\u4e8686.67% \u7684\u7528\u6237\n\n\u89e3\u9898\u601d\u8def\uff1a\n    \u5177\u4f53\u5b9e\u73b0\u89c1\u4ee3\u7801\u6ce8\u91ca\n\"\"\"\nclass Solution:\n    def orangesRotting(self, grid: List[List[int]]) -&gt; int:\n        roted = []  # \u4fdd\u5b58\u574f\u6a58\u5b50\u7684\u5750\u6807\n        empty = []  # \u4fdd\u5b58\u7a7a\u5750\u6807\n        fresh = []  # \u4fdd\u5b58\u65b0\u9c9c\u7684\u6a58\u5b50\u7684\u5750\u6807\n        h, w = len(grid), len(grid[0])\n        directions = [(0, -1), (0, 1), (-1, 0), (1, 0)] # \u4e0a\u4e0b\u5de6\u53f3\u56db\u4e2a\u79fb\u52a8\u65b9\u5411\n\n        # \u904d\u5386\u683c\u5b50\uff0c\u8bb0\u5f55\u574f\u6a58\u5b50\uff0c\u7a7a\u683c\u5b50\uff0c\u65b0\u9c9c\u6a58\u5b50\u7684\u5750\u6807\n        for i in range(h):\n            for j in range(w):\n                if grid[i][j] == 2:\n                    roted.append((i, j))\n                elif grid[i][j] == 1:\n                    fresh.append((i, j))\n                elif grid[i][j] == 0:\n                    empty.append((i, j))\n\n        if roted == []: # \u5982\u679c\u6ca1\u6709\u574f\u6a58\u5b50\n            if fresh:   # \u5982\u679c\u6709\u65b0\u9c9c\u7684\uff0c\u5219\u8fd4\u56de-1\uff08\u56e0\u4e3a\u6ca1\u6709\u574f\u6a58\u5b50\uff0c\u4e0d\u4f1a\u611f\u67d3\uff09\n                return -1\n            else:   # \u5426\u5219\uff0c\u6ca1\u6709\u65b0\u9c9c\u6a58\u5b50\uff0c\u8fd4\u56de0\n                return 0\n\n        num = 0 # \u7528\u4e8e\u8bb0\u5f55\u65f6\u95f4\n        while roted:\n            num += 1\n            new_roted = []  # \u8bb0\u5f55\u65b0\u8150\u70c2\u7684\u6a58\u5b50\u5750\u6807\n            for rot in roted:   # \u4ece\u5df2\u7ecf\u8150\u70c2\u7684\u6a58\u5b50\u5750\u6807\u5f00\u59cb\n                for d in directions:    # \u67e5\u770b\u56db\u4e2a\u65b9\u5411\n                    new_i = rot[0] + d[0]\n                    new_j = rot[1] + d[1]\n                    if (new_i, new_j) in fresh: # \u5982\u679c\u5f53\u524d\u5750\u6807\u5b58\u5728\u597d\u6a58\u5b50\n                        fresh.remove((new_i, new_j))    # \u5219\u4ece\u597d\u6a58\u5b50\u5217\u8868\u4e2d\u5220\u9664\uff0c\u5f53\u524d\u5750\u6807\n                        new_roted.append((new_i, new_j))    # \u5728\u65b0\u8150\u70c2\u7684\u6a58\u5b50\u5217\u8868\u4e2d\u8bb0\u5f55\u5f53\u524d\u5750\u6807\n\n            roted = new_roted[:]    # \u66f4\u65b0\u574f\u6a58\u5b50\u5217\u8868\uff0c\u4e0b\u6b21\u4ece\u65b0\u8150\u70c2\u7684\u5f00\u59cb\n        if fresh:   # \u5982\u679c\u611f\u67d3\u5b8c\u540e\uff0c\u5b58\u5728\u65b0\u9c9c\u6a58\u5b50\uff0c\u5219\u8fd4\u56de-1\n            return -1\n        else:       # \u5426\u5219\uff0c \u8fd4\u56de0\n            return num-1<\/code><\/pre>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>\u5728\u7ed9\u5b9a\u7684\u7f51\u683c\u4e2d\uff0c\u6bcf\u4e2a\u5355\u5143\u683c\u53ef\u4ee5\u6709\u4ee5\u4e0b\u4e09\u4e2a\u503c\u4e4b\u4e00\uff1a \u503c&nbsp;0&nbsp;\u4ee3\u8868\u7a7a\u5355\u5143\u683c\uff1b \u503c&nbsp;1&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":[],"categories":[11,1],"tags":[],"_links":{"self":[{"href":"http:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=\/wp\/v2\/posts\/659"}],"collection":[{"href":"http:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=659"}],"version-history":[{"count":3,"href":"http:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=\/wp\/v2\/posts\/659\/revisions"}],"predecessor-version":[{"id":757,"href":"http:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=\/wp\/v2\/posts\/659\/revisions\/757"}],"wp:attachment":[{"href":"http:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=659"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=659"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=659"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}