const directions = [[-1, 0],[-1, 1],[0, 1],[1, 1],[1, 0],[1, -1],[0, -1],[-1, -1]];
const check = (nx, ny, rowLength, colLength, now) =>
nx >= 0 && ny >= 0 && nx < rowLength && ny < colLength;
const dfs = (board, x, y) => {
if (board[x][y] !== "E") return;
let cnt = 0;
const nextArr = [];
const rowLength = board.length;
const colLength = board[0].length;
for (let i = 0; i < 8; i += 1) {
const [dx, dy] = directions[i];
const nx = x + dx;
const ny = y + dy;
if (check(nx, ny, rowLength, colLength)) {
if (board[nx][ny] === "M") cnt += 1;
if (board[nx][ny] === "E") nextArr.push([nx, ny]);
}
}
if (cnt) {
board[x][y] = `${cnt}`;
} else {
board[x][y] = "B";
for (const [nx, ny] of nextArr) {
dfs(board, nx, ny);
}
}
};
const updateBoard = (board, click) => {
const now = board[click[0]][click[1]];
if (now === 'M') board[click[0]][click[1]] = 'X';
dfs(board, click[0], click[1])
return board;
};
const updateBoard = (board, click) => {
const queue = [];
const m = board.length;
const n = board[0].length;
const directX = [-1, -1, 0, 1, 1, 1, 0, -1];
const directY = [0, 1, 1, 1, 0, -1, -1, -1];
const visited = Array.from({length: m}, () => Array(n).fill(false));
const answer = Array.from({length: m}, () => Array(n).fill("E"));
const checkRange = (x, y) => x >= 0 && x < m && y >= 0 && y < n;
const dfs = (x, y) => {
visited[x][y] = true;
if(board[x][y] === "M"){
board[x][y] = "X";
return;
}
let isBlank = true;
let count = 0;
const next = [];
for(let i = 0; i < 8; i++){
const nx = x + directX[i];
const ny = y + directY[i];
if(checkRange(nx, ny) && !visited[nx][ny]){
if(board[nx][ny] === "E"){
next.push([nx, ny]);
} else if(board[nx][ny] === "M"){
count++;
isBlank = false;
}
}
}
if(isBlank){
board[x][y] = "B";
for(let i = 0; i < next.length; i++){
dfs(next[i][0], next[i][1]);
}
} else{
board[x][y] = count.toString();
}
}
dfs(click[0], click[1]);
return board;
};
var updateBoard = function(board, click) {
const directX = [1, 1, 0, -1, -1, -1, 0, 1];
const directY = [0, -1, -1, -1, 0, 1, 1, 1];
const r = board.length, l = board[0].length;
let visited = Array.from(Array(r), () => Array(l).fill(false));
const checkRange = (x, y) => x>=0 && x<r && y>=0 && y<l;
const dfs = (x, y) => {
visited[x][y] = true;
if(board[x][y] === 'M') {
board[x][y] = 'X';
return;
}
let isBlank = true;
let count = 0;
const next = [];
for(let i=0; i<8; i++) {
const nx = x + directX[i];
const ny = y + directY[i];
if(checkRange(nx, ny) && !visited[nx][ny]) {
if(board[nx][ny] === 'E') {
next.push([nx, ny]);
} else if(board[nx][ny] === 'M') {
count++;
isBlank = false;
}
}
}
if(isBlank) {
board[x][y] = 'B';
for(let i=0; i<next.length; i++) {
dfs(next[i][0], next[i][1]);
}
}
else {
board[x][y] = count.toString();
}
}
dfs(click[0], click[1]);
return board;
};