---
# System prepended metadata

title: PID-1080

---

Here is an assignment to solve the Minesweeper game:
Problem Statement:
You are given a 2D array `arr[][]` of dimensions `N*M`, representing a Minesweeper matrix, where each cell contains an integer from the range [0, 9], representing the number of mines in itself and all the eight cells adjacent to it. Your task is to solve the Minesweeper and uncover all the mines in the matrix. Print `X` for the cell containing a mine and `_` for all the other empty cells. If it is not possible to solve the Minesweeper, then print `-1`.
Code:
```cpp
#include <iostream>
#include <vector>

using namespace std;

bool is_valid(int x, int y, int N, int M) {
    return x >= 0 && x < N && y >= 0 && y < M;
}

vector<vector<char>> solve_minesweeper(vector<vector<char>>& arr, int N, int M) {
    vector<vector<int>> dx = {{-1, -1, -1, 0, 0, 1, 1, 1}};
    vector<vector<int>> dy = {{-1, 0, 1, -1, 1, -1, 0, 1}};
    
    for (int i = 0; i < N; ++i) {
        for (int j = 0; j < M; ++j) {
            if (arr[i][j] == 'X') {
                continue;
            }
            int count = 0;
            for (int d = 0; d < 8; ++d) {
                int x = i + dx[0][d];
                int y = j + dy[0][d];
                if (is_valid(x, y, N, M) && arr[x][y] == 'X') {
                    count++;
                }
            }
            if (count != int(arr[i][j] - '0')) {
                vector<vector<char>> impossible_solution = {{'-', '1'}};
                return impossible_solution;
            }
        }
    }
    return arr;
}

void print_minesweeper(vector<vector<char>>& arr) {
    if (arr[0][0] == '-' && arr[0][1] == '1') {
        cout << -1 << endl;
        return;
    }
    for (vector<char>& row : arr) {
        for (char cell : row) {
            cout << cell;
        }
        cout << endl;
    }
}

int main() {
    int N, M;
    cin >> N >> M;
    
    vector<vector<char>> arr(N, vector<char>(M));
    for (int i = 0; i < N; ++i) {
        for (int j = 0; j < M; ++j) {
            cin >> arr[i][j];
        }
    }
    
    vector<vector<char>> result = solve_minesweeper(arr, N, M);
    print_minesweeper(result);
    
    return 0;
}
```
Input:
* The first line contains two integers, `N and M`, representing the dimensions of the Minesweeper grid (N rows and M columns).
* The next N lines contain the cells of the Minesweeper grid. Each cell contains either an integer from the range [0, 9] or `X`, where `X` represents a mine.
Output:
* The output will be either a solved Minesweeper grid or -1 indicating that it is not possible to solve the Minesweeper with the given input.
Constraints:
* 1 <= N, M <= 50: The dimensions of the Minesweeper grid can be between 1x1 and 50x50, inclusive.
* Each cell in the grid contains an integer from the range [0, 9] or `X`, where `X` represents a mine.
Example:
```
    Input:
    3 3
    X11
    222
    1X_

    Output:
    X11
    222
    1X_
```
In this case, the Minesweeper is solved correctly. The `X` cells represent mines, and the numbers represent the count of adjacent mines. This solution is valid, so the output is the solved Minesweeper grid.