File size: 4,316 Bytes
3c88a0f
 
 
 
1
2
3
4
5
{"type": "session", "harness": "opencode", "id": "cp-10036", "name": "LeetCode 0529: Minesweeper"}
{"type": "message", "message": {"role": "system", "content": "You are an expert LeetCode problem solver. You solve problems efficiently with clean code."}}
{"type": "message", "message": {"role": "user", "content": "Problem: Minesweeper (Difficulty: Medium)\nTags: Array, Depth-First Search, Breadth-First Search, Matrix\n\nLet's play the minesweeper game (Wikipedia, online game)!\nYou are given an m x n char matrix board representing the game board where:\nYou are also given an integer array click where click = [clickr, clickc] represents the next click position among all the unrevealed squares ('M' or 'E').\nReturn the board after revealing this position according to the following rules:\n\nExample 1:\nInput: board = [[\"E\",\"E\",\"E\",\"E\",\"E\"],[\"E\",\"E\",\"M\",\"E\",\"E\"],[\"E\",\"E\",\"E\",\"E\",\"E\"],[\"E\",\"E\",\"E\",\"E\",\"E\"]], click = [3,0]\nOutput: [[\"B\",\"1\",\"E\",\"1\",\"B\"],[\"B\",\"1\",\"M\",\"1\",\"B\"],[\"B\",\"1\",\"1\",\"1\",\"B\"],[\"B\",\"B\",\"B\",\"B\",\"B\"]]\n\nExample 2:\nInput: board = [[\"B\",\"1\",\"E\",\"1\",\"B\"],[\"B\",\"1\",\"M\",\"1\",\"B\"],[\"B\",\"1\",\"1\",\"1\",\"B\"],[\"B\",\"B\",\"B\",\"B\",\"B\"]], click = [1,2]\nOutput: [[\"B\",\"1\",\"E\",\"1\",\"B\"],[\"B\",\"1\",\"X\",\"1\",\"B\"],[\"B\",\"1\",\"1\",\"1\",\"B\"],[\"B\",\"B\",\"B\",\"B\",\"B\"]]\n\nConstraints:\n- m == board.length\n- n == board[i].length\n- 1 <= m, n <= 50\n- board[i][j] is either 'M', 'E', 'B', or a digit from '1' to '8'.\n- click.length == 2\n- 0 <= clickr < m\n- 0 <= clickc < n\n- board[clickr][clickc] is either 'M' or 'E'."}}
{"type": "message", "message": {"role": "assistant", "content": "# Time:  O(m * n)\n# Space: O(m + n)\n\n# dfs\nclass Solution(object):\n    def updateBoard(self, board, click):\n        \"\"\"\n        :type board: List[List[str]]\n        :type click: List[int]\n        :rtype: List[List[str]]\n        \"\"\"\n        if board[click[0]][click[1]] == 'M':\n            board[click[0]][click[1]] = 'X'\n            return board\n        stk = [click]\n        while stk:\n            r, c = stk.pop()\n            cnt = 0\n            adj = []\n            for dr in range(-1, 2):\n                for dc in range(-1, 2):\n                    if dr == dc == 0:\n                        continue\n                    nr, nc = r+dr, c+dc\n                    if not (0 <= nr < len(board) and 0 <= nc < len(board[r])):\n                        continue\n                    if board[nr][nc] == 'M':\n                        cnt += 1\n                    elif board[nr][nc] == 'E':\n                        adj.append((nr, nc))\n            if cnt:\n                board[r][c] = chr(cnt + ord('0'))\n                continue\n            board[r][c] = 'B'\n            for nr, nc in adj:\n                board[nr][nc] = ' '\n                stk.append((nr, nc))\n        return board\n\n\n# Time:  O(m * n)\n# Space: O(m + n)\n# dfs\nclass Solution2(object):\n    def updateBoard(self, board, click):\n        \"\"\"\n        :type board: List[List[str]]\n        :type click: List[int]\n        :rtype: List[List[str]]\n        \"\"\"\n        if board[click[0]][click[1]] == 'M':\n            board[click[0]][click[1]] = 'X'\n            return board\n        q = [click]\n        while q:\n            new_q = []\n            for r, c in q:\n                cnt = 0\n                adj = []\n                for dr in range(-1, 2):\n                    for dc in range(-1, 2):\n                        if dr == dc == 0:\n                            continue\n                        nr, nc = r+dr, c+dc\n                        if not (0 <= nr < len(board) and 0 <= nc < len(board[r])):\n                            continue\n                        if board[nr][nc] == 'M':\n                            cnt += 1\n                        elif board[nr][nc] == 'E':\n                            adj.append((nr, nc))\n                if cnt:\n                    board[r][c] = chr(cnt + ord('0'))\n                    continue\n                board[r][c] = 'B'\n                for nr, nc in adj:\n                    board[nr][nc] = ' '\n                    new_q.append((nr, nc))\n            q = new_q\n        return board\n"}}