
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
Bomb Enemy in C++
Suppose we have a 2D grid, here each cell is either a wall 'W', an enemy 'E' or that is empty '0', We have to find the maximum enemies we can kill using one bomb. The bomb kills all the enemies in the same row and column from the planted point until it hits the wall. And we can put bombs only on blank spaces.
So, if the input is like
then the output will be 3, as placing bomb at the green place, it will kill three enemies.
To solve this, we will follow these steps −
ret := 0
n := row count of grid, m := column count of grid
Define an array colCnt of size m
for initialize i := 0, when i < n, update (increase i by 1), do −
for initialize j := 0, when j < m, update (increase j by 1), do −
if j is zero or grid[i, j] is same as 'W', then −
rowCnt := 0
if grid[i, j] is same as 'W', then −
k := j + 1
Otherwise
k := j
for k < m and grid[i, k] is not equal to 'W', update (increase k by 1), do −
rowCnt := rowCnt + 1 when (grid[i, k] is 'E'), otherwise 0
if i is zero or grid[i, j] is same as 'W', then −
colCnt[j] := 0
if grid[i, j] is same as 'W', then −
k := i + 1
Otherwise
k := i
for k < n and grid[k, j] is not equal to 'W', update (increase k by 1), do −
colCnt[j] := colCnt[j] + 1 when (grid[k, j] is 'E') otherwise 0
if grid[i, j] is same as '0', then −
ret := maximum of ret and rowCnt + colCnt[j]
return ret
Example
Let us see the following implementation to get better understanding −
#include <bits/stdc++.h> using namespace std; class Solution { public: int maxKilledEnemies(vector<vector<char>>& grid) { int ret = 0; int n = grid.size(); int m = n ? grid[0].size() : 0; int rowCnt = 0; vector<int< colCnt(m); for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { if (!j || grid[i][j] == 'W') { rowCnt = 0; int k; if (grid[i][j] == 'W') k = j + 1; else k = j; for (; k < m && grid[i][k] != 'W'; k++) { rowCnt += (grid[i][k] == 'E'); } } if (!i || grid[i][j] == 'W') { colCnt[j] = 0; int k; if (grid[i][j] == 'W') k = i + 1; else k = i; for (; k < n && grid[k][j] != 'W'; k++) { colCnt[j] += (grid[k][j] == 'E'); } } if (grid[i][j] == '0') { ret = max(ret, rowCnt + colCnt[j]); } } } return ret; } }; main(){ Solution ob; vector<vector<char>> v = {{'0','E','0','0'},{'E','0','W','E'},{'0','E','0','0'}}; cout << (ob.maxKilledEnemies(v)); }
Input
{{'0','E','0','0'},{'E','0','W','E'},{'0','E','0','0'}}
Output
3
- Related Articles
- Killing Enemy in JavaScript
- What is a Zip Bomb (aka Decompression Bomb)?
- C vs BASH Fork bomb?
- C vs BASH Fork bomb in C/C++?
- Prevent fork bomb by limiting user process in linux
- Name the type of nuclear reaction which is involved in the working of:(a) a hydrogen bomb.(b) an atom bomb.
- What is Fork Bomb, aka Rabbit Virus?
- C++ code to find minimum moves with weapons to kill enemy
- Program to decrypt code to defuse the bomb in Python
- An uncontrolled nuclear chain reaction forms the basis of:(a) nuclear power plant (b) hydrogen bomb (c) thermal power station (d) atom bomb
- Program to find number of places are safe when bomb explodes in Python?
- C++ Program to find out the minimum number of operations required to defeat an enemy
- Program to find maximum number enemies will be killed to place a bomb in C++?
- Program to find a path a continuous path in a rectangular area without engaging a bomb in Python
- (a) What is the source of energy of the sun and other stars?(b) Describe the working of a hydrogen bomb. (c) What is common between the sun and a hydrogen bomb?
