<p>Let&#39;s play the minesweeper game (<a href="https://en.wikipedia.org/wiki/Minesweeper_(video_game)" target="_blank">Wikipedia</a>, <a href="http://minesweeperonline.com" target="_blank">online game</a>)!</p>

<p>You are given an <code>m x n</code> char matrix <code>board</code> representing the game board where:</p>

<ul>
	<li><code>&#39;M&#39;</code> represents an unrevealed mine,</li>
	<li><code>&#39;E&#39;</code> represents an unrevealed empty square,</li>
	<li><code>&#39;B&#39;</code> represents a revealed blank square that has no adjacent mines (i.e., above, below, left, right, and all 4 diagonals),</li>
	<li>digit (<code>&#39;1&#39;</code> to <code>&#39;8&#39;</code>) represents how many mines are adjacent to this revealed square, and</li>
	<li><code>&#39;X&#39;</code> represents a revealed mine.</li>
</ul>

<p>You are also given an integer array <code>click</code> where <code>click = [click<sub>r</sub>, click<sub>c</sub>]</code> represents the next click position among all the unrevealed squares (<code>&#39;M&#39;</code> or <code>&#39;E&#39;</code>).</p>

<p>Return <em>the board after revealing this position according to the following rules</em>:</p>

<ol>
	<li>If a mine <code>&#39;M&#39;</code> is revealed, then the game is over. You should change it to <code>&#39;X&#39;</code>.</li>
	<li>If an empty square <code>&#39;E&#39;</code> with no adjacent mines is revealed, then change it to a revealed blank <code>&#39;B&#39;</code> and all of its adjacent unrevealed squares should be revealed recursively.</li>
	<li>If an empty square <code>&#39;E&#39;</code> with at least one adjacent mine is revealed, then change it to a digit (<code>&#39;1&#39;</code> to <code>&#39;8&#39;</code>) representing the number of adjacent mines.</li>
	<li>Return the board when no more squares will be revealed.</li>
</ol>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>
<img src="https://assets.leetcode.com/uploads/2023/08/09/untitled.jpeg" style="width: 500px; max-width: 400px; height: 269px;" />
<pre>
<strong>Input:</strong> board = [[&quot;E&quot;,&quot;E&quot;,&quot;E&quot;,&quot;E&quot;,&quot;E&quot;],[&quot;E&quot;,&quot;E&quot;,&quot;M&quot;,&quot;E&quot;,&quot;E&quot;],[&quot;E&quot;,&quot;E&quot;,&quot;E&quot;,&quot;E&quot;,&quot;E&quot;],[&quot;E&quot;,&quot;E&quot;,&quot;E&quot;,&quot;E&quot;,&quot;E&quot;]], click = [3,0]
<strong>Output:</strong> [[&quot;B&quot;,&quot;1&quot;,&quot;E&quot;,&quot;1&quot;,&quot;B&quot;],[&quot;B&quot;,&quot;1&quot;,&quot;M&quot;,&quot;1&quot;,&quot;B&quot;],[&quot;B&quot;,&quot;1&quot;,&quot;1&quot;,&quot;1&quot;,&quot;B&quot;],[&quot;B&quot;,&quot;B&quot;,&quot;B&quot;,&quot;B&quot;,&quot;B&quot;]]
</pre>

<p><strong class="example">Example 2:</strong></p>
<img src="https://assets.leetcode.com/uploads/2023/08/09/untitled-2.jpeg" style="width: 489px; max-width: 400px; height: 269px;" />
<pre>
<strong>Input:</strong> board = [[&quot;B&quot;,&quot;1&quot;,&quot;E&quot;,&quot;1&quot;,&quot;B&quot;],[&quot;B&quot;,&quot;1&quot;,&quot;M&quot;,&quot;1&quot;,&quot;B&quot;],[&quot;B&quot;,&quot;1&quot;,&quot;1&quot;,&quot;1&quot;,&quot;B&quot;],[&quot;B&quot;,&quot;B&quot;,&quot;B&quot;,&quot;B&quot;,&quot;B&quot;]], click = [1,2]
<strong>Output:</strong> [[&quot;B&quot;,&quot;1&quot;,&quot;E&quot;,&quot;1&quot;,&quot;B&quot;],[&quot;B&quot;,&quot;1&quot;,&quot;X&quot;,&quot;1&quot;,&quot;B&quot;],[&quot;B&quot;,&quot;1&quot;,&quot;1&quot;,&quot;1&quot;,&quot;B&quot;],[&quot;B&quot;,&quot;B&quot;,&quot;B&quot;,&quot;B&quot;,&quot;B&quot;]]
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>m == board.length</code></li>
	<li><code>n == board[i].length</code></li>
	<li><code>1 &lt;= m, n &lt;= 50</code></li>
	<li><code>board[i][j]</code> is either <code>&#39;M&#39;</code>, <code>&#39;E&#39;</code>, <code>&#39;B&#39;</code>, or a digit from <code>&#39;1&#39;</code> to <code>&#39;8&#39;</code>.</li>
	<li><code>click.length == 2</code></li>
	<li><code>0 &lt;= click<sub>r</sub> &lt; m</code></li>
	<li><code>0 &lt;= click<sub>c</sub> &lt; n</code></li>
	<li><code>board[click<sub>r</sub>][click<sub>c</sub>]</code> is either <code>&#39;M&#39;</code> or <code>&#39;E&#39;</code>.</li>
</ul>

class Solution {
public:
    bool isValid(int i , int j , int rowSize , int colSize) {
        if(i>=0 && i<rowSize && j>=0 && j<colSize)
            return true;
        return false;
    }

    int countAdjacentMines(vector<int> &click , vector<vector<char>> &board) {
        int count = 0;
        int i=click[0];
        int j=click[1];
        int rowSize = board.size();
        int colSize = board[0].size();
        //top left i-1,j-1
        if(isValid(i-1,j-1, rowSize,colSize) && board[i-1][j-1] == 'M')
            count++;

        //top i-1,j
        if(isValid(i-1,j,rowSize,colSize) && board[i-1][j] == 'M')
            count++;

        //top right i-1,j+1
        if(isValid(i-1 , j+1 , rowSize , colSize) && board[i-1][j+1] == 'M')
            count++;

        //right i,j+1
        if(isValid(i, j+1, rowSize , colSize) && board[i][j+1] == 'M')
            count++;

        //bottom right i+1,j+1
        if(isValid(i+1,j+1,rowSize,colSize) && board[i+1][j+1] == 'M')
            count++;

        //bottom i+1,j
        if(isValid(i+1,j,rowSize,colSize) && board[i+1][j] == 'M')
            count++;

        //bottom left i+1,j-1;
        if(isValid(i+1,j-1,rowSize,colSize) && board[i+1][j-1] == 'M')
            count++;

        //left i , j-1;
        if(isValid(i,j-1,rowSize,colSize) && board[i][j-1] == 'M')
            count++;


        return count;
    }

    vector<vector<char>> updateBoard(vector<vector<char>>& board,
                                     vector<int>& click) {

        int rowSize = board.size();
        int colSize = board[0].size();

        //return if click is 'M' , mark as 'X';
        if(board[click[0]][click[1]] == 'M') {
            board[click[0]][click[1]] = 'X';
            return board;
        }

        queue<vector<int>> q;
        q.push(click);

        while (q.size() > 0) {
            vector<int> c = q.front();
            q.pop();
            
            int i = c[0];
            int j = c[1];
            // click iff the current cell is not revealed, i.e. , 'E';
            if (board[i][j] == 'E') {
                // check all eight cells, if no mines place 'B' and add all
                // cells with 'E' in the queue; 
                int numberOfMines = countAdjacentMines(c, board);
                if(numberOfMines == 0) {
                    board[i][j] = 'B';
                    //add all adjacent 'E' cells to the queue;

                    //top left i-1,j-1
                    if(isValid(i-1,j-1, rowSize,colSize) && board[i-1][j-1] == 'E')
                        q.push({i-1,j-1});

                    //top i-1,j
                    if(isValid(i-1,j,rowSize,colSize) && board[i-1][j] == 'E')
                        q.push({i-1,j});

                    //top right i-1,j+1
                    if(isValid(i-1 , j+1 , rowSize , colSize) && board[i-1][j+1] == 'E')
                        q.push({i-1,j+1});

                    //right i,j+1
                    if(isValid(i, j+1, rowSize , colSize) && board[i][j+1] == 'E')
                        q.push({i , j+1});

                    //bottom right i+1,j+1
                    if(isValid(i+1,j+1,rowSize,colSize) && board[i+1][j+1] == 'E')
                        q.push({i+1,j+1});

                    //bottom i+1,j
                    if(isValid(i+1,j,rowSize,colSize) && board[i+1][j] == 'E')
                        q.push({i+1,j});

                    //bottom left i+1,j-1;
                    if(isValid(i+1,j-1,rowSize,colSize) && board[i+1][j-1] == 'E')
                        q.push({i+1,j-1});

                    //left i , j-1;
                    if(isValid(i,j-1,rowSize,colSize) && board[i][j-1] == 'E')
                        q.push({i,j-1});
                }
                else {
                    // if mines , place digit 1-8 according to number of adjacent mines; and dont add any cells
                    // to the queue.
                    board[i][j] = numberOfMines + '0';
                }
            }
        }

        return board;
    }
};
17000