{"id":961,"date":"2025-12-14T05:22:38","date_gmt":"2025-12-13T21:22:38","guid":{"rendered":"https:\/\/www.guanhaobo.cn\/?p=961"},"modified":"2025-12-14T05:22:38","modified_gmt":"2025-12-13T21:22:38","slug":"leetcode-994-%e8%85%90%e7%83%82%e7%9a%84%e6%a9%98%e5%ad%90","status":"publish","type":"post","link":"https:\/\/www.guanhaobo.cn\/?p=961","title":{"rendered":"LeetCode 994 &#8211; \u8150\u70c2\u7684\u6a58\u5b50"},"content":{"rendered":"<h1>\u9898\u76ee\u63cf\u8ff0<\/h1>\n<p>\u5728\u7ed9\u5b9a\u7684 m x n \u7f51\u683c grid \u4e2d\uff0c\u6bcf\u4e2a\u5355\u5143\u683c\u53ef\u4ee5\u6709\u4ee5\u4e0b\u4e09\u4e2a\u503c\u4e4b\u4e00\uff1a<\/p>\n<p>\u503c 0 \u4ee3\u8868\u7a7a\u5355\u5143\u683c\uff1b<br \/>\n\u503c 1 \u4ee3\u8868\u65b0\u9c9c\u6a58\u5b50\uff1b<br \/>\n\u503c 2 \u4ee3\u8868\u8150\u70c2\u7684\u6a58\u5b50\u3002<br \/>\n\u6bcf\u5206\u949f\uff0c\u8150\u70c2\u7684\u6a58\u5b50 \u5468\u56f4 4 \u4e2a\u65b9\u5411\u4e0a\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 -1 \u3002<\/p>\n<h1>\u9898\u76ee\u5206\u6790<\/h1>\n<p>\u8150\u70c2\u662f\u6309\u65f6\u95f4\u8513\u5ef6\u7684\uff0c\u56e0\u6b64\u4f7f\u7528BFS\u8fdb\u884c\u6a21\u62df\u5373\u53ef\u3002<br \/>\n\u8bb0\u5f55\u6bcf\u4e00\u5c42\u6b21\u7684\u4e2a\u6570\uff0c\u8be5\u5c42\u5168\u90e8\u51fa\u961f\u65f6\uff0c\u65f6\u95f4 + 1\u3002<br \/>\n\u6700\u540e\u68c0\u67e5\u4e00\u4e0b\u662f\u5426\u8fd8\u6709\u65b0\u9c9c\u6a58\u5b50\u3002<\/p>\n<h1>Java<\/h1>\n<pre><code class=\"language-java line-numbers\">private int[][] direction = new int[][]{{1, 0}, {-1, 0}, {0, 1}, {0, -1}};\n\npublic int orangesRotting(int[][] grid) {\n    int m = grid.length;\n    int n = grid[0].length;\n\n    \/\/ \u5b58\u50a8\u8150\u70c2\u6a58\u5b50\n    Queue&lt;int[]&gt; queue = new LinkedList&lt;&gt;();\n    for (int i = 0; i &lt; m; i++) {\n        for (int j = 0; j &lt; n; j++) {\n            if (grid[i][j] == 2) {\n                queue.offer(new int[]{i, j});\n            }\n        }\n    }\n\n    \/\/ \u6a21\u62df\u65f6\u95f4\u6d41\u901d\n    int time = 0;\n    while (!queue.isEmpty()) {\n        int size = queue.size();\n        while (size-- &gt; 0) {\n            int[] node = queue.poll();\n            int i = node[0], j = node[1];\n            for (int index = 0; index &lt; direction.length; index++) {\n                int[] dir = direction[index];\n                int nextI = i + dir[0];\n                int nextJ = j + dir[1];\n                if (isValid(nextI, nextJ, m, n) &amp;&amp; grid[nextI][nextJ] == 1) {\n                    queue.offer(new int[]{nextI, nextJ});\n                    grid[nextI][nextJ] = 2;\n                }\n            }\n        }\n        time++;\n    }\n\n    \/\/ \u68c0\u67e5\u662f\u5426\u6709\u65b0\u9c9c\u6a58\u5b50\n    for (int i = 0; i &lt; m; i++) {\n        for (int j = 0; j &lt; n; j++) {\n            if (grid[i][j] == 1) {\n                return -1;\n            }\n        }\n    }\n    return Math.max(0, time - 1);\n}\n\nprivate boolean isValid(int i, int j, int m, int n) {\n    return i &gt;= 0 &amp;&amp; i &lt; m &amp;&amp; j &gt;= 0 &amp;&amp; j &lt; n;\n}\n<\/code><\/pre>\n<h1>Kotlin<\/h1>\n<pre><code class=\"language-kotlin line-numbers\">private val direction = arrayOf(intArrayOf(0, 1), intArrayOf(0, -1), intArrayOf(1, 0), intArrayOf(-1, 0))\n\nfun orangesRotting(grid: Array&lt;IntArray&gt;): Int {\n    val queue = LinkedList&lt;IntArray&gt;()\n    for (i in grid.indices) {\n        for (j in grid[0].indices) {\n            if (grid[i][j] == 2) {\n                queue.offer(intArrayOf(i, j))\n            }\n        }\n    }\n\n    var time = 0\n    while (!queue.isEmpty()) {\n        var size = queue.size\n        while (size-- &gt; 0) {\n            val node = queue.poll()\n            val i = node[0]\n            val j = node[1]\n            for (dir in direction) {\n                val nextI = i + dir[0]\n                val nextJ = j + dir[1]\n                if (nextI in grid.indices &amp;&amp; nextJ in grid[0].indices &amp;&amp; grid[nextI][nextJ] == 1) {\n                    queue.offer(intArrayOf(nextI, nextJ))\n                    grid[nextI][nextJ] = 2\n                }\n            }\n        }\n        time++\n    }\n\n    for (i in grid.indices) {\n        for (j in grid[0].indices) {\n            if (grid[i][j] == 1) {\n                return -1\n            }\n        }\n    }\n\n    return max(0, time - 1)\n}\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u9898\u76ee\u63cf\u8ff0 \u5728\u7ed9\u5b9a\u7684 m x n \u7f51\u683c grid \u4e2d\uff0c\u6bcf\u4e2a\u5355\u5143\u683c\u53ef\u4ee5\u6709\u4ee5\u4e0b\u4e09\u4e2a\u503c\u4e4b\u4e00\uff1a \u503c 0 \u4ee3\u8868\u7a7a\u5355\u5143\u683c\uff1b  [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5],"tags":[88,20,73,54,83],"class_list":["post-961","post","type-post","status-publish","format-standard","hentry","category-algo","tag-bfs","tag-leetcode","tag-73","tag-54","tag-83"],"_links":{"self":[{"href":"https:\/\/www.guanhaobo.cn\/index.php?rest_route=\/wp\/v2\/posts\/961","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.guanhaobo.cn\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.guanhaobo.cn\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.guanhaobo.cn\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.guanhaobo.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=961"}],"version-history":[{"count":1,"href":"https:\/\/www.guanhaobo.cn\/index.php?rest_route=\/wp\/v2\/posts\/961\/revisions"}],"predecessor-version":[{"id":962,"href":"https:\/\/www.guanhaobo.cn\/index.php?rest_route=\/wp\/v2\/posts\/961\/revisions\/962"}],"wp:attachment":[{"href":"https:\/\/www.guanhaobo.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=961"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.guanhaobo.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=961"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.guanhaobo.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=961"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}