Ad
matlazFailed Tests

Maze Runner

You are given a 2D grid representing a maze, where:

0 represents an open cell,
1 represents a wall,
S represents the starting point,
E represents the endpoint.

  • Your task is to find the shortest path from the starting point S to the endpoint E. You can move up, down, left, or right, but you cannot move through walls (1). If there is no path, return None.

Input:
A list of lists (2D grid) representing the maze.

Output:
A list of tuples representing the coordinates of the path from S to E, or None if no path exists.

def find_shortest_path(maze):
    pass

Problem Statement:

You are given a 2D grid (matrix) of integers where each cell represents a number. Your task is to find the maximum sum of a path from the top-left corner to the bottom-right corner of the grid. You can only move right or down at any point in time. However, there is a twist: certain cells are "blocked" and cannot be traversed. The blocked cells are represented by a special value (e.g., -1).

Requirements:

  1. Implement a function max_path_sum(grid: List[List[int]]) -> int that takes a 2D list of integers as input.
  2. The function should return the maximum sum of the path from the top-left corner to the bottom-right corner, or -1 if there is no valid path.
  3. The grid will contain:
  • Positive integers (representing valid cells).
  • "-1" (representing blocked cells).
  1. The grid will have at least one row and one column, and at most 100 rows and 100 columns.
  2. You must optimize your solution to handle the maximum grid size efficiently.
from typing import List

def max_path_sum(grid: List[List[int]]) -> int:
    if not grid or not grid[0]:
        return -1
    
    rows, cols = len(grid), len(grid[0])
    
    # Create a 2D list to store the maximum sums
    dp = [[-1] * cols for _ in range(rows)]
    
    # Initialize the starting point
    if grid[0][0] != -1:
        dp[0][0] = grid[0][0]
    
    # Fill the dp table
    for i in range(rows):
        for j in range(cols):
            if grid[i][j] == -1:
                continue  # Skip blocked cells
            
            # If we can come from the top
            if i > 0 and dp[i-1][j] != -1:
                dp[i][j] = max(dp[i][j], dp[i-1][j] + grid[i][j])
            
            # If we can come from the left
            if j > 0 and dp[i][j-1] != -1:
                dp[i][j] = max(dp[i][j], dp[i][j-1] + grid[i][j])
    
    # The result is in the bottom-right corner
    return dp[rows-1][cols-1]