{"id":543,"date":"2020-09-26T10:53:58","date_gmt":"2020-09-26T02:53:58","guid":{"rendered":"http:\/\/39.96.58.60\/?p=543"},"modified":"2022-10-18T16:38:54","modified_gmt":"2022-10-18T08:38:54","slug":"leetcode-529-%e6%89%ab%e9%9b%b7%e6%b8%b8%e6%88%8f","status":"publish","type":"post","link":"http:\/\/www.yatenglg.cn\/blog\/?p=543","title":{"rendered":"Leetcode 529. \u626b\u96f7\u6e38\u620f"},"content":{"rendered":"<p>\u8ba9\u6211\u4eec\u4e00\u8d77\u6765\u73a9\u626b\u96f7\u6e38\u620f\uff01<\/p>\n<p>\u7ed9\u5b9a\u4e00\u4e2a\u4ee3\u8868\u6e38\u620f\u677f\u7684\u4e8c\u7ef4\u5b57\u7b26\u77e9\u9635\u3002&nbsp;<strong>&#8216;M&#8217;<\/strong>&nbsp;\u4ee3\u8868\u4e00\u4e2a<strong>\u672a\u6316\u51fa\u7684<\/strong>\u5730\u96f7\uff0c<strong>&#8216;E&#8217;<\/strong>&nbsp;\u4ee3\u8868\u4e00\u4e2a<strong>\u672a\u6316\u51fa\u7684<\/strong>\u7a7a\u65b9\u5757\uff0c<strong>&#8216;B&#8217;&nbsp;<\/strong>\u4ee3\u8868\u6ca1\u6709\u76f8\u90bb\uff08\u4e0a\uff0c\u4e0b\uff0c\u5de6\uff0c\u53f3\uff0c\u548c\u6240\u67094\u4e2a\u5bf9\u89d2\u7ebf\uff09\u5730\u96f7\u7684<strong>\u5df2\u6316\u51fa\u7684<\/strong>\u7a7a\u767d\u65b9\u5757\uff0c<strong>\u6570\u5b57<\/strong>\uff08&#8217;1&#8242; \u5230 &#8216;8&#8217;\uff09\u8868\u793a\u6709\u591a\u5c11\u5730\u96f7\u4e0e\u8fd9\u5757<strong>\u5df2\u6316\u51fa\u7684<\/strong>\u65b9\u5757\u76f8\u90bb\uff0c<strong>&#8216;X&#8217;<\/strong>&nbsp;\u5219\u8868\u793a\u4e00\u4e2a<strong>\u5df2\u6316\u51fa\u7684<\/strong>\u5730\u96f7\u3002<\/p>\n<p>\u73b0\u5728\u7ed9\u51fa\u5728\u6240\u6709<strong>\u672a\u6316\u51fa\u7684<\/strong>\u65b9\u5757\u4e2d\uff08&#8217;M&#8217;\u6216\u8005&#8217;E&#8217;\uff09\u7684\u4e0b\u4e00\u4e2a\u70b9\u51fb\u4f4d\u7f6e\uff08\u884c\u548c\u5217\u7d22\u5f15\uff09\uff0c\u6839\u636e\u4ee5\u4e0b\u89c4\u5219\uff0c\u8fd4\u56de\u76f8\u5e94\u4f4d\u7f6e\u88ab\u70b9\u51fb\u540e\u5bf9\u5e94\u7684\u9762\u677f\uff1a<\/p>\n<ol>\n<li>\u5982\u679c\u4e00\u4e2a\u5730\u96f7\uff08&#8217;M&#8217;\uff09\u88ab\u6316\u51fa\uff0c\u6e38\u620f\u5c31\u7ed3\u675f\u4e86- \u628a\u5b83\u6539\u4e3a&nbsp;<strong>&#8216;X&#8217;<\/strong>\u3002<\/li>\n<li>\u5982\u679c\u4e00\u4e2a<strong>\u6ca1\u6709\u76f8\u90bb\u5730\u96f7<\/strong>\u7684\u7a7a\u65b9\u5757\uff08&#8217;E&#8217;\uff09\u88ab\u6316\u51fa\uff0c\u4fee\u6539\u5b83\u4e3a\uff08&#8217;B&#8217;\uff09\uff0c\u5e76\u4e14\u6240\u6709\u548c\u5176\u76f8\u90bb\u7684<strong>\u672a\u6316\u51fa<\/strong>\u65b9\u5757\u90fd\u5e94\u8be5\u88ab\u9012\u5f52\u5730\u63ed\u9732\u3002<\/li>\n<li>\u5982\u679c\u4e00\u4e2a<strong>\u81f3\u5c11\u4e0e\u4e00\u4e2a\u5730\u96f7\u76f8\u90bb<\/strong>\u7684\u7a7a\u65b9\u5757\uff08&#8217;E&#8217;\uff09\u88ab\u6316\u51fa\uff0c\u4fee\u6539\u5b83\u4e3a\u6570\u5b57\uff08&#8217;1&#8217;\u5230&#8217;8&#8217;\uff09\uff0c\u8868\u793a\u76f8\u90bb\u5730\u96f7\u7684\u6570\u91cf\u3002<\/li>\n<li>\u5982\u679c\u5728\u6b64\u6b21\u70b9\u51fb\u4e2d\uff0c\u82e5\u65e0\u66f4\u591a\u65b9\u5757\u53ef\u88ab\u63ed\u9732\uff0c\u5219\u8fd4\u56de\u9762\u677f\u3002<\/li>\n<\/ol>\n<p>&nbsp;<\/p>\n<p><strong>\u793a\u4f8b 1\uff1a<\/strong><\/p>\n<pre><strong>\u8f93\u5165:<\/strong> \n\n\n\n[['E', 'E', 'E', 'E', 'E'],\n\n ['E', 'E', 'M', 'E', 'E'],\n\n ['E', 'E', 'E', 'E', 'E'],\n\n ['E', 'E', 'E', 'E', 'E']]\n\n\n\nClick : [3,0]\n\n\n\n<strong>\u8f93\u51fa:<\/strong> \n\n\n\n[['B', '1', 'E', '1', 'B'],\n\n ['B', '1', 'M', '1', 'B'],\n\n ['B', '1', '1', '1', 'B'],\n\n ['B', 'B', 'B', 'B', 'B']]\n\n\n\n<strong>\u89e3\u91ca:<\/strong>\n\n<img src=\"https:\/\/assets.leetcode-cn.com\/aliyun-lc-upload\/uploads\/2018\/10\/12\/minesweeper_example_1.png\" style=\"width: 100%;\">\n\n<\/pre>\n<p><strong>\u793a\u4f8b 2\uff1a<\/strong><\/p>\n<pre><strong>\u8f93\u5165:<\/strong> \n\n\n\n[['B', '1', 'E', '1', 'B'],\n\n ['B', '1', 'M', '1', 'B'],\n\n ['B', '1', '1', '1', 'B'],\n\n ['B', 'B', 'B', 'B', 'B']]\n\n\n\nClick : [1,2]\n\n\n\n<strong>\u8f93\u51fa:<\/strong> \n\n\n\n[['B', '1', 'E', '1', 'B'],\n\n ['B', '1', 'X', '1', 'B'],\n\n ['B', '1', '1', '1', 'B'],\n\n ['B', 'B', 'B', 'B', 'B']]\n\n\n\n<strong>\u89e3\u91ca:<\/strong>\n\n<img src=\"https:\/\/assets.leetcode-cn.com\/aliyun-lc-upload\/uploads\/2018\/10\/12\/minesweeper_example_2.png\" style=\"width: 100%;\">\n\n<\/pre>\n<p>&nbsp;<\/p>\n<p><strong>\u6ce8\u610f\uff1a<\/strong><\/p>\n<ol>\n<li>\u8f93\u5165\u77e9\u9635\u7684\u5bbd\u548c\u9ad8\u7684\u8303\u56f4\u4e3a [1,50]\u3002<\/li>\n<li>\u70b9\u51fb\u7684\u4f4d\u7f6e\u53ea\u80fd\u662f\u672a\u88ab\u6316\u51fa\u7684\u65b9\u5757 (&#8216;M&#8217; \u6216\u8005 &#8216;E&#8217;)\uff0c\u8fd9\u4e5f\u610f\u5473\u7740\u9762\u677f\u81f3\u5c11\u5305\u542b\u4e00\u4e2a\u53ef\u70b9\u51fb\u7684\u65b9\u5757\u3002<\/li>\n<li>\u8f93\u5165\u9762\u677f\u4e0d\u4f1a\u662f\u6e38\u620f\u7ed3\u675f\u7684\u72b6\u6001\uff08\u5373\u6709\u5730\u96f7\u5df2\u88ab\u6316\u51fa\uff09\u3002<\/li>\n<li>\u7b80\u5355\u8d77\u89c1\uff0c\u672a\u63d0\u53ca\u7684\u89c4\u5219\u5728\u8fd9\u4e2a\u95ee\u9898\u4e2d\u53ef\u88ab\u5ffd\u7565\u3002\u4f8b\u5982\uff0c\u5f53\u6e38\u620f\u7ed3\u675f\u65f6\u4f60\u4e0d\u9700\u8981\u6316\u51fa\u6240\u6709\u5730\u96f7\uff0c\u8003\u8651\u6240\u6709\u4f60\u53ef\u80fd\u8d62\u5f97\u6e38\u620f\u6216\u6807\u8bb0\u65b9\u5757\u7684\u60c5\u51b5\u3002<\/li>\n<\/ol>\n<p>**\u96be\u5ea6**: Medium<\/p>\n<p>**\u6807\u7b7e**: \u6df1\u5ea6\u4f18\u5148\u641c\u7d22\u3001 \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\uff1a248 ms, \u5728\u6240\u6709 Python3 \u63d0\u4ea4\u4e2d\u51fb\u8d25\u4e8636.27% \u7684\u7528\u6237\n\u5185\u5b58\u6d88\u8017\uff1a17.2 MB, \u5728\u6240\u6709 Python3 \u63d0\u4ea4\u4e2d\u51fb\u8d25\u4e8610.36% \u7684\u7528\u6237\n\n\u89e3\u9898\u601d\u8def\uff1a\n    \u6a21\u62df\u70b9\u51fb\n    \u5f53\u53d1\u751f\u70b9\u51fb\u64cd\u4f5c\u65f6\uff0c\u6709\u4e09\u79cd\u60c5\u51b5\uff1a\n        1. \u70b9\u51fb\u5230'M'\uff0c \u4e5f\u5c31\u662f\u70b9\u51fb\u5230\u96f7\uff0c\u6b64\u65f6\uff0c\u628a\u96f7 \u6539\u6210'X'\uff0c \u8fd4\u56de\u5373\u53ef\n        2. \u70b9\u51fb\u5230'E'\uff0c\u4e5f\u5373\u662f\u672a\u70b9\u5230\u96f7\u65f6\uff0c\u9700\u8981\u904d\u5386\u5468\u56f4\u60c5\u51b5\uff1a\n            3. \u662f\u5426\u4e0e\u96f7\u76f8\u90bb\uff0c\uff0c\u5982\u679c\u76f8\u90bb\uff0c\u5219\u628a\u5f53\u524d'E'\uff0c \u6539\u6210\u5468\u56f4\u96f7\u7684\u6570\u91cf\n            4. \u5982\u679c\u4e0d\u76f8\u90bb\uff0c\u5219\u6539\u4e3a'B'\uff0c\u4e14\u5f00\u59cb\u904d\u5386\u5468\u56f4\u5176\u4ed6\u5757\n    \u5177\u4f53\u5b9e\u73b0\u89c1\u4ee3\u7801\u5907\u6ce8\n\"\"\"\nclass Solution:\n    def updateBoard(self, board: List[List[str]], click: List[int]) -&gt; List[List[str]]:\n        m = len(board)\n        n = len(board[0])\n        directions = [[-1, 0], [-1, -1], [0, -1], [1, -1], [1, 0], [1, 1], [0, 1], [-1, 1] ]    # \u516b\u4e2a\u65b9\u4f4d\n\n        def f(i, j):    # \u5b9a\u4e49\u4e00\u4e2a\u70b9\u51fb\u51fd\u6570\uff0c\u7528\u4e8e\u904d\u5386\n            record = [] # \u8bb0\u5f55\u5468\u56f4\u9700\u8981\u70b9\u51fb\u7684'E'\u7684\u5750\u6807\n            mines = 0   # \u8bb0\u5f55\u5f53\u524d\u5750\u6807\u5468\u56f4\u96f7\u7684\u6570\u91cf\n            for direction in directions:    # \u516b\u4e2a\u65b9\u4f4d\uff0c\u6328\u7740\u904d\u5386\n                new_i, new_j = i + direction[0], j+direction[1]\n                if 0 &lt;= new_i &lt; m and 0 &lt;= new_j &lt; n:   # \u5982\u679c\u8d8a\u754c\uff0c\u5219\u5ffd\u7565\n                    if board[new_i][new_j] == 'M':      # \u5468\u56f4\u6709\u96f7\uff0c \u96f7\u6570\u91cf+1\n                        mines += 1\n                    elif board[new_i][new_j] == 'E':    # \u5468\u56f4\u4e0d\u662f\u96f7\uff0c\u5c06\u8fd9\u4e2a\u5750\u6807\u8bb0\u5f55\u4e0b\u6765\n                        record.append([new_i, new_j])\n\n            if mines == 0:  # \u5982\u679c\u96f7\u7684\u6570\u91cf\u662f0\uff0c\u5219\u4e3a'B', \u4e14\u9700\u8981\u904d\u5386\u5468\u56f4\u5176\u4ed6\u4e0d\u662f\u96f7\u7684\u533a\u57df\n                board[i][j] = 'B'\n                for new_i, new_j in record: # \u904d\u5386\u5468\u56f4\u5176\u4ed6\u533a\u57df\n                    f(new_i, new_j)\n            else:\n                board[i][j] = str(mines)    # \u5982\u679c\u5b58\u5728\u96f7\uff0c\u5219\u5c06\u8be5\u5750\u6807 \u6539\u4e3a\u96f7\u7684\u6570\u91cf\n\n        i, j = click\n        if board[i][j] == 'M':  # \u7b2c\u4e00\u6b21\u70b9\u51fb\uff0c\u662f\u96f7\uff0c\u5219\u9700\u8981\u6539\u4e3aX\uff0c\u8fd4\u56de\u5373\u53ef\n            board[i][j] = 'X'\n            return board\n\n        elif board[i][j] == 'E':    # \u4e0d\u662f\u96f7\uff0c\u5219\u9700\u8981\u4ee5\u5f53\u524d\u5750\u6807\u4e3a\u4e2d\u5fc3\u5f00\u59cb\u904d\u5386\u5468\u56f4\u70b9\n            f(i, j)\n\n        return board<\/code><\/pre>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>\u8ba9\u6211\u4eec\u4e00\u8d77\u6765\u73a9\u626b\u96f7\u6e38\u620f\uff01 \u7ed9\u5b9a\u4e00\u4e2a\u4ee3\u8868\u6e38\u620f\u677f\u7684\u4e8c\u7ef4\u5b57\u7b26\u77e9\u9635\u3002&nbsp;&#8216;M&#8217;&#038;nbs&#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\/543"}],"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=543"}],"version-history":[{"count":1,"href":"http:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=\/wp\/v2\/posts\/543\/revisions"}],"predecessor-version":[{"id":544,"href":"http:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=\/wp\/v2\/posts\/543\/revisions\/544"}],"wp:attachment":[{"href":"http:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=543"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=543"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=543"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}