Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Program to find minimum number of cells it will take to reach bottom right corner in Python
Suppose we have a 2D grid representing a maze where 0 represents an empty space and 1 represents a wall. We start at grid[0][0] and need to find the minimum number of cells it takes to reach the bottom-right corner. If we cannot reach the destination, we return -1.
Problem Example
Consider this grid ?
| 0 | 0 | 0 |
| 1 | 0 | 0 |
| 1 | 0 | 0 |
The output will be 5 because the shortest path requires 5 steps.
Algorithm Steps
We use Breadth-First Search (BFS) to find the shortest path ?
- Get the dimensions: R = number of rows, C = number of columns
- Initialize a queue with starting position (0, 0, 1) if the starting cell is empty
- Mark the starting cell as visited by setting it to 1
- For each cell in the queue, check if we've reached the bottom-right corner
- Explore all 4 adjacent cells (up, down, left, right)
- Add valid unvisited cells to the queue with incremented distance
- Return -1 if no path is found
Implementation
class Solution:
def solve(self, grid):
rows, cols = len(grid), len(grid[0])
# If starting cell is blocked, return -1
if grid[0][0] == 1:
return -1
# Queue stores (row, col, distance)
queue = [(0, 0, 1)]
grid[0][0] = 1 # Mark as visited
for row, col, distance in queue:
# Check if we reached the destination
if (row, col) == (rows - 1, cols - 1):
return distance
# Explore all 4 directions: down, up, right, left
for next_row, next_col in [(row + 1, col), (row - 1, col), (row, col + 1), (row, col - 1)]:
# Check if the cell is valid and unvisited
if 0 <= next_row < rows and 0 <= next_col < cols and grid[next_row][next_col] == 0:
grid[next_row][next_col] = 1 # Mark as visited
queue.append((next_row, next_col, distance + 1))
return -1 # No path found
# Test the solution
solution = Solution()
grid = [
[0, 0, 0],
[1, 0, 0],
[1, 0, 0]
]
print(solution.solve(grid))
5
How It Works
The algorithm uses BFS to explore the grid level by level, ensuring we find the shortest path. We mark visited cells as 1 to avoid revisiting them. The queue maintains cells to visit along with their distance from the starting point.
Example Walkthrough
For the given grid, the path would be ?
- Start at (0,0) - distance 1
- Move to (0,1) - distance 2
- Move to (0,2) - distance 3
- Move to (1,2) - distance 4
- Move to (2,2) - distance 5 (destination reached)
Edge Cases
solution = Solution()
# Test case 1: Starting cell is blocked
blocked_start = [[1, 0], [0, 0]]
print("Blocked start:", solution.solve(blocked_start))
# Test case 2: No path available
no_path = [[0, 1], [1, 0]]
print("No path:", solution.solve(no_path))
# Test case 3: Single cell grid
single_cell = [[0]]
print("Single cell:", solution.solve(single_cell))
Blocked start: -1 No path: -1 Single cell: 1
Conclusion
This BFS approach guarantees finding the minimum number of cells to reach the destination. The time complexity is O(rows × cols) and space complexity is O(rows × cols) for the queue storage.
