# Count the number of survivors

• Difficulty Level : Medium
• Last Updated : 15 Sep, 2022

Given a matrix mat[][] containing the characters [1 – 9], P and * where P is a person and * is an empty space. An integer in the current cell denotes a bomb that can destroy an object within a radius equal to the value of the cell. The task is to count and print the number of persons that will survive the blast.

Examples:

```Input:
mat[] = { "P***",
"****",
"2**P",
"****" }
Output: 1
Only the person in the third row
will survive. Note that the person
in the first row is within the
radius of the bomb.

Input:
mat[] = { "2***P*",
"P*****",
"******",
"**1***",
"******",
"*****P" }
Output: 2
The persons in the first and last
row survive.```

Approach:

Traverse the matrix character by character, if the current character is a digit (1 – 9) then change all the surrounding elements within the radius to * (in all eight directions) indicating that no one survived within that radius. After all the elements of the matrix have been traversed, count the number of cells indicating a person i.e. mat[i][j] = ‘P’. Print the count in the end.

Below is the implementation of the above approach:

## C++

 `// C++ implementation of the approach``#include ``using` `namespace` `std;` `// Function to return the count of``// the people that survived``int` `countSurvivors(string mat[], ``int` `row, ``int` `col)``{``    ``int` `i, j, count = 0;` `    ``for` `(i = 0; i < row; i++) {``        ``for` `(j = 0; j < col; j++) {` `            ``// If current cell is a bomb``            ``if` `(mat[i][j] > ``'0'` `&& mat[i][j] <= ``'9'``) {``                ``int` `radius = mat[i][j] - ``'0'``;` `                ``// Coordinates of the sub-matrix``                ``// that will be affected by the blast``                ``int` `iMin = max(i - radius, 0);``                ``int` `iMax = min(i + radius, row - 1);``                ``int` `jMin = max(j - radius, 0);``                ``int` `jMax = min(j + radius, col - 1);` `                ``for` `(``int` `k = iMin; k <= iMax; k++)``                    ``for` `(``int` `l = jMin; l <= jMax; l++)` `                        ``// Set the current cell to``                        ``//  empty space without a person``                        ``mat[k][l] = ``'*'``;``            ``}``        ``}``    ``}` `    ``// Count the remaining persons``    ``for` `(i = 0; i < row; i++)``        ``for` `(j = 0; j < col; j++)``            ``if` `(mat[i][j] == ``'P'``)``                ``count++;` `    ``return` `count;``}` `// Driver Code``int` `main()``{``    ``// Input matrix``    ``string mat[] = { ``"P***"``, ``"****"``, ``"2**P"``, ``"****"` `};` `    ``// Rows and columns of the matrix``    ``int` `row = ``sizeof``(mat) / ``sizeof``(mat);``    ``int` `col = mat.length();` `    ``// Total people survived``    ``cout << countSurvivors(mat, row, col);``}`

## Java

 `// Java implementation of the approach``class` `GFG``{` `// Function to return the count of``// the people that survived``static` `int` `countSurvivors(String mat[], ``int` `row, ``int` `col)``    ``{``        ``int` `i, j, count = ``0``;``    ` `        ``for` `(i = ``0``; i < row; i++)``        ``{``            ``for` `(j = ``0``; j < col; j++)``            ``{``    ` `                ``// If current cell is a bomb``                ``if` `(mat[i].charAt(j) > ``'0'` `&& mat[i].charAt(j) <= ``'9'``)``                ``{``                    ``int` `radius = mat[i].charAt(j) - ``'0'``;``    ` `                    ``// Coordinates of the sub-matrix``                    ``// that will be affected by the blast``                    ``int` `iMin = Math.max(i - radius, ``0``);``                    ``int` `iMax = Math.min(i + radius, row - ``1``);``                    ``int` `jMin = Math.max(j - radius, ``0``);``                    ``int` `jMax = Math.min(j + radius, col - ``1``);``    ` `                    ``for` `(``int` `k = iMin; k <= iMax; k++)``                    ``{``                        ` `                        ``// convert string nto character array``                        ``char` `C [] = mat[k].toCharArray();``                        ` `                        ``for` `(``int` `l = jMin; l <= jMax; l++)``                        ``{``                            ``// Set the current cell to``                            ``// empty space without a person``                            ``C [l] = ``'*'` `;``                        ``}``                        ``// convert character array back into the string``                        ``String s = ``new` `String(C) ;``                        ``mat[k] = s ;``                    ``}``                ``}``            ``}``        ``}``    ` `        ``// Count the remaining persons``        ``for` `(i = ``0``; i < row; i++)``            ``for` `(j = ``0``; j < col; j++)``                ``if` `(mat[i].charAt(j) == ``'P'``)``                    ``count++;``    ` `        ``return` `count;``    ``}``    ` `    ``// Driver Code``    ``public` `static` `void` `main(String []args)``    ``{``        ``// Input matrix``        ``String mat[] = { ``"P***"``, ``"****"``, ``"2**P"``, ``"****"` `};``    ` `        ``// Rows and columns of the matrix``        ``int` `row = mat.length ;``        ``int` `col = mat[``0``].length();``    ` `        ``// Total people survived``        ``System.out.println(countSurvivors(mat, row, col));``    ``}``}` `// This code is contributed by Ryuga`

