- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

# Program to find number of places are safe when bomb explodes in Python?

Suppose we have a 2d binary matrix, where 1 represents a bomb and 0 represents an empty cell. When a bomb explodes, all the spaces along on the same row and column are damaged. We have to find the number of spaces we can stand in to not get damaged.

So, if the input is like

1 | 1 | 0 |

0 | 0 | 0 |

0 | 0 | 0 |

then the output will be 2, as there are two spaces the bottom right cell and the middle right cell are safe.

To solve this, we will follow these steps:

r := a list of size same as row count of matrix and fill with false

c := a list of size same as column count of matrix and fill with false

for i in range 0 to row count of matrix - 1, do

for j in range 0 to column count of matrix - 1, do

if matrix[i, j] is same as 1, then

r[i] := True, c[j] := True

ct := 0

for i in range 0 to row count of matrix - 1, do

for j in range 0 to column count of matrix - 1, do

if r[i] is False and c[j] is False, then

ct := ct + 1

return ct

Let us see the following implementation to get better understanding:

## Example

class Solution: def solve(self, matrix): r = [False for i in range(len(matrix))] c = [False for i in range(len(matrix[0]))] for i in range(len(matrix)): for j in range(len(matrix[0])): if matrix[i][j] == 1: r[i] = True c[j] = True ct = 0 for i in range(len(matrix)): for j in range(len(matrix[0])): if r[i] == False and c[j] == False: ct += 1 return ct ob = Solution() matrix = [ [1, 1, 0], [0, 0, 0], [0, 0, 0] ] print(ob.solve(matrix))

## Input

[ [1, 1, 0], [0, 0, 0], [0, 0, 0] ]

## Output

2

- Related Questions & Answers
- Program to find maximum number enemies will be killed to place a bomb in C++?
- Program to find the product of three elements when all are unique in Python
- Program to decrypt code to defuse the bomb in Python
- Program to find number of good pairs in Python
- Program to find number of good triplets in Python
- Program to find number of distinct subsequences in Python
- Program to find number of squareful arrays in Python
- Program to find number of subsequence that are present inside word list in python
- Program to find nearest number of n where all digits are odd in python
- Program to find number of strictly increasing colorful candle sequences are there in Python
- Program to find maximum value of k for which we can maintain safe distance in Python
- Python program to find better divisor of a number
- Python program to find factorial of a large number
- Program to find winner of number reducing game in Python
- Program to find maximum number of eaten apples in Python