
- 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
Program to find maximum number enemies will be killed to place a bomb in C++?
Suppose we have a 2D matrix of three different values, 2s, 1s, and 0s, where a 2 represents an enemy, 1 represents a wall and 0 represents an empty cell. 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 we can place the bomb at the green box to kill maximum 3 enemies.
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 1, then:
rowCnt := 0
if grid[i, j] is same as 1, then:
k := j + 1
Otherwise
k := j
for k < m and grid[i, k] is not equal to 1, update (increase k by 1), do:
rowCnt := rowCnt + 1 when (grid[i, k] is 2), otherwise 0
if i is zero or grid[i, j] is same as 1, then:
colCnt[j] := 0
if grid[i, j] is same as 1, then:
k := i + 1
Otherwise
k := i
for k < n and grid[k, j] is not equal to 1, update (increase k by 1), do:
colCnt[j] := colCnt[j] + 1 when (grid[k, j] is 2) otherwise 0
if grid[i, j] is same as 0, then:
ret := maximum of ret and rowCnt + colCnt[j]
return ret
Let us see the following implementation to get better understanding:
Example
#include <bits/stdc++.h> using namespace std; class Solution { public: int solve(vector<vector<int>>& 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] == 1) { rowCnt = 0; int k; if (grid[i][j] == 1) k = j + 1; else k = j; for (; k < m && grid[i][k] != 1; k++) { rowCnt += (grid[i][k] == 2); } } if (!i || grid[i][j] == 1) { colCnt[j] = 0; int k; if (grid[i][j] == 1) k = i + 1; else k = i; for (; k < n && grid[k][j] != 1; k++) { colCnt[j] += (grid[k][j] == 2); } } if (grid[i][j] == 0) { ret = max(ret, rowCnt + colCnt[j]); } } } return ret; } }; main(){ Solution ob; vector<vector<int>> v = { {0,2,0,0}, {2,0,1,2}, {0,2,0,0}}; cout << (ob.solve(v)); }
Input
{{0,2,0,0}, {2,0,1,2}, {0,2,0,0}}
Output
3
- Related Articles
- Program to find maximum number by adding 5 at any place in Python
- Check if all enemies are killed with bombs placed in a matrix in Python
- Maximum number of people that can be killed with strength P in C++
- Program to find number of places are safe when bomb explodes in Python?
- Program to find maximum number of package that can be bought by buyers in C++
- C++ program to find out the maximum number of cells that can be illuminated
- Program to find maximum number of balls in a box using Python
- Program to find maximum number of eaten apples in Python
- Program to find the maximum number in rotated list in C++
- Program to find maximum number of non-overlapping substrings in Python
- Program to find maximum units that can be put on a truck in Python
- Program to decrypt code to defuse the bomb in Python
- Program to find a path a continuous path in a rectangular area without engaging a bomb in Python
- C++ Program to Find Maximum Number of Edge Disjoint Paths
- Program to find number of columns flips to get maximum number of equal Rows in Python?
