Java implementation LeetCode 529 Minesweeper (DFS)

529. Minesweeper

Let's play Minesweeper!

Given a two-dimensional matrix represents the character of the game board. 'M' represents a non-excavated mine, 'E' represents a non-empty squares excavated, 'B' indicates no adjacent (upper, lower, left, right, and all four diagonal) has mines dug out of the box blank, digital ( '1' to '8') indicates how many mines have been dug out of the box with this neighbor, 'X' indicates that a mine has been dug up.

In all but now given excavated box ( 'M' or 'E') at a click position (row and column index), according to the following rules, a respective return position corresponding to the click panel:

If a mine ( 'M') was dug up, the game is over - change it to 'X'.
If a mine empty squares ( 'E') adjacent excavated, it is modified ( 'B'), and all of its neighboring block, and should be recursively disclosed.
If a mine with at least one adjacent open squares ( 'E') is excavated, change it to a digital ( '1' to '8'), it represents the number of adjacent mines.
If you click on this, the absence of more blocks may be revealed, the panel is returned.

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:
Here Insert Picture Description
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:
Here Insert Picture Description

note:

Input matrix width and height in the range [1, 50].
Click the location is not only dug out of the box ( 'M' or 'E'), this also means that the panel includes at least a clickable box.
Input Panel will not be the end of the game state (ie mines have been dug up).
For simplicity, the rules are not mentioned may be ignored in this problem. For example, when the game is that you do not need to dig out all the mines, consider all the circumstances you might win a game or mark the box.

PS:
all other things to say, mostly blank grid, you need to continue to recursively, other cases are found in mines or something when direct return on it

class Solution {
    static public char[][] updateBoard(char[][] board, int[] click) {
       
   	return visit(board,click[0],click[1]);	
   }
	
  static public char[][] visit(char[][] board, int x,int y){
	   if(board[x][y] == 'M') {
		   board[x][y] = 'X';
		   return board;
	   }
   	int count = 0;
       for(int i =x-1;i<=x+1;i++)
          for(int j =y-1; j<= y+1;j++){
        	  if(i>=0 && i < board.length && j>=0 && j<board[0].length)
              if(board[i][j] == 'M')
                   count++;
           }
   	if(count != 0 ) {
   		board[x][y] = (char)(count+'0');
   	}
   	else {
   		board[x][y] = 'B';
   	    for(int i =x-1;i<=x+1;i++)
               for(int j =y-1; j<= y+1;j++){
            	   if(i>=0 && i < board.length && j>=0 && j<board[0].length)
                   if(board[i][j] == 'E')
                       visit(board,i,j);
               }

   	}
   	
   	return board;
   }
}
Released 1621 original articles · won praise 20000 + · views 2.69 million +

Guess you like

Origin blog.csdn.net/a1439775520/article/details/105068303
Recommended