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.
1class SafestPathFinder {
2 constructor() {
3 this.directions = [[0, 1], [1, 0], [0, -1], [-1, 0]];
4 }
5
6 bidirectionalSafenessFactor(grid) {
7 const n = grid.length;
8 const distFromStart = Array.from({ length: n }, () => Array(n).fill(Infinity));
9 const distFromEnd = Array.from({ length: n }, () => Array(n).fill(Infinity));
10
11 const startQueue = [[0, 0]];
12 distFromStart[0][0] = 0;
13
14 const endQueue = [[n - 1, n - 1]];
15 distFromEnd[n - 1][n - 1] = 0;
16
17 while (startQueue.length || endQueue.length) {
18 if (startQueue.length) {
19 const [r, c] = startQueue.shift();
20 for (const [dr, dc] of this.directions) {
21 const nr = r + dr, nc = c + dc;
22 if (nr >= 0 && nr < n && nc >= 0 && nc < n && distFromStart[nr][nc] === Infinity) {
23 distFromStart[nr][nc] = distFromStart[r][c] + 1;
24 startQueue.push([nr, nc]);
25 }
26 }
27 }
28
29 if (endQueue.length) {
30 const [r, c] = endQueue.shift();
31 for (const [dr, dc] of this.directions) {
32 const nr = r + dr, nc = c + dc;
33 if (nr >= 0 && nr < n && nc >= 0 && nc < n && distFromEnd[nr][nc] === Infinity) {
34 distFromEnd[nr][nc] = distFromEnd[r][c] + 1;
35 endQueue.push([nr, nc]);
36 }
37 }
38 }
39
40 // Check overlap
41 for (let i = 0; i < n; ++i) {
42 for (let j = 0; j < n; ++j) {
43 if (distFromStart[i][j] !== Infinity && distFromEnd[i][j] !== Infinity) {
44 return distFromStart[i][j] + distFromEnd[i][j];
45 }
46 }
47 }
48 }
49
50 return -1;
51 }
52}
53
54const spf = new SafestPathFinder();
55const grid = [[0, 0, 1], [0, 0, 0], [0, 0, 0]];
56console.log(spf.bidirectionalSafenessFactor(grid)); // Output: Safeness factor
57
The JavaScript interface executes bidirectional BFS starting from both ends, with simultaneous processing enhancing the invulnerability factor calculation through effective intersection detection.