1

Minesweeper

Difficulty: MEDIUM
Avg. time to solve
30 min
Success Rate
70%

Problem Statement
Suggest Edit
1) ‘#’ (HASH) represents an unrevealed mine.
2) ‘.’ (DOT) represents an unrevealed empty cell.
3) ‘_’ (UNDERSCORE) represents a revealed blank cell that has no neighbouring mines.
4) Digits (‘1’ to ‘8’) represents the number of neighbouring mines corresponding to the current cell.
5) ‘*’ (ASTERISK) represents a revealed mine.

In this game, you are given a click position (row & column indices) among all unrevealed cells (‘#’ or ‘.’). Your task is to return the game board after revealing the click position as per the following rules:

1) If the revealed cell contains a mine (‘#’), then the game is over - change it to ‘*’.
2) If the revealed cell is empty ('.') with no neighbouring mines revealed, then change it to revealed blank ('_'), and all of its neighbouring unrevealed cells should be revealed recursively.
3) If the revealed cell is empty ('.') with at least one neighbouring mine revealed, then change it to a digit ('1' to '8') representing the number of neighbouring mines.

Note:

1) Two cells are neighbours if they share a corner or an edge, i.e., cells corresponding above, below, left, right, and all four diagonals.
2) For simplicity, ignore the rules which are not mentioned in this problem. For example, when the game is over,  you don't need to reveal all the unrevealed mines.

Input Format:

The first line of input contains an integer 'T' representing the number of test cases.

The first line of each test case contains two single-spaced integers, ‘M’ and ‘N’, representing the number of rows and columns of the matrix, respectively.

The next ‘M’ line contains ‘N’ single-spaced elements.

The last line of each test case contains two single-spaced integers, ‘x’ and ‘y’, representing the row and column indices of the click position, respectively.

Output Format:

For each test case, return the updated game board when no more cells can be revealed.

The output of each test case will be printed in a separate line.

Note:

You do not need to print anything, it has already been taken care of. Just implement the given function.

Constraints:

1 <= T <= 5
1 <= M, N <= 50
0 <= x <= M - 1
0 <= y <= N - 1
data ∈ {‘#’, ‘*’, ‘.’, ‘_’, ‘1’, ‘2’, ‘3’, ‘4’, ‘5’, ‘6’, ‘7’, ‘8’}

Where ‘data’ is the value of the elements of the matrix, "gameBoard".

The click position will correspond to an unrevealed cell ('#' or '.'), which also means the input board contains at least one clickable cell.
The input board is not provided at a stage when the game is over (some mines have been revealed).

Time Limit: 1 sec

Sample Input 1:

2
5 5
. . 1 _ _
. . 2 1 _
. . . 1 _
1 . . 2 _
. . # 1 _
0 1
4 6
. . # 1 . .
2 3 . . . .
_ 1 . . 4 .
_ 1 . . . .
0 2

Sample Output 1:

_ _ 1 _ _
_ _ 2 1 _
_ _ _ 1 _
1 1 1 2 _
. . # 1 _
. . * 1 . .
2 3 . . . .
_ 1 . . 4 .
_ 1 . . . .

Explanation of Sample Output 1:

Test Case 1 :
The configuration of ‘gameBoard’ is:
. . * 1 . .
2 3 . . . .
_ 1 . . 4 .
_ 1 . . . .
And, the click position is (0, 1).
At (0, 1), we have an unrevealed cell. So, we will count the number of neighbouring mines, which is 0. So, we will replace the ‘.’ by ‘_’ and then recur in all eight directions from (0, 1). The possible click positions will then be (0, 0), (0, 2), (1, 0), (1, 1), (1, 2).
After recurring for all click positions, the final configuration of the board will be:
_ _ 1 _ _
_ _ 2 1 _
_ _ _ 1 _
1 1 1 2 _
. . # 1 _
which will be printed as output.


Test Case 2 : 
The configuration of ‘gameBoard’ is:
. . # 1 . .
2 3 . . . .
_ 1 . . 4 .
_ 1 . . . .
And, click position is (0, 2).
We can see that the cell at (0, 2) contains an unrevealed mine (‘#’). So, according to the rules, we will replace the unrevealed mine (‘#’) by a revealed mine (‘*’) and return the board.
Therefore, the final configuration of the board will be:
. . * 1 . .
2 3 . . . .
_ 1 . . 4 .
_ 1 . . . .
which will be printed as output.

Sample Input 2:

1
4 6
. . # 1 . .
2 3 . . . .
_ 1 . . 4 .
_ 1 . . . .
3 2

Sample Output 2:

. . # 1 _ _
2 3 1 1 _ _
_ 1 _ _ 4 _
_ 1 _ _ _ _
Reset Code
Full screen
copy-code
Console