{"id":6447,"date":"2020-03-09T20:18:39","date_gmt":"2020-03-10T03:18:39","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=6447"},"modified":"2020-03-09T20:31:55","modified_gmt":"2020-03-10T03:31:55","slug":"leetcode-529-minesweeper","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/searching\/leetcode-529-minesweeper\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 529. Minesweeper"},"content":{"rendered":"\n<p>Let&#8217;s play the minesweeper game (<a href=\"https:\/\/en.wikipedia.org\/wiki\/Minesweeper_(video_game)\">Wikipedia<\/a>,&nbsp;<a href=\"http:\/\/minesweeperonline.com\/\">online game<\/a>)!<\/p>\n\n\n\n<p>You are given a 2D char matrix representing the game board.&nbsp;<strong>&#8216;M&#8217;<\/strong>&nbsp;represents an&nbsp;<strong>unrevealed<\/strong>&nbsp;mine,&nbsp;<strong>&#8216;E&#8217;<\/strong>&nbsp;represents an&nbsp;<strong>unrevealed<\/strong>&nbsp;empty square,&nbsp;<strong>&#8216;B&#8217;<\/strong>&nbsp;represents a&nbsp;<strong>revealed<\/strong>&nbsp;blank square that has no adjacent (above, below, left, right, and all 4 diagonals) mines,&nbsp;<strong>digit<\/strong>&nbsp;(&#8216;1&#8217; to &#8216;8&#8217;) represents how many mines are adjacent to this&nbsp;<strong>revealed<\/strong>&nbsp;square, and finally&nbsp;<strong>&#8216;X&#8217;<\/strong>&nbsp;represents a&nbsp;<strong>revealed<\/strong>&nbsp;mine.<\/p>\n\n\n\n<p>Now given the next click position (row and column indices) among all the&nbsp;<strong>unrevealed<\/strong>&nbsp;squares (&#8216;M&#8217; or &#8216;E&#8217;), return the board after revealing this position according to the following rules:<\/p>\n\n\n\n<ol class=\"wp-block-list\"><li>If a mine (&#8216;M&#8217;) is revealed, then the game is over &#8211; change it to&nbsp;<strong>&#8216;X&#8217;<\/strong>.<\/li><li>If an empty square (&#8216;E&#8217;) with&nbsp;<strong>no adjacent mines<\/strong>&nbsp;is revealed, then change it to revealed blank (&#8216;B&#8217;) and all of its adjacent&nbsp;<strong>unrevealed<\/strong>&nbsp;squares should be revealed recursively.<\/li><li>If an empty square (&#8216;E&#8217;) with&nbsp;<strong>at least one adjacent mine<\/strong>&nbsp;is revealed, then change it to a digit (&#8216;1&#8217; to &#8216;8&#8217;) representing the number of adjacent mines.<\/li><li>Return the board when no more squares will be revealed.<\/li><\/ol>\n\n\n\n<p><strong>Example 1:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> \n\n[['E', 'E', 'E', 'E', 'E'],\n ['E', 'E', 'M', 'E', 'E'],\n ['E', 'E', 'E', 'E', 'E'],\n ['E', 'E', 'E', 'E', 'E']]\n\nClick : [3,0]\n\n<strong>Output:<\/strong> \n\n[['B', '1', 'E', '1', 'B'],\n ['B', '1', 'M', '1', 'B'],\n ['B', '1', '1', '1', 'B'],\n ['B', 'B', 'B', 'B', 'B']]\n\n<strong>Explanation:<\/strong>\n\n<\/pre>\n\n\n\n<p><strong>Example 2:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> \n\n[['B', '1', 'E', '1', 'B'],\n ['B', '1', 'M', '1', 'B'],\n ['B', '1', '1', '1', 'B'],\n ['B', 'B', 'B', 'B', 'B']]\n\nClick : [1,2]\n\n<strong>Output:<\/strong> \n\n[['B', '1', 'E', '1', 'B'],\n ['B', '1', 'X', '1', 'B'],\n ['B', '1', '1', '1', 'B'],\n ['B', 'B', 'B', 'B', 'B']]\n\n<strong>Explanation:<\/strong>\n\n<\/pre>\n\n\n\n<p><strong>Note:<\/strong><\/p>\n\n\n\n<ol class=\"wp-block-list\"><li>The range of the input matrix&#8217;s height and width is [1,50].<\/li><li>The click position will only be an unrevealed square (&#8216;M&#8217; or &#8216;E&#8217;), which also means the input board contains at least one clickable square.<\/li><li>The input board won&#8217;t be a stage when game is over (some mines have been revealed).<\/li><li>For simplicity, not mentioned rules should be ignored in this problem. For example, you&nbsp;<strong>don&#8217;t<\/strong>&nbsp;need to reveal all the unrevealed mines when the game is over, consider any cases that you will win the game or flag any squares.<\/li><\/ol>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution: DFS<\/strong><\/h2>\n\n\n\n<p>Time complexity: O(m*n)<br>Space complexity: O(m* n)<\/p>\n\n\n\n<div class=\"responsive-tabs\">\n<h2 class=\"tabtitle\">C++<\/h2>\n<div class=\"tabcontent\">\n\n<pre lang=\"C++\">\n\/\/ Author: Huahua\nclass Solution {\npublic:\n  vector<vector<char>> updateBoard(vector<vector<char>>& board, vector<int>& click) {\n    const int m = board.size();\n    const int n = board[0].size();\n    function<void(int, int)> dfs = [&](int x, int y) {\n      if (board[y][x] != 'E') return;\n      int c = 0;\n      for (int tx = x - 1; tx <= x + 1; ++tx)\n        for (int ty = y - 1; ty <= y + 1; ++ty)\n          if (tx >= 0 && tx < n &#038;&#038; ty >= 0 && ty < m)\n            c += board[ty][tx] == 'M';\n      \n      if (c > 0) {\n        board[y][x] = '0' + c;\n        return;      \n      }\n      \n      board[y][x] = 'B';      \n      \n      for (int tx = x - 1; tx <= x + 1; ++tx)\n        for (int ty = y - 1; ty <= y + 1; ++ty)\n          if (tx >= 0 && tx < n &#038;&#038; ty >= 0 && ty < m)\n            dfs(tx, ty);\n    };\n    int x = click[1];\n    int y = click[0];\n    dfs(x, y);\n    if (board[y][x] == 'M') board[y][x] = 'X';\n    return board;\n  }\n};\n<\/pre>\n<\/div><\/div>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution 2: BFS<\/strong><\/h2>\n\n\n\n<div class=\"responsive-tabs\">\n<h2 class=\"tabtitle\">Python3<\/h2>\n<div class=\"tabcontent\">\n\n<pre lang=\"python\">\n# Author: Huahua\nclass Solution:\n  def updateBoard(self, board: List[List[str]], click: List[int]) -> List[List[str]]:\n    m, n = len(board), len(board[0])\n    cx, cy = click[1], click[0] \n    q = [(cx, cy)]\n    while q:\n      q2 = []\n      for x, y in q:        \n        if board[y][x] != 'E': continue\n        c = 0\n        for tx in range(x - 1, x + 2):\n          for ty in range(y - 1, y + 2):\n            if 0 <= tx < n and 0 <= ty < m and board[ty][tx] == 'M':\n              c += 1\n        if c > 0:\n          board[y][x] = chr(ord('0') + c)\n          continue\n        board[y][x] = 'B'\n        for tx in range(x - 1, x + 2):\n          for ty in range(y - 1, y + 2):\n            if 0 <= tx < n and 0 <= ty < m and board[ty][tx] == 'E':\n              q2.append((tx, ty))              \n      q = q2\n    if board[cy][cx] == 'M': board[y][x] = 'X'\n    return board\n<\/pre>\n<\/div><\/div>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Let&#8217;s play the minesweeper game (Wikipedia,&nbsp;online game)! You are given a 2D char matrix representing the game board.&nbsp;&#8216;M&#8217;&nbsp;represents an&nbsp;unrevealed&nbsp;mine,&nbsp;&#8216;E&#8217;&nbsp;represents an&nbsp;unrevealed&nbsp;empty square,&nbsp;&#8216;B&#8217;&nbsp;represents a&nbsp;revealed&nbsp;blank square that has&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[44],"tags":[33,278,177],"class_list":["post-6447","post","type-post","status-publish","format-standard","hentry","category-searching","tag-dfs","tag-grid","tag-medium","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/6447","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=6447"}],"version-history":[{"count":2,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/6447\/revisions"}],"predecessor-version":[{"id":6449,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/6447\/revisions\/6449"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=6447"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=6447"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=6447"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}