## Python3

 `# Python3 implementation of the approach``import` `math as mt` `# Function to return the count of``# the people that survived``def` `countSurvivors(mat, row, col):` `    ``i, j, count ``=` `0``, ``0``, ``0` `    ``for` `i ``in` `range``(row):``        ``for` `j ``in` `range``(col):` `            ``# If current cell is a bomb``            ``if` `(mat[i][j] > ``'0'` `and` `mat[i][j] <``=` `'9'``):``                ``radius ``=` `ord``(mat[i][j]) ``-` `ord``(``'0'``)` `                ``# Coordinates of the sub-matrix``                ``# that will be affected by the blast``                ``iMin ``=` `max``(i ``-` `radius, ``0``)``                ``iMax ``=` `min``(i ``+` `radius, row ``-` `1``)``                ``jMin ``=` `max``(j ``-` `radius, ``0``)``                ``jMax ``=` `min``(j ``+` `radius, col ``-` `1``)` `                ``for` `k ``in` `range``(iMin, iMax ``+` `1``):``                    ``for` `l ``in` `range``(jMin, jMax ``+` `1``):` `                        ``# Set the current cell to``                        ``# empty space without a person``                        ``mat[k][l] ``=` `'*'``            ` `    ``# Count the remaining persons``    ``for` `i ``in` `range``(row):``        ``for` `j ``in` `range``(col):``            ``if` `(mat[i][j] ``=``=` `'P'``):``                ``count ``+``=` `1` `    ``return` `count` `# Driver Code` `# Input matrix``mat ``=` `[[``"P"``, ``"*"``, ``"*"``, ``"*"``],``       ``[``"*"``, ``"*"``, ``"*"``, ``"*"``],``       ``[``"2"``, ``"*"``, ``"*"``, ``"P"``],``       ``[``"*"``, ``"*"``, ``"*"``, ``"*"``]]` `# Rows and columns of the matrix``row ``=` `len``(mat)``col ``=` `len``(mat[``0``])` `# Total people survived``print``(countSurvivors(mat, row, col))` `# This code is contributed by``# Mohit Kumar 29`

## C#

 `// C# implementation of the approach``using` `System;``using` `System.Collections.Generic;``    ` `class` `GFG``{` `// Function to return the count of``// the people that survived``static` `int` `countSurvivors(String []mat,``                          ``int` `row, ``int` `col)``{``    ``int` `i, j, count = 0;` `    ``for` `(i = 0; i < row; i++)``    ``{``        ``for` `(j = 0; j < col; j++)``        ``{` `            ``// If current cell is a bomb``            ``if` `(mat[i][j] > ``'0'` `&& mat[i][j] <= ``'9'``)``            ``{``                ``int` `radius = mat[i][j] - ``'0'``;` `                ``// Coordinates of the sub-matrix``                ``// that will be affected by the blast``                ``int` `iMin = Math.Max(i - radius, 0);``                ``int` `iMax = Math.Min(i + radius, row - 1);``                ``int` `jMin = Math.Max(j - radius, 0);``                ``int` `jMax = Math.Min(j + radius, col - 1);` `                ``for` `(``int` `k = iMin; k <= iMax; k++)``                ``{``                    ` `                    ``// convert string nto character array``                    ``char` `[]C = mat[k].ToCharArray();``                    ` `                    ``for` `(``int` `l = jMin; l <= jMax; l++)``                    ``{``                        ``// Set the current cell to``                        ``// empty space without a person``                        ``C [l] = ``'*'``;``                    ``}``                    ``// convert character array back into the string``                    ``String s = ``new` `String(C);``                    ``mat[k] = s;``                ``}``            ``}``        ``}``    ``}` `    ``// Count the remaining persons``    ``for` `(i = 0; i < row; i++)``        ``for` `(j = 0; j < col; j++)``            ``if` `(mat[i][j] == ``'P'``)``                ``count++;` `    ``return` `count;``}` `// Driver Code``public` `static` `void` `Main(String []args)``{``    ``// Input matrix``    ``String []mat = { ``"P***"``, ``"****"``,``                     ``"2**P"``, ``"****"` `};` `    ``// Rows and columns of the matrix``    ``int` `row = mat.Length ;``    ``int` `col = mat.Length;` `    ``// Total people survived``    ``Console.WriteLine(countSurvivors(mat, row, col));``}``}` `// This code is contributed by Rajput-Ji`

## Javascript

 ``

Output

`1`

My Personal Notes arrow_drop_up