|
| 1 | +--- |
| 2 | +id: maximum-safeness-factor-of-a-path |
| 3 | +title: Maximum Safeness Factor of a Path |
| 4 | +sidebar_label: Maximum Safeness Factor of a Path |
| 5 | +tags: |
| 6 | + - Grid |
| 7 | + - Pathfinding |
| 8 | + - BFS |
| 9 | + - Dijkstra |
| 10 | +description: "This is a solution to the Maximum Safeness Factor of a Path problem." |
| 11 | +--- |
| 12 | + |
| 13 | +## Problem Description |
| 14 | + |
| 15 | +You are given a 0-indexed 2D matrix `grid` of size `n x n`, where: |
| 16 | + |
| 17 | +- `grid[r][c] = 1` represents a cell containing a thief. |
| 18 | +- `grid[r][c] = 0` represents an empty cell. |
| 19 | + |
| 20 | +You are initially positioned at cell `(0, 0)`. In one move, you can move to any adjacent cell in the grid, including cells containing thieves. |
| 21 | + |
| 22 | +The safeness factor of a path on the grid is defined as the minimum Manhattan distance from any cell in the path to any thief in the grid. |
| 23 | + |
| 24 | +Return the maximum safeness factor of all paths leading to cell `(n - 1, n - 1)`. |
| 25 | + |
| 26 | +An adjacent cell of cell `(r, c)` is one of the cells `(r, c + 1)`, `(r, c - 1)`, `(r + 1, c)`, and `(r - 1, c)` if it exists. |
| 27 | + |
| 28 | +The Manhattan distance between two cells `(a, b)` and `(x, y)` is equal to `|a - x| + |b - y|`, where `|val|` denotes the absolute value of `val`. |
| 29 | + |
| 30 | +### Examples |
| 31 | + |
| 32 | +**Example 1:** |
| 33 | + |
| 34 | +``` |
| 35 | +Input: grid = [[1,0,0],[0,0,0],[0,0,1]] |
| 36 | +Output: 0 |
| 37 | +Explanation: All paths from (0, 0) to (n - 1, n - 1) go through the thieves in cells (0, 0) and (n - 1, n - 1). |
| 38 | +``` |
| 39 | + |
| 40 | +**Example 2:** |
| 41 | + |
| 42 | +``` |
| 43 | +Input: grid = [[0,0,1],[0,0,0],[0,0,0]] |
| 44 | +Output: 2 |
| 45 | +Explanation: The path depicted in the picture above has a safeness factor of 2 since: |
| 46 | +- The closest cell of the path to the thief at cell (0, 2) is cell (0, 0). The distance between them is | 0 - 0 | + | 0 - 2 | = 2. |
| 47 | +It can be shown that there are no other paths with a higher safeness factor. |
| 48 | +``` |
| 49 | + |
| 50 | +**Example 3:** |
| 51 | + |
| 52 | +``` |
| 53 | +Input: grid = [[0,0,0,1],[0,0,0,0],[0,0,0,0],[1,0,0,0]] |
| 54 | +Output: 2 |
| 55 | +Explanation: The path depicted in the picture above has a safeness factor of 2 since: |
| 56 | +- The closest cell of the path to the thief at cell (0, 3) is cell (1, 2). The distance between them is | 0 - 1 | + | 3 - 2 | = 2. |
| 57 | +- The closest cell of the path to the thief at cell (3, 0) is cell (3, 2). The distance between them is | 3 - 3 | + | 0 - 2 | = 2. |
| 58 | +It can be shown that there are no other paths with a higher safeness factor. |
| 59 | +``` |
| 60 | + |
| 61 | +### Constraints |
| 62 | + |
| 63 | +- The number of nodes in the grid is in the range `[1, 400]`. |
| 64 | +- `grid[i][j]` is either 0 or 1. |
| 65 | +- There is at least one thief in the grid. |
| 66 | + |
| 67 | +### Approach |
| 68 | + |
| 69 | +To solve this problem, we will first use a multi-source Breadth-First Search (BFS) to calculate the minimum distance from each cell to the nearest thief. Then, we will use a modified Dijkstra's algorithm to find the path from `(0, 0)` to `(n - 1, n - 1)` that maximizes the minimum safeness factor. |
| 70 | + |
| 71 | +#### Code in Python |
| 72 | + |
| 73 | +```python |
| 74 | +import heapq |
| 75 | +from collections import deque |
| 76 | + |
| 77 | +class Solution: |
| 78 | + def maximumSafenessFactor(self, grid): |
| 79 | + n = len(grid) |
| 80 | + |
| 81 | + # Step 1: Calculate distance to nearest thief for each cell using multi-source BFS |
| 82 | + distance = [[float('inf')] * n for _ in range(n)] |
| 83 | + q = deque() |
| 84 | + |
| 85 | + # Initialize the BFS queue with all thieves' positions |
| 86 | + for r in range(n): |
| 87 | + for c in range(n): |
| 88 | + if grid[r][c] == 1: |
| 89 | + q.append((r, c)) |
| 90 | + distance[r][c] = 0 |
| 91 | + |
| 92 | + # Directions for moving up, down, left, and right |
| 93 | + directions = [(-1, 0), (1, 0), (0, -1), (0, 1)] |
| 94 | + |
| 95 | + while q: |
| 96 | + r, c = q.popleft() |
| 97 | + for dr, dc in directions: |
| 98 | + nr, nc = r + dr, c + dc |
| 99 | + if 0 <= nr < n and 0 <= nc < n and distance[nr][nc] == float('inf'): |
| 100 | + distance[nr][nc] = distance[r][c] + 1 |
| 101 | + q.append((nr, nc)) |
| 102 | + |
| 103 | + # Step 2: Use modified Dijkstra's algorithm to find the path with the maximum safeness factor |
| 104 | + max_heap = [(-distance[0][0], 0, 0)] # Max-heap (negative distance to simulate max-heap in python) |
| 105 | + visited = set() |
| 106 | + |
| 107 | + while max_heap: |
| 108 | + min_dist, r, c = heapq.heappop(max_heap) |
| 109 | + min_dist = -min_dist # Convert back to positive |
| 110 | + |
| 111 | + if (r, c) == (n-1, n-1): |
| 112 | + return min_dist |
| 113 | + |
| 114 | + if (r, c) in visited: |
| 115 | + continue |
| 116 | + visited.add((r, c)) |
| 117 | + |
| 118 | + for dr, dc in directions: |
| 119 | + nr, nc = r + dr, c + dc |
| 120 | + if 0 <= nr < n and 0 <= nc < n and (nr, nc) not in visited: |
| 121 | + heapq.heappush(max_heap, (-min(distance[nr][nc], min_dist), nr, nc)) |
| 122 | + |
| 123 | + return 0 # Fallback, though problem guarantees a path |
| 124 | + |
| 125 | +# Example test cases |
| 126 | +sol = Solution() |
| 127 | +grid1 = [[1,0,0],[0,0,0],[0,0,1]] |
| 128 | +print(sol.maximumSafenessFactor(grid1)) # Output: 0 |
| 129 | + |
| 130 | +grid2 = [[0,0,1],[0,0,0],[0,0,0]] |
| 131 | +print(sol.maximumSafenessFactor(grid2)) # Output: 2 |
| 132 | + |
| 133 | +grid3 = [[0,0,0,1],[0,0,0,0],[0,0,0,0],[1,0,0,0]] |
| 134 | +print(sol.maximumSafenessFactor(grid3)) # Output: 2 |
| 135 | +``` |
| 136 | +``` |
0 commit comments