Check if all enemies are killed with bombs placed in a matrix
Last Updated :
15 Sep, 2022
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
- And store the rows and column in rs and cls array.
- After all traversals, check for each enemy if there is a bomb present in that row or column or not.
- If any enemy is present in a row or column where there is no bomb, Print NO, else Print Yes.
Implementation:
C++
#include <iostream>
using namespace std;
int Kill_Enemy(string s[], int row, int col)
{
int rs[row]={0},cls[col]={0};
for ( int i=0;i<row;i++){
for ( int j=0;j<col;j++){
if (s[i][j]== 'B' ){
rs[i]++;cls[j]++;
}
}
}
for ( int i=0;i<row;i++){
for ( int j=0;j<col;j++){
if (s[i][j]== 'E' ){
if (rs[i]==0&&cls[j]==0) return 0;
}
}
}
return 1;
}
int main( int argc, char ** argv)
{
string s[] = { "XXEX" ,
"XBXX" ,
"XEXX" ,
"XXBX" };
int row = sizeof (s) / sizeof (s[0]),
col = s[0].length();
if (Kill_Enemy(s, row, col) == 1)
cout << "Yes" ;
else
cout << "No" ;
return 0;
}
|
Java
class GFG
{
static int Kill_Enemy( char [][]s, int row, int col)
{
int i, j, x, y;
for (i = 0 ; i < row; i++)
{
for (j = 0 ; j < col; j++)
{
if (s[i][j] == 'B' )
{
for (x = 0 ; x < row; x++)
{
if (s[x][j] != 'B' )
s[x][j] = 'X' ;
}
for (y = 0 ; y < col; y++)
{
if (s[i][y] != 'B' )
s[i][y] = 'X' ;
}
}
}
}
for (i = 0 ; i < row; i++)
{
for (j = 0 ; j < col; j++)
{
if (s[i][j] == 'E' )
return 0 ;
}
}
return 1 ;
}
public static void main(String[] args)
{
char [][]s = { "XXEX" .toCharArray(),
"XBXX" .toCharArray(),
"XEXX" .toCharArray(),
"XXBX" .toCharArray() };
int row = s.length,
col = s[ 0 ].length;
if (Kill_Enemy(s, row, col) == 1 )
System.out.print( "Yes" );
else
System.out.print( "No" );
}
}
|
Python3
def Kill_Enemy(s, row, col):
i, j, x, y = 0 , 0 , 0 , 0 ;
for i in range (row):
for j in range (col):
if (s[i][j] = = 'B' ):
for x in range (row):
if (s[x][j] ! = 'B' ):
s[x][j] = 'X' ;
for y in range (col):
if (s[i][y] ! = 'B' ):
s[i][y] = 'X' ;
for i in range (row):
for j in range (col):
if (s[i][j] = = 'E' ):
return 0 ;
return 1 ;
if __name__ = = '__main__' :
s = [[ 'X' , 'X' , 'E' , 'X' ],
[ 'X' , 'B' , 'X' , 'X' ],
[ 'X' , 'E' , 'X' , 'X' ],
[ 'X' , 'X' , 'B' , 'X' ]]
row = len (s);
col = len (s[ 0 ]);
if (Kill_Enemy(s, row, col) = = 1 ):
print ( "Yes" );
else :
print ( "No" );
|
C#
using System;
class GFG
{
static int Kill_Enemy( char [,]s,
int row, int col)
{
int i, j, x, y;
for (i = 0; i < row; i++)
{
for (j = 0; j < col; j++)
{
if (s[i, j] == 'B' )
{
for (x = 0; x < row; x++)
{
if (s[x, j] != 'B' )
s[x, j] = 'X' ;
}
for (y = 0; y < col; y++)
{
if (s[i, y] != 'B' )
s[i, y] = 'X' ;
}
}
}
}
for (i = 0; i < row; i++)
{
for (j = 0; j < col; j++)
{
if (s[i, j] == 'E' )
return 0;
}
}
return 1;
}
public static void Main(String[] args)
{
char [,]s = {{ 'X' , 'B' , 'X' , 'X' },
{ 'X' , 'E' , 'X' , 'X' },
{ 'X' , 'X' , 'B' , 'X' }};
int row = s.GetLength(0),
col = s.GetLength(1);
if (Kill_Enemy(s, row, col) == 1)
Console.Write( "Yes" );
else
Console.Write( "No" );
}
}
|
Javascript
<script>
function Kill_Enemy(s, row, col)
{
let i, j, x, y;
for (i = 0; i < row; i++)
{
for (j = 0; j < col; j++)
{
if (s[i][j] == 'B' )
{
for (x = 0; x < row; x++)
{
if (s[x][j] != 'B' )
s[x][j] = 'X' ;
}
for (y = 0; y < col; y++)
{
if (s[i][y] != 'B' )
s[i][y] = 'X' ;
}
}
}
}
for (i = 0; i < row; i++)
{
for (j = 0; j < col; j++)
{
if (s[i][j] == 'E' )
return 0;
}
}
return 1;
}
let s = [ "XXEX" .split( '' ),
"XBXX" .split( '' ),
"XEXX" .split( '' ),
"XXBX" .split( '' ) ];
let row = s.length, col = s[0].length;
if (Kill_Enemy(s, row, col) == 1)
document.write( "Yes" );
else
document.write( "No" );
</script>
|
Complexity Analysis:
- Time Complexity: O(M × N) // where M is the number of rows and N is the number of columns
- Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...