LeetCode 529. Minesweeper
原题链接在这里:https://leetcode.com/problems/minesweeper/description/
题目:
Let's play the minesweeper game (Wikipedia, online game)!
You are given a 2D char matrix representing the game board. 'M' represents an unrevealed mine, 'E' represents an unrevealed empty square, 'B' represents a revealed blank square that has no adjacent (above, below, left, right, and all 4 diagonals) mines, digit ('1' to '8') represents how many mines are adjacent to this revealed square, and finally 'X' represents a revealed mine.
Now given the next click position (row and column indices) among all the unrevealed squares ('M' or 'E'), return the board after revealing this position according to the following rules:
- If a mine ('M') is revealed, then the game is over - change it to 'X'.
- If an empty square ('E') with no adjacent mines is revealed, then change it to revealed blank ('B') and all of its adjacent unrevealed squares should be revealed recursively.
- If an empty square ('E') with at least one adjacent mine is revealed, then change it to a digit ('1' to '8') representing the number of adjacent mines.
- Return the board when no more squares will be revealed.
Example 1:
Input: [['E', 'E', 'E', 'E', 'E'], ['E', 'E', 'M', 'E', 'E'], ['E', 'E', 'E', 'E', 'E'], ['E', 'E', 'E', 'E', 'E']] Click : [3,0] Output: [['B', '1', 'E', '1', 'B'], ['B', '1', 'M', '1', 'B'], ['B', '1', '1', '1', 'B'], ['B', 'B', 'B', 'B', 'B']] Explanation:
Example 2:
Input: [['B', '1', 'E', '1', 'B'], ['B', '1', 'M', '1', 'B'], ['B', '1', '1', '1', 'B'], ['B', 'B', 'B', 'B', 'B']] Click : [1,2] Output: [['B', '1', 'E', '1', 'B'], ['B', '1', 'X', '1', 'B'], ['B', '1', '1', '1', 'B'], ['B', 'B', 'B', 'B', 'B']] Explanation:
Note:
- The range of the input matrix's height and width is [1,50].
- The click position will only be an unrevealed square ('M' or 'E'), which also means the input board contains at least one clickable square.
- The input board won't be a stage when game is over (some mines have been revealed).
- For simplicity, not mentioned rules should be ignored in this problem. For example, you don't 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.
题解:
可以采用BFS, 最开始的click, if it is B, add it to the que.
For the poll position, for all 8 neighbors, if it is E and could become B, add to the que.
Note: it is 8 neighbors, not only 4.
Time Complexity: O(m*n). m = board.length. n = board[0].length.
Space: O(m*n).
AC Java:
1 class Solution { 2 public char[][] updateBoard(char[][] board, int[] click) { 3 if(board == null || board.length == 0 || board[0].length == 0 || click == null || click.length != 2){ 4 return board; 5 } 6 7 int m = board.length; 8 int n = board[0].length; 9 if(click[0] < 0 || click[0] >= m || click[1] < 0 || click[1] >= n){ 10 return board; 11 } 12 13 LinkedList<int []> que = new LinkedList<>(); 14 if(board[click[0]][click[1]] == 'M'){ 15 board[click[0]][click[1]] = 'X'; 16 return board; 17 } 18 19 int count = getCount(board, click); 20 if(count > 0){ 21 board[click[0]][click[1]] = (char)('0' + count); 22 return board; 23 } 24 25 int [][] dirs = new int[][]{{1, 0}, {0, 1}, {-1, 0}, {0, -1}}; 26 board[click[0]][click[1]] = 'B'; 27 que.add(click); 28 while(!que.isEmpty()){ 29 int [] cur = que.poll(); 30 for(int i = -1; i <= 1; i++){ 31 for(int j = -1; j <=1; j++){ 32 if(i == 0 && j == 0){ 33 continue; 34 } 35 36 int x = cur[0] + i; 37 int y = cur[1] + j; 38 if(x < 0 || x >= m || y < 0 || y >= n || board[x][y] != 'E'){ 39 continue; 40 } 41 42 int soundCount = getCount(board, new int[]{x, y}); 43 if(soundCount > 0){ 44 board[x][y] = (char)('0' + soundCount); 45 }else{ 46 board[x][y] = 'B'; 47 que.add(new int[]{x, y}); 48 } 49 } 50 } 51 } 52 53 return board; 54 } 55 56 private int getCount(char [][] board, int [] arr){ 57 int count = 0; 58 59 for(int i = -1; i <= 1; i++){ 60 for(int j = -1; j <= 1; j++){ 61 if(i == 0 && j == 0){ 62 continue; 63 } 64 65 int x = arr[0] + i; 66 int y = arr[1] + j; 67 if(x < 0 || x >= board.length || y < 0 || y >= board[0].length){ 68 continue; 69 } 70 71 if(board[x][y] == 'M' || board[x][y] == 'X'){ 72 count++; 73 } 74 } 75 } 76 77 return count; 78 } 79 }
也可以采用DFS. DFS state needs the board and current click index.
Time Complexity: O(m*n). m = board.length, n = board[0].length.
Space: O(m*n).
AC Java:
1 class Solution { 2 public char[][] updateBoard(char[][] board, int[] click) { 3 if(board == null || board.length == 0 || board[0].length == 0){ 4 return board; 5 } 6 7 int m = board.length; 8 int n = board[0].length; 9 10 int r = click[0]; 11 int c = click[1]; 12 if(board[r][c] == 'M'){ 13 board[r][c] = 'X'; 14 }else{ 15 board[r][c] = 'B'; 16 17 int count = 0; 18 for(int i = -1; i<=1; i++){ 19 for(int j = -1; j<=1; j++){ 20 if(i==0 && j==0){ 21 continue; 22 } 23 24 int x = r+i; 25 int y = c+j; 26 if(x<0 || x>=m || y<0 || y>=n){ 27 continue; 28 } 29 if(board[x][y] == 'M' || board[x][y] == 'X'){ 30 count++; 31 } 32 } 33 } 34 35 if(count > 0){ 36 board[r][c] = (char)('0'+count); 37 }else{ 38 for(int i = -1; i<=1; i++){ 39 for(int j = -1; j<=1; j++){ 40 if(i==0 && j==0){ 41 continue; 42 } 43 44 int x = r+i; 45 int y = c+j; 46 if(x<0 || x>=m || y<0 || y>=n){ 47 continue; 48 } 49 if(board[x][y] == 'E'){ 50 updateBoard(board, new int[]{x, y}); 51 } 52 } 53 } 54 } 55 } 56 57 return board; 58 } 59 }