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 java.util.*;
2
3public class SafestPathFinder {
4 private static final int[][] directions = {{0, 1}, {1, 0}, {0, -1}, {-1, 0}};
5
6 public int maxSafenessFactor(int[][] grid) {
7 int n = grid.length;
8 int[][] distance = new int[n][n];
9 for (int[] row : distance) Arrays.fill(row, Integer.MAX_VALUE);
10 Queue<int[]> queue = new LinkedList<>();
11
12 // BFS to calculate distance to the nearest thief
13 for (int r = 0; r < n; r++) {
14 for (int c = 0; c < n; c++) {
15 if (grid[r][c] == 1) {
16 queue.offer(new int[]{r, c});
17 distance[r][c] = 0;
18 }
19 }
20 }
21
22 while (!queue.isEmpty()) {
23 int[] pos = queue.poll();
24 int r = pos[0], c = pos[1];
25 for (int[] dir : directions) {
26 int nr = r + dir[0], nc = c + dir[1];
27 if (nr >= 0 && nr < n && nc >= 0 && nc < n && distance[nr][nc] == Integer.MAX_VALUE) {
28 distance[nr][nc] = distance[r][c] + 1;
29 queue.offer(new int[]{nr, nc});
30 }
31 }
32 }
33
34 // Max-heap-like Dijkstra's for the safest path
35 PriorityQueue<int[]> pq = new PriorityQueue<>((a, b) -> b[2] - a[2]); // Max-heap based on safeness factor
36 pq.offer(new int[]{0, 0, distance[0][0]});
37 boolean[][] visited = new boolean[n][n];
38
39 while (!pq.isEmpty()) {
40 int[] cur = pq.poll();
41 int r = cur[0], c = cur[1], safeness = cur[2];
42
43 if (r == n - 1 && c == n - 1) return safeness;
44 visited[r][c] = true;
45
46 for (int[] dir : directions) {
47 int nr = r + dir[0], nc = c + dir[1];
48 if (nr >= 0 && nr < n && nc >= 0 && nc < n && !visited[nr][nc]) {
49 int newSafeness = Math.min(safeness, distance[nr][nc]);
50 pq.offer(new int[]{nr, nc, newSafeness});
51 }
52 }
53 }
54
55 return 0;
56 }
57
58 public static void main(String[] args) {
59 SafestPathFinder spf = new SafestPathFinder();
60 int[][] grid = {{0, 0, 1}, {0, 0, 0}, {0, 0, 0}};
61 System.out.println(spf.maxSafenessFactor(grid)); // Output: 2
62 }
63}
64
The Java implementation uses BFS to compute how far each cell is from the closest thief, followed by priority-based processing to ensure optimal safeness to the target using Dijkstra's inspired strategy.
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.