原题链接在这里: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加入queue中, 出queue后若这是个周围没有雷的点, 就把周围8个'E'的点加进queue.
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 LinkedList<int []> que = new LinkedList<int []>(); 10 que.add(click); 11 while(!que.isEmpty()){ 12 int [] cur = que.poll(); 13 int r = cur[0]; 14 int c = cur[1]; 15 16 if(board[r][c] == 'M'){ 17 // 是雷 18 board[r][c] = 'X'; 19 }else{ 20 // 不是雷 21 board[r][c] = 'B'; 22 int count = 0; 23 for(int i = -1; i<=1; i++){ 24 for(int j = -1; j<=1; j++){ 25 if(i==0 && j==0){ 26 continue; 27 } 28 int x = r+i; 29 int y = c+j; 30 if(x<0 || x>=m || y<0 || y>=n){ 31 continue; 32 } 33 if(board[x][y] == 'M' || board[x][y] == 'X'){ 34 count++; 35 } 36 } 37 } 38 39 if(count > 0){ 40 // 周围有雷 41 board[r][c] = (char)(count+'0'); 42 }else{ 43 for(int i = -1; i<=1; i++){ 44 for(int j = -1; j<=1; j++){ 45 if(i==0 && j==0){ 46 continue; 47 } 48 49 int x = r+i; 50 int y = c+j; 51 if(x<0 || x>=m || y<0 || y>=n){ 52 continue; 53 } 54 if(board[x][y] == 'E'){ 55 que.add(new int[]{x, y}); 56 board[x][y] = 'B'; 57 } 58 } 59 } 60 } 61 } 62 } 63 return board; 64 } 65 }
也可以采用DFS.
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 }