Given a Character matrix as input, the task is to check whether all the enemies are killed or not based on below conditions:
1. The matrix can contain 3 characters
X –> Denotes the War area.
B –> Denotes the bomb.
E –> Denotes the Enemies.2. Bomb ‘B’ can blast in only horizontal and vertical directions from one end to another.
3. If all enemies are killed by the present bombs, print Yes, else print No
Examples:
Input: matrix = XXEX XBXX XEXX XXBX Output: Yes Input: matrix = XXEX XBXX XEXX XXXX Output: No
Approach: The given problem can be solved by the following approach:
- Get the character Matrix
- Traverse to find all bomb indices in the matrix
- For each bomb found, see if any enemy is present in its vertical or horizontal direction. If present kill that enemy, i.e. change E to X
- After all traversals, check if any enemy is present in the matrix or not.
- Print Yes if all enemies are killed, else print No.
Implementation:
C++
// C++ programt to kill all enemies #include <iostream> using namespace std; // Function to find Enemies killed or not int Kill_Enemy(string s[], int row, int col) { int i, j, x, y; // Loop to evaluate the Bomb for (i = 0; i < row; i++) { for (j = 0; j < col; j++) { // Check if this index is a bomb if (s[i][j] == 'B') { // Kill all enemies // in horizontal direction for (x = 0; x < row; x++) { if (s[x][j] != 'B') s[x][j] = 'X'; } // Kill all enemies // in vertical direction for (y = 0; y < col; y++) { if (s[i][y] != 'B') s[i][y] = 'X'; } } } } // All bombs have been found // Check if any enemy is still present for (i = 0; i < row; i++) { for (j = 0; j < col; j++) { if (s[i][j] == 'E') // Since an enemy is present // Return 0 denoting No as output return 0; } } // Since all enemies are killed // Return 1 denoting Yes as output return 1; } // Driver Code int main(int argc, char** argv) { // Get the input matrix string s[] = { "XXEX", "XBXX", "XEXX", "XXBX" }; // Calculate Rows and columns of the string int row = sizeof(s) / sizeof(s[0]), col = s[0].length(); // Check if all enemies will be killed or not if (Kill_Enemy(s, row, col) == 1) cout << "Yes"; else cout << "No"; return 0; } |
Java
// Java programt to kill all enemies class GFG { // Function to find Enemies killed or not static int Kill_Enemy(char [][]s, int row, int col) { int i, j, x, y; // Loop to evaluate the Bomb for (i = 0; i < row; i++) { for (j = 0; j < col; j++) { // Check if this index is a bomb if (s[i][j] == 'B') { // Kill all enemies // in horizontal direction for (x = 0; x < row; x++) { if (s[x][j] != 'B') s[x][j] = 'X'; } // Kill all enemies // in vertical direction for (y = 0; y < col; y++) { if (s[i][y] != 'B') s[i][y] = 'X'; } } } } // All bombs have been found // Check if any enemy is still present for (i = 0; i < row; i++) { for (j = 0; j < col; j++) { if (s[i][j] == 'E') // Since an enemy is present // Return 0 denoting No as output return 0; } } // Since all enemies are killed // Return 1 denoting Yes as output return 1; } // Driver Code public static void main(String[] args) { // Get the input matrix char [][]s = { "XXEX".toCharArray(), "XBXX".toCharArray(), "XEXX".toCharArray(), "XXBX".toCharArray() }; // Calculate Rows and columns of the String int row = s.length, col = s[0].length; // Check if all enemies will be killed or not if (Kill_Enemy(s, row, col) == 1) System.out.print("Yes"); else System.out.print("No"); } } // This code is contributed by Rajput-Ji |
Python3
# Python3 program to kill all enemies # Function to find Enemies killed or not def Kill_Enemy(s, row, col): i, j, x, y = 0, 0, 0, 0; # Loop to evaluate the Bomb for i in range(row): for j in range(col): # Check if this index is a bomb if (s[i][j] == 'B'): # Kill all enemies # in horizontal direction for x in range(row): if (s[x][j] != 'B'): s[x][j] = 'X'; # Kill all enemies # in vertical direction for y in range(col): if (s[i][y] != 'B'): s[i][y] = 'X'; # All bombs have been found # Check if any enemy is still present for i in range(row): for j in range(col): if (s[i][j] == 'E'): # Since an enemy is present # Return 0 denoting No as output return 0; # Since all enemies are killed # Return 1 denoting Yes as output return 1; # Driver Code if __name__ == '__main__': # Get the input matrix s = [['X','X','E','X'], ['X','B','X','X'], ['X','E','X','X'], ['X','X','B','X']] # Calculate Rows and columns of the String row = len(s); col = len(s[0]); # Check if all enemies will be killed or not if (Kill_Enemy(s, row, col) == 1): print("Yes"); else: print("No"); # This code is contributed by Rajput-Ji |
C#
// C# program to kill all enemies using System; class GFG { // Function to find Enemies killed or not static int Kill_Enemy(char [,]s, int row, int col) { int i, j, x, y; // Loop to evaluate the Bomb for (i = 0; i < row; i++) { for (j = 0; j < col; j++) { // Check if this index is a bomb if (s[i, j] == 'B') { // Kill all enemies // in horizontal direction for (x = 0; x < row; x++) { if (s[x, j] != 'B') s[x, j] = 'X'; } // Kill all enemies // in vertical direction for (y = 0; y < col; y++) { if (s[i, y] != 'B') s[i, y] = 'X'; } } } } // All bombs have been found // Check if any enemy is still present for (i = 0; i < row; i++) { for (j = 0; j < col; j++) { if (s[i, j] == 'E') // Since an enemy is present // Return 0 denoting No as output return 0; } } // Since all enemies are killed // Return 1 denoting Yes as output return 1; } // Driver Code public static void Main(String[] args) { // Get the input matrix char [,]s = {{'X', 'B', 'X', 'X'}, {'X', 'E', 'X', 'X'}, {'X', 'X', 'B', 'X'}}; // Calculate Rows and columns of the String int row = s.GetLength(0), col = s.GetLength(1); // Check if all enemies will be killed or not if (Kill_Enemy(s, row, col) == 1) Console.Write("Yes"); else Console.Write("No"); } } // This code is contributed by Rajput-Ji |
Yes
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
Recommended Posts:
- Check if matrix can be converted to another matrix by transposing square sub-matrices
- Program to check diagonal matrix and scalar matrix
- Check if a given matrix can be converted to another given matrix by row and column exchanges
- Check if it is possible to make the given matrix increasing matrix or not
- Program to check if a matrix is Binary matrix or not
- Check if a Matrix is Bitonic or not
- Check whether a given matrix is orthogonal or not
- Check if a Matrix is Invertible
- Check for possible path in 2D matrix
- Check if Matrix sum is prime or not
- Check if the Matrix follows the given constraints or not
- Check whether the given Matrix is balanced or not
- Check if a given matrix is Hankel or not
- Check if a given matrix is sparse or not
- Program to check idempotent matrix
- Check if it is possible to create a matrix such that every row has A 1s and every column has B 1s
- Program to check Involutory Matrix
- Program to check if a matrix is symmetric
- Check if possible to cross the matrix with given power
- Check if sums of i-th row and i-th column are same in matrix
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 : Rajput-Ji

