Count the number of survivors

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 object within the 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 cell indicating a person i.e. mat[i][j] = ‘P’. Print the count in the end.

Below is the implementation of the above approach:

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ implementation of the approach
#include <iostream>
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[0]);
    int col = mat[0].length();
  
    // Total people survived
    cout << countSurvivors(mat, row, col);
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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

chevron_right


Output:

1


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



Improved By : mohit kumar 29, AnkitRai01



Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.