
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
Check if all enemies are killed with bombs placed in a matrix in Python
Suppose we have a matrix mat. There are few different values as follows The cells of matrix can hold any of these 3 characters
- 0 for empty area.
- 1 for bomb.
- 2 for Enemies.
Now bomb can blast in only horizontal and vertical directions from one end to another. We have to check whether all enemies will die when bomb explodes or not.
So, if the input is like
0 | 0 | 2 | 0 |
0 | 1 | 0 | 0 |
0 | 2 | 0 | 0 |
0 | 0 | 1 | 0 |
then the output will be True, because bomb at place [1, 1] can fill enemy at place [2, 1] and enemy at [0, 2] will be killed by bomb placed at [3, 2].
To solve this, we will follow these steps −
- r := row count of mat
- c := col count of mat
- i := 0, j := 0, x := 0, y := 0
- for i in range 0 to r - 1, do
- for j in range 0 to c - 1, do
- if mat[i, j] is 1, then
- for x in range 0 to r - 1, do
- if mat[x, j] is not 1, then
- mat[x, j] := 0
- if mat[x, j] is not 1, then
- for y in range 0 to c - 1, do
- if mat[i, y] is not 1, then
- mat[i, y] := 0
- if mat[i, y] is not 1, then
- for x in range 0 to r - 1, do
- if mat[i, j] is 1, then
- for j in range 0 to c - 1, do
- for i in range 0 to r - 1, do
- for j in range 0 to c - 1, do
- if mat[i, j] is 2, then
- return False
- if mat[i, j] is 2, then
- for j in range 0 to c - 1, do
- return True
Let us see the following implementation to get better understanding −
Example
def solve(mat): r = len(mat) c = len(mat[0]) i, j, x, y = 0, 0, 0, 0 for i in range(r): for j in range(c): if mat[i][j] == 1: for x in range(r): if mat[x][j] != 1: mat[x][j] = 0 for y in range(c): if mat[i][y] != 1: mat[i][y] = 0 for i in range(r): for j in range(c): if mat[i][j] == 2: return False return True matrix = [ [0,0,2,0], [0,1,0,0], [0,2,0,0], [0,0,1,0] ] print(solve(matrix))
Input
[ [0,0,2,0], [0,1,0,0], [0,2,0,0], [0,0,1,0] ]
Output
True
- Related Articles
- Check if all rows of a matrix are circular rotations of each other in Python
- Program to find maximum number enemies will be killed to place a bomb in C++?
- Python - Check if all elements in a List are same
- Python - Check if all elements in a list are identical
- Python – Test if all elements are unique in columns of a Matrix
- Check if all array elements are distinct in Python
- Check if all bits of a number are set in Python
- Python - Check If All the Characters in a String Are Alphanumeric?
- Check if a matrix is symmetric using Python
- Check if a pair with given product exists in a Matrix in C++
- Check if all the 1s in a binary string are equidistant or not in Python
- Python - Check if all the values in a list are less than a given value
- Check if a string has all characters with same frequency with one variation allowed in Python
- Check if Matrix remains unchanged after row reversals in Python
- Python - Check if frequencies of all characters of a string are different

Advertisements