给定一个字符矩阵作为输入,任务是根据以下条件检查所有敌人是否被杀死:
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
例子:
Input: matrix =
XXEX
XBXX
XEXX
XXBX
Output: Yes
Input: matrix =
XXEX
XBXX
XEXX
XXXX
Output: No
方法:可以通过以下方法解决给定的问题:
- 获取字符矩阵
- 遍历以找到矩阵中的所有炸弹指数
- 对于找到的每枚炸弹,请查看其垂直或水平方向上是否存在任何敌人。如果存在,则杀死该敌人,即将E更改为X
- 在所有遍历之后,检查矩阵中是否存在任何敌人。
- 如果所有敌人都被杀死,则打印“是”,否则打印“否”。
执行:
C++
// C++ program to kill all enemies
#include
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 program 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
想要从精选的最佳视频中学习并解决问题,请查看有关从基础到高级C++的C++基础课程以及有关语言和STL的C++ STL课程。要完成从学习语言到DS Algo等的更多准备工作,请参阅“完整面试准备课程” 。