This approach involves using Breadth-First Search (BFS) to precompute the minimum Manhattan distance from each cell to any thief in the grid, and then using this information to find the maximum safeness factor for reaching the bottom-right corner.
Time Complexity: O(n2 log n), where n is the grid size, due to the Dijkstra-like process.
Space Complexity: O(n2) for storing distances and safeness factors.
1import heapq
2
3class SafestPathFinder:
4 def maxSafenessFactor(self, grid):
5 n = len(grid)
6 directions = [(0, 1), (1, 0), (0, -1), (-1, 0)]
7 distance = [[float('inf')] * n for _ in range(n)]
8 q = []
9
10 # BFS from all thieves
11 for r in range(n):
12 for c in range(n):
13 if grid[r][c] == 1:
14 q.append((r, c))
15 distance[r][c] = 0
16
17 for r, c in q:
18 for dr, dc in directions:
19 nr, nc = r + dr, c + dc
20 if 0 <= nr < n and 0 <= nc < n and distance[nr][nc] == float('inf'):
21 distance[nr][nc] = distance[r][c] + 1
22 q.append((nr, nc))
23
24 # Max-safeness factor using a max-heap
25 heap = [(-distance[0][0], 0, 0)] # store as negative for max-heap behavior
26 maxSafeness = [[-1] * n for _ in range(n)]
27 maxSafeness[0][0] = distance[0][0]
28
29 while heap:
30 safeness, r, c = heapq.heappop(heap)
31 safeness = -safeness
32
33 for dr, dc in directions:
34 nr, nc = r + dr, c + dc
35 if 0 <= nr < n and 0 <= nc < n:
36 newSafeness = min(safeness, distance[nr][nc])
37 if newSafeness > maxSafeness[nr][nc]:
38 maxSafeness[nr][nc] = newSafeness
39 heapq.heappush(heap, (-newSafeness, nr, nc))
40
41 return maxSafeness[n-1][n-1]
42
43# Example usage
44spf = SafestPathFinder()
45grid = [[0, 0, 1], [0, 0, 0], [0, 0, 0]]
46print(spf.maxSafenessFactor(grid)) # Output: 2
47
The Python solution uses BFS to precompute distances from all thieves. Then, a max-heap (prioritizing higher safeness) guides traversal from the start to the end, ensuring the net max safeness factor is achieved.
This approach considers processing from both the starting and ending points in a bidirectional BFS style, potentially meeting in the middle for optimized distance calculations and safeness factor determination.
Time Complexity: O(n2) due to simultaneous BFS processing.
Space Complexity: O(n2) as distances from both ends are computed.
1#include <stdio.h>
2#include <stdlib.h>
3#include <limits.h>
4#include <stdbool.h>
5#include <string.h>
6
7#define MAX 400
8
9int grid[MAX][MAX];
10int n;
11
12int direction[4][2] = {{0,1}, {1,0}, {0,-1}, {-1,0}};
13
14int bidirectionalSafenessFactor(int grid[][MAX], int n) {
15 // Distance from start and end
16 int distanceStart[MAX][MAX];
17 int distanceEnd[MAX][MAX];
18
19 memset(distanceStart, INT_MAX, sizeof(distanceStart));
20 memset(distanceEnd, INT_MAX, sizeof(distanceEnd));
21
22 // BFS start queue
23 int queueStart[MAX*MAX][2];
24 int frontStart = 0, rearStart = 0;
25
26 queueStart[rearStart][0] = 0;
27 queueStart[rearStart][1] = 0;
28 rearStart++;
29 distanceStart[0][0] = 0;
30
31 // BFS end queue
32 int queueEnd[MAX*MAX][2];
33 int frontEnd = 0, rearEnd = 0;
34
35 queueEnd[rearEnd][0] = n-1;
36 queueEnd[rearEnd][1] = n-1;
37 rearEnd++;
38 distanceEnd[n-1][n-1] = 0;
39
40 while (frontStart < rearStart && frontEnd < rearEnd) {
41 // Expand from start
42 int r = queueStart[frontStart][0], c = queueStart[frontStart][1];
43 frontStart++;
44
45 for (int d = 0; d < 4; ++d) {
46 int nr = r + direction[d][0], nc = c + direction[d][1];
47 if (nr >= 0 && nr < n && nc >= 0 && nc < n && distanceStart[nr][nc] == INT_MAX) {
48 distanceStart[nr][nc] = distanceStart[r][c] + 1;
49 queueStart[rearStart][0] = nr;
50 queueStart[rearStart][1] = nc;
51 rearStart++;
52 }
53 }
54
55 // Expand from end
56 r = queueEnd[frontEnd][0], c = queueEnd[frontEnd][1];
57 frontEnd++;
58
59 for (int d = 0; d < 4; ++d) {
60 int nr = r + direction[d][0], nc = c + direction[d][1];
61 if (nr >= 0 && nr < n && nc >= 0 && nc < n && distanceEnd[nr][nc] == INT_MAX) {
62 distanceEnd[nr][nc] = distanceEnd[r][c] + 1;
63 queueEnd[rearEnd][0] = nr;
64 queueEnd[rearEnd][1] = nc;
65 rearEnd++;
66 }
67 }
68
69 // Check overlap
70 for (int i = 0; i < n; ++i) {
71 for (int j = 0; j < n; ++j) {
72 if (distanceStart[i][j] != INT_MAX && distanceEnd[i][j] != INT_MAX) {
73 return distanceStart[i][j] + distanceEnd[i][j];
74 }
75 }
76 }
77 }
78
79 return -1;
80}
81
82int main() {
83 int gridExample[MAX][MAX] = {{0, 0, 1}, {0, 0, 0}, {0, 0, 0}};
84 int n = 3;
85 printf("%d\n", bidirectionalSafenessFactor(gridExample, n));
86 return 0;
87}
88
The C solution processes the grid using bidirectional BFS starting from both the top-left corner and the bottom-right corner simultaneously, aiming to explore overlapping paths and maximize their safeness factor.