{"id":631,"date":"2026-07-12T16:45:30","date_gmt":"2026-07-12T08:45:30","guid":{"rendered":"https:\/\/mitongxue.cn\/?p=631"},"modified":"2026-07-12T16:45:30","modified_gmt":"2026-07-12T08:45:30","slug":"%e8%85%90%e7%83%82%e7%9a%84%e6%a9%98%e5%ad%90","status":"publish","type":"post","link":"https:\/\/mitongxue.cn\/index.php\/2026\/07\/12\/%e8%85%90%e7%83%82%e7%9a%84%e6%a9%98%e5%ad%90\/","title":{"rendered":"\u8150\u70c2\u7684\u6a58\u5b50"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">\u5728\u7ed9\u5b9a\u7684&nbsp;<code>m x n<\/code>&nbsp;\u7f51\u683c&nbsp;<code>grid<\/code>&nbsp;\u4e2d\uff0c\u6bcf\u4e2a\u5355\u5143\u683c\u53ef\u4ee5\u6709\u4ee5\u4e0b\u4e09\u4e2a\u503c\u4e4b\u4e00\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u503c\u00a0<code>0<\/code>\u00a0\u4ee3\u8868\u7a7a\u5355\u5143\u683c\uff1b<\/li>\n\n\n\n<li>\u503c\u00a0<code>1<\/code>\u00a0\u4ee3\u8868\u65b0\u9c9c\u6a58\u5b50\uff1b<\/li>\n\n\n\n<li>\u503c\u00a0<code>2<\/code>\u00a0\u4ee3\u8868\u8150\u70c2\u7684\u6a58\u5b50\u3002<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">\u6bcf\u5206\u949f\uff0c\u8150\u70c2\u7684\u6a58\u5b50&nbsp;<strong>\u5468\u56f4&nbsp;4 \u4e2a\u65b9\u5411\u4e0a\u76f8\u90bb<\/strong>&nbsp;\u7684\u65b0\u9c9c\u6a58\u5b50\u90fd\u4f1a\u8150\u70c2\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u8fd4\u56de&nbsp;<em>\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><\/em>&nbsp;\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u793a\u4f8b 1\uff1a<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/assets.leetcode.cn\/aliyun-lc-upload\/uploads\/2019\/02\/16\/oranges.png\" alt=\"\"\/><\/figure>\n\n\n\n<pre class=\"wp-block-code\"><code><strong>\u8f93\u5165\uff1a<\/strong>grid = &#91;&#91;2,1,1],&#91;1,1,0],&#91;0,1,1]]\n<strong>\u8f93\u51fa\uff1a<\/strong>4\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u793a\u4f8b 2\uff1a<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><strong>\u8f93\u5165\uff1a<\/strong>grid = &#91;&#91;2,1,1],&#91;0,1,1],&#91;1,0,1]]\n<strong>\u8f93\u51fa\uff1a<\/strong>-1\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\u65b9\u5411\u4e0a\u3002\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u793a\u4f8b 3\uff1a<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><strong>\u8f93\u5165\uff1a<\/strong>grid = &#91;&#91;0,2]]\n<strong>\u8f93\u51fa\uff1a<\/strong>0\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<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u63d0\u793a\uff1a<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>m == grid.length<\/code><\/li>\n\n\n\n<li><code>n == grid[i].length<\/code><\/li>\n\n\n\n<li><code>1 &lt;= m, n &lt;= 10<\/code><\/li>\n\n\n\n<li><code>grid[i][j]<\/code>\u00a0\u4ec5\u4e3a\u00a0<code>0<\/code>\u3001<code>1<\/code>\u00a0\u6216\u00a0<code>2<\/code><\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">\u601d\u8def<\/h2>\n\n\n\n<ol class=\"wp-block-list\">\n<li>\u5efa\u7acb\u961f\u5217\uff0c\u628a<strong>\u6240\u6709\u521d\u59cb\u8150\u70c2\u6a58\u5b50\u5750\u6807<\/strong>\u4e00\u6b21\u6027\u5165\u961f\uff08\u591a\u6e90\u8d77\u70b9\uff09\uff1b\u904d\u5386\u8f93\u5165\uff0c\u7edf\u8ba1fresh\u548c\u961f\u5217<\/li>\n<\/ol>\n\n\n\n<p class=\"wp-block-paragraph\">2. \u7edf\u8ba1\u65b0\u9c9c\u6a58\u5b50\u603b\u6570\u91cf <code>fresh<\/code>\uff1b <\/p>\n\n\n\n<p class=\"wp-block-paragraph\">3. \u7279\u6b8a\u63d0\u524d\u5224\u65ad\uff1a<code>fresh === 0<\/code>\uff0c\u65e0\u65b0\u9c9c\u6a58\u5b50\u76f4\u63a5\u8fd4\u56de 0\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">4. minute\u9700\u8981\u51cf1<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/**\n * @param {number&#91;]&#91;]} grid\n * @return {number}\n *\/\nvar orangesRotting = function(grid) {\n    if(!grid.length) return -1;\n    const m = grid.length;\n    const n = grid&#91;0].length;\n    let fresh = 0;\n    const queue = &#91;];\n    for(let i=0; i &lt; m; i++ ){\n        for(let j=0; j&lt;n; j++){\n            if(grid&#91;i]&#91;j]===1){\n                fresh++;\n            }else if(grid&#91;i]&#91;j]===2){\n                queue.push(&#91;i,j]);\n            }\n        }\n    }\n    \/\/console.log(queue,'queue');\n    \/\/console.log(fresh,'fresh');\n    let minute = 0;\n    if(fresh === 0) return 0;\n    const dirt = &#91;&#91;0,1],&#91;0,-1],&#91;-1,0],&#91;1,0]];\n    while(queue.length){\n        const size = queue.length;\n        minute++;\n        for(let i=0;i&lt;size;i++){\n            const &#91;x,y] = queue.shift();\n            for(const &#91;dx,dy] of dirt){\n                const tx = x+dx;\n                const ty = y+dy;\n                if(tx>=0 &amp;&amp; tx &lt;m &amp;&amp; ty >=0 &amp;&amp; ty&lt;n &amp;&amp; grid&#91;tx]&#91;ty]===1){\n                    fresh--;\n                    queue.push(&#91;tx,ty]);\n                    grid&#91;tx]&#91;ty] = 2;\n                }\n            }\n        }\n    }\n    return fresh > 0 ? -1: minute-1;\n};<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5728\u7ed9\u5b9a\u7684&nbsp;m x n&nbsp;\u7f51\u683c&#038;n..<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[16,11],"tags":[],"class_list":["post-631","post","type-post","status-publish","format-standard","hentry","category-js","category-front"],"featured_image_urls":{"full":"","thumbnail":"","medium":"","medium_large":"","large":"","1536x1536":"","2048x2048":""},"author_info":{"info":["\u9648 \u67d0\u4eba"]},"category_info":"<a href=\"https:\/\/mitongxue.cn\/index.php\/category\/front\/js\/\" rel=\"category tag\">JS<\/a> <a href=\"https:\/\/mitongxue.cn\/index.php\/category\/front\/\" rel=\"category tag\">\u524d\u7aef<\/a>","tag_info":"\u524d\u7aef","comment_count":"0","_links":{"self":[{"href":"https:\/\/mitongxue.cn\/index.php\/wp-json\/wp\/v2\/posts\/631","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/mitongxue.cn\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mitongxue.cn\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mitongxue.cn\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/mitongxue.cn\/index.php\/wp-json\/wp\/v2\/comments?post=631"}],"version-history":[{"count":1,"href":"https:\/\/mitongxue.cn\/index.php\/wp-json\/wp\/v2\/posts\/631\/revisions"}],"predecessor-version":[{"id":632,"href":"https:\/\/mitongxue.cn\/index.php\/wp-json\/wp\/v2\/posts\/631\/revisions\/632"}],"wp:attachment":[{"href":"https:\/\/mitongxue.cn\/index.php\/wp-json\/wp\/v2\/media?parent=631"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mitongxue.cn\/index.php\/wp-json\/wp\/v2\/categories?post=631"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mitongxue.cn\/index.php\/wp-json\/wp\/v2\/tags?post=631"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}