Given a matrix mat[][] containing the characters Z, P and * where Z is a zombie, P is a plant and * is a bare land. Given that a zombie can attack a plant if the plant is adjacent to the zombie (total, 8 adjacent cells are possible). The task is to print the number of plants that are safe from the zombies.
Examples:Â
Input:
mat[] = { "**P*",
"*Z**",
"*Z**",
"***P" }
Output: 1
Input:
mat[] = { "**P*P",
"*Z**",
"*P**",
"***P" }
Output: 2
Approach: Traverse the matrix element by element, if the current element is a plant i.e., mat[i][j] = ‘P’ then check if the plant is surrounded by any zombie (in all the 8 adjacent cells). If the plant is safe, then update count = count + 1. Print the count in the end.
Below is the implementation of the above approach:Â
C++
// C++ implementation of the approach#include <iostream>using namespace std;Â
// Function that returns true if mat[i][j] is a zombiebool isZombie(int i, int j, int r, int c, string mat[]){    if (i < 0 || j < 0 || i >= r || j >= c        || mat[i][j] != 'Z')        return false;Â
    return true;}Â
// Function to return the count of plants// that survived from the zombies attackint Plant_Vs_Zombies(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 plant            if (mat[i][j] == 'P') {Â
                // If current plant is safe from zombies                if (!isZombie(i - 1, j - 1, row, col, mat)                    && !isZombie(i - 1, j, row, col, mat)                    && !isZombie(i - 1, j + 1, row, col, mat)                    && !isZombie(i, j - 1, row, col, mat)                    && !isZombie(i, j, row, col, mat)                    && !isZombie(i, j + 1, row, col, mat)                    && !isZombie(i + 1, j - 1, row, col, mat)                    && !isZombie(i + 1, j, row, col, mat)                    && !isZombie(i + 1, j + 1, row, col, mat)) {                    count++;                }            }        }    }Â
    return count;}Â
// Driver Codeint main(){    // Input matrix    string mat[] = { "**P*", "*Z**", "*Z**", "***P" };Â
    // Rows and columns of the matrix    int row = sizeof(mat) / sizeof(mat[0]);    int col = mat[0].length();Â
    // Total plants survived    cout << Plant_Vs_Zombies(mat, row, col);} |
Java
// Java implementation of the approach. Â
class GfG{Â
    // Function that returns true if     // mat[i][j] is a zombie     static boolean isZombie(int i, int j, int r,                            int c, String mat[])     {         if (i < 0 || j < 0 || i >= r || j >= c             || mat[i].charAt(j) != 'Z')             return false;              return true;     }          // Function to return the count of plants     // that survived from the zombies attack     static int Plant_Vs_Zombies(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 plant                 if (mat[i].charAt(j) == 'P')                 {                          // If current plant is safe from zombies                     if (!isZombie(i - 1, j - 1, row, col, mat)                         && !isZombie(i - 1, j, row, col, mat)                         && !isZombie(i - 1, j + 1, row, col, mat)                         && !isZombie(i, j - 1, row, col, mat)                         && !isZombie(i, j, row, col, mat)                         && !isZombie(i, j + 1, row, col, mat)                         && !isZombie(i + 1, j - 1, row, col, mat)                         && !isZombie(i + 1, j, row, col, mat)                         && !isZombie(i + 1, j + 1, row, col, mat)) {                         count++;                     }                 }             }         }         return count;     } Â
    // Driver code    public static void main(String []args)    {                 // Input matrix         String[] mat = { "**P*", "*Z**", "*Z**", "***P" };              // Rows and columns of the matrix         int row = mat.length;         int col = mat[0].length();              // Total plants survived         System.out.println(Plant_Vs_Zombies(mat, row, col));    }}Â
// This code is contributed by Rituraj Jain |
Python3
# Python3 implementation of the approach. Â
# Function that returns true if # mat[i][j] is a zombie def isZombie(i, j, r, c, mat): Â
    if (i < 0 or j < 0 or i >= r or        j >= c or mat[i][j] != 'Z'):         return True; Â
    return False; Â
# Function to return the count of plants # that survived from the zombies attack def Plant_Vs_Zombies(mat, row, col): Â
    count = 0; Â
    for i in range(row):         for j in range(col):Â
            # If current cell is a plant             if (mat[i][j] == 'P'):Â
                # If current plant is safe from zombies                 if (isZombie(i - 1, j - 1, row, col, mat) and                    isZombie(i - 1, j, row, col, mat) and                    isZombie(i - 1, j + 1, row, col, mat) and                    isZombie(i, j - 1, row, col, mat) and                    isZombie(i, j, row, col, mat) and                    isZombie(i, j + 1, row, col, mat) and                    isZombie(i + 1, j - 1, row, col, mat) and                    isZombie(i + 1, j, row, col, mat) and                    isZombie(i + 1, j + 1, row, col, mat)):                    count += 1;     return count; Â
# Driver codeÂ
# Input matrix mat = ["**P*", "*Z**", "*Z**", "***P"]; Â
# Rows and columns of the matrix row = len(mat); col = len(mat[0]); Â
# Total plants survived print(Plant_Vs_Zombies(mat, row, col));Â
# This code is contributed by mits |
C#
// C# implementation of the approach.using System;Â
class GfG{Â
    // Function that returns true if     // mat[i][j] is a zombie     static bool isZombie(int i, int j, int r,                            int c, String []mat)     {         if (i < 0 || j < 0 || i >= r || j >= c             || mat[i][j] != 'Z')             return false;              return true;     }          // Function to return the count of plants     // that survived from the zombies attack     static int Plant_Vs_Zombies(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 plant                 if (mat[i][j] == 'P')                 {                          // If current plant is safe from zombies                     if (!isZombie(i - 1, j - 1, row, col, mat)                         && !isZombie(i - 1, j, row, col, mat)                         && !isZombie(i - 1, j + 1, row, col, mat)                         && !isZombie(i, j - 1, row, col, mat)                         && !isZombie(i, j, row, col, mat)                         && !isZombie(i, j + 1, row, col, mat)                         && !isZombie(i + 1, j - 1, row, col, mat)                         && !isZombie(i + 1, j, row, col, mat)                         && !isZombie(i + 1, j + 1, row, col, mat))                     {                         count++;                     }                 }             }         }         return count;     } Â
    // Driver code    public static void Main(String []args)    {                 // Input matrix         String[] mat = { "**P*", "*Z**", "*Z**", "***P" };              // Rows and columns of the matrix         int row = mat.Length;         int col = mat[0].Length;              // Total plants survived         Console.WriteLine(Plant_Vs_Zombies(mat, row, col));    }}Â
// This code contributed by Rajput-Ji |
PHP
<?php// PHP implementation of the approach. Â
// Function that returns true if // mat[i][j] is a zombie function isZombie($i, $j, $r, $c, $mat) { Â Â Â Â if ($i < 0 || $j < 0 || $i >= $r || Â Â Â Â Â Â Â Â $j >= $c || $mat[$i][$j] != 'Z') Â Â Â Â Â Â Â Â return false; Â
    return true; } Â
// Function to return the count of plants // that survived from the zombies attack function Plant_Vs_Zombies($mat, $row, $col) { Â Â Â Â $i; $j; $count = 0; Â
    for ($i = 0; $i < $row; $i++)     {         for ($j = 0; $j < $col; $j++)        { Â
            // If current cell is a plant             if ($mat[$i][$j] == 'P')             { Â
                // If current plant is safe from zombies                 if (!isZombie($i - 1, $j - 1, $row, $col, $mat) &&                    !isZombie($i - 1, $j, $row, $col, $mat) &&                    !isZombie($i - 1, $j + 1, $row, $col, $mat) &&                     !isZombie($i, $j - 1, $row, $col, $mat) &&                     !isZombie($i, $j, $row, $col, $mat) &&                    !isZombie($i, $j + 1, $row, $col, $mat) &&                    !isZombie($i + 1, $j - 1, $row, $col, $mat) &&                    !isZombie($i + 1, $j, $row, $col, $mat) &&                     !isZombie($i + 1, $j + 1, $row, $col, $mat))                 {                     $count++;                 }             }         }     }     return $count; } Â
// Driver codeÂ
// Input matrix $mat = array( "**P*", "*Z**", "*Z**", "***P" ); Â
// Rows and columns of the matrix $row = sizeof($mat); $col = strlen($mat[0]); Â
// Total plants survived echo(Plant_Vs_Zombies($mat, $row, $col));Â
// This code is contributed by Code_Mech.?> |
Javascript
<script>Â
// Javascript implementation of the approach.Â
// Function that returns true if // mat[i][j] is a zombie function isZombie(i, j, r, c, mat) {     if (i < 0 || j < 0 || i >= r ||        j >= c || mat[i][j] != 'Z')         return false;        return true; }    // Function to return the count of plants // that survived from the zombies attack function Plant_Vs_Zombies(mat, row, col) {     let i, j, count = 0;        for(i = 0; i < row; i++)     {         for(j = 0; j < col; j++)        {                          // If current cell is a plant             if (mat[i][j] == 'P')             {                                  // If current plant is safe from zombies                 if (!isZombie(i - 1, j - 1, row, col, mat) &&                     !isZombie(i - 1, j, row, col, mat) &&                     !isZombie(i - 1, j + 1, row, col, mat) &&                    !isZombie(i, j - 1, row, col, mat) &&                     !isZombie(i, j, row, col, mat) &&                     !isZombie(i, j + 1, row, col, mat) &&                     !isZombie(i + 1, j - 1, row, col, mat) &&                     !isZombie(i + 1, j, row, col, mat) &&                     !isZombie(i + 1, j + 1, row, col, mat))                 {                     count++;                 }             }         }     }     return count; } Â
// Driver codeÂ
// Input matrix let mat = [ "**P*", "*Z**", "*Z**", "***P" ]; Â
// Rows and columns of the matrix let row = mat.length; let col = mat[0].length; Â
// Total plants survived document.write(Plant_Vs_Zombies(mat, row, col));Â
// This code is contributed by mukesh07Â
</script> |
1
Complexity Analysis:
- Time Complexity: O(N×N)
- Auxiliary Space: O(1)
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!
