# Straight forward Java solution

• ``````public class Solution {
public char[][] updateBoard(char[][] board, int[] click) {
int x = click[0], y = click[1];
if (board[x][y] == 'M') {
board[x][y] = 'X';
return board;
}

dfs(board, x, y);
return board;
}

int[] dx = {-1, 0, 1, -1, 1, 0, 1, -1};
int[] dy = {-1, 1, 1, 0, -1, -1, 0, 1};
private void dfs(char[][] board, int x, int y) {
if (x < 0 || x >= board.length || y < 0 || y >= board[0].length || board[x][y] != 'E')  return;

int num = getNumsOfBombs(board, x, y);

if (num == 0) {
board[x][y] = 'B';
for (int i = 0; i < 8; i++) {
int nx = x + dx[i], ny = y + dy[i];
dfs(board, nx, ny);
}
} else {
board[x][y] = (char)('0' + num);
}

}

private int getNumsOfBombs(char[][] board, int x, int y) {
int num = 0;
for (int i = -1; i <= 1; i++) {
for (int j = -1; j <= 1; j++) {
int nx = x + i, ny = y + j;
if (nx < 0 || nx >= board.length || ny < 0 || ny >= board[0].length)    continue;
if (board[nx][ny] == 'M' || board[nx][ny] == 'X') {
num++;
}
}
}
return num;
}
}
``````

• Thank you so your solution. It's really elegant. I attached my BFS solution for reference.

``````public char[][] updateBoard(char[][] board, int[] click) {
int m = board.length, n = board[0].length;
int x = click[0], y = click[1];
if (board[x][y] == 'M') {
board[x][y] = 'X';
return board;
}

queue.offer(x * n + y);

while (!queue.isEmpty()) {
int idx = queue.poll();
int curX = idx / n, curY = idx % n;
int num = getNumberOfBombs(board, curX, curY);
if (num == 0) {
board[curX][curY] = 'B';
for (int i = -1; i < 2; i++) {
for (int j = -1; j < 2; j++) {
if (i == 0 && j == 0) continue;
int newX = curX + i, newY = curY + j;
if (newX < 0 || newY < 0 || newX >= board.length || newY >= board[0].length || board[newX][newY] != 'E') continue;
queue.offer(newX * n + newY);
board[newX][newY] = 'B'; // Avoid being added again
}
}
} else {
board[curX][curY] = (char)(num + '0');
}
}
return board;
}

private int getNumberOfBombs(char[][] board, int x, int y) {
int count = 0;
for (int i = -1; i < 2; i++) {
for (int j = -1; j < 2; j++) {
if (i == 0 && j == 0) continue;
int newX = x + i, newY = y + j;
if (newX < 0 || newY < 0 || newX >= board.length || newY >= board[0].length) continue;
if (board[newX][newY] == 'M' || board[newX][newY] == 'X') count++;
}
}

return count;
}
``````

• No need to use additional array dx, dy as helper, just two for loop is fine.
Attach my code here:

``````public class Solution {
public char[][] updateBoard(char[][] board, int[] click) {
int x = click[0], y = click[1];
if (board[x][y] == 'M') {
board[x][y] = 'X';
return board;
}
dfs(board, x, y);
return board;
}

private void dfs(char[][] board, int x, int y) {
if (x < 0 || y < 0 || x >= board.length || y >= board[0].length || board[x][y] != 'E') return;
int mines = findMines(board, x, y);
if (mines == 0) {
board[x][y] = 'B';
for (int i = -1; i <= 1; i++) {
for (int j = -1; j <= 1; j++) {
dfs(board, x + i, y + j);
}
}
}
else board[x][y] = (char)('0' + mines);
}

private int findMines(char[][] board, int x, int y) {
int num = 0;
for (int i = -1; i <= 1; i++) {
for (int j = -1; j <= 1; j++) {
int x1 = x + i, y1 = y + j;
if (x1 < 0 || y1 < 0 || x1 >= board.length || y1 >= board[0].length) continue;
if (board[x1][y1] == 'M')
num++;
}
}
return num;
}
}
``````

• Easy and Simple #Genius #roleModel

• @uzumaki hahahahahah

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.