/**
* Problem: LeetCode 2812 - Find the Safest Path in a Grid
* Difficulty: Medium
* Language: Java
* Author: CodeByTushu
*/
import java.util.*;
/**
* Problem: LeetCode 2812 - Find the Safest Path in a Grid
* Difficulty: Medium
* Language: Java
* Author: CodeByTushu
*/
class Solution {
private int n;
private int[][] dist;
private final int[][] dirs = {{1,0},{-1,0},{0,1},{0,-1}};
public int maximumSafenessFactor(List<List<Integer>> grid) {
n = grid.size();
dist = new int[n][n];
for (int[] row : dist) Arrays.fill(row, -1);
Queue<int[]> q = new LinkedList<>();
// Multi-source BFS from all thieves
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
if (grid.get(i).get(j) == 1) {
dist[i][j] = 0;
q.offer(new int[]{i, j});
}
}
}
while (!q.isEmpty()) {
int[] cur = q.poll();
for (int[] d : dirs) {
int x = cur[0] + d[0];
int y = cur[1] + d[1];
if (x >= 0 && x < n && y >= 0 && y < n && dist[x][y] == -1) {
dist[x][y] = dist[cur[0]][cur[1]] + 1;
q.offer(new int[]{x, y});
}
}
}
int low = 0, high = 2 * n;
while (low <= high) {
int mid = (low + high) / 2;
if (canReach(mid)) {
low = mid + 1;
} else {
high = mid - 1;
}
}
return high;
}
private boolean canReach(int limit) {
if (dist[0][0] < limit) return false;
boolean[][] vis = new boolean[n][n];
Queue<int[]> q = new LinkedList<>();
q.offer(new int[]{0, 0});
vis[0][0] = true;
while (!q.isEmpty()) {
int[] cur = q.poll();
if (cur[0] == n - 1 && cur[1] == n - 1) return true;
for (int[] d : dirs) {
int x = cur[0] + d[0];
int y = cur[1] + d[1];
if (x >= 0 && x < n && y >= 0 && y < n &&
!vis[x][y] && dist[x][y] >= limit) {
vis[x][y] = true;
q.offer(new int[]{x, y});
}
}
}
return false;
}
}
"""
Problem: LeetCode 2812 - Find the Safest Path in a Grid
Difficulty: Medium
Language: Python
Author: CodeByTushu
"""
from collections import deque
from typing import List
class Solution:
def maximumSafenessFactor(self, grid: List[List[int]]) -> int:
n = len(grid)
dist = [[-1] * n for _ in range(n)]
q = deque()
# Multi-source BFS from all thieves
for i in range(n):
for j in range(n):
if grid[i][j] == 1:
dist[i][j] = 0
q.append((i, j))
dirs = [(1, 0), (-1, 0), (0, 1), (0, -1)]
while q:
r, c = q.popleft()
for dr, dc in dirs:
nr, nc = r + dr, c + dc
if 0 <= nr < n and 0 <= nc < n and dist[nr][nc] == -1:
dist[nr][nc] = dist[r][c] + 1
q.append((nr, nc))
def canReach(limit: int) -> bool:
if dist[0][0] < limit:
return False
vis = [[False] * n for _ in range(n)]
bq = deque([(0, 0)])
vis[0][0] = True
while bq:
r, c = bq.popleft()
if r == n - 1 and c == n - 1:
return True
for dr, dc in dirs:
nr, nc = r + dr, c + dc
if 0 <= nr < n and 0 <= nc < n and not vis[nr][nc] and dist[nr][nc] >= limit:
vis[nr][nc] = True
bq.append((nr, nc))
return False
low, high = 0, 2 * n
while low <= high:
mid = (low + high) // 2
if canReach(mid):
low = mid + 1
else:
high = mid - 1
return high
/**
* Problem: LeetCode 2812 - Find the Safest Path in a Grid
* Difficulty: Medium
* Language: C++
* Author: CodeByTushu
*/
#include <vector>
#include <queue>
#include <algorithm>
using namespace std;
/**
* Problem: LeetCode 2812 - Find the Safest Path in a Grid
* Difficulty: Medium
* Language: C++
* Author: CodeByTushu
*/
class Solution {
private:
int n;
vector<vector<int>> dist;
const int dirs[4][2] = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}};
bool canReach(int limit) {
if (dist[0][0] < limit) return false;
vector<vector<bool>> vis(n, vector<bool>(n, false));
queue<pair<int, int>> q;
q.push({0, 0});
vis[0][0] = true;
while (!q.empty()) {
auto [r, c] = q.front();
q.pop();
if (r == n - 1 && c == n - 1) return true;
for (const auto& d : dirs) {
int nr = r + d[0];
int nc = c + d[1];
if (nr >= 0 && nr < n && nc >= 0 && nc < n &&
!vis[nr][nc] && dist[nr][nc] >= limit) {
vis[nr][nc] = true;
q.push({nr, nc});
}
}
}
return false;
}
public:
int maximumSafenessFactor(vector<vector<int>>& grid) {
n = grid.size();
dist.assign(n, vector<int>(n, -1));
queue<pair<int, int>> q;
// Multi-source BFS from all thieves
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
if (grid[i][j] == 1) {
dist[i][j] = 0;
q.push({i, j});
}
}
}
while (!q.empty()) {
auto [r, c] = q.front();
q.pop();
for (const auto& d : dirs) {
int nr = r + d[0];
int nc = c + d[1];
if (nr >= 0 && nr < n && nc >= 0 && nc < n && dist[nr][nc] == -1) {
dist[nr][nc] = dist[r][c] + 1;
q.push({nr, nc});
}
}
}
int low = 0, high = 2 * n;
while (low <= high) {
int mid = low + (high - low) / 2;
if (canReach(mid)) {
low = mid + 1;
} else {
high = mid - 1;
}
}
return high;
}
};
/**
* Problem: LeetCode 2812 - Find the Safest Path in a Grid
* Difficulty: Medium
* Language: JavaScript
* Author: CodeByTushu
*/
var maximumSafenessFactor = function(grid) {
const n = grid.length;
const dist = Array.from({ length: n }, () => new Int32Array(n).fill(-1));
const dirs = [[1, 0], [-1, 0], [0, 1], [0, -1]];
const q = [];
let qHead = 0;
// Multi-source BFS from all thieves
for (let i = 0; i < n; i++) {
for (let j = 0; j < n; j++) {
if (grid[i][j] === 1) {
dist[i][j] = 0;
q.push((i << 16) | j);
}
}
}
while (qHead < q.length) {
const cur = q[qHead++];
const r = cur >> 16;
const c = cur & 0xFFFF;
for (const [dr, dc] of dirs) {
const nr = r + dr;
const nc = c + dc;
if (nr >= 0 && nr < n && nc >= 0 && nc < n && dist[nr][nc] === -1) {
dist[nr][nc] = dist[r][c] + 1;
q.push((nr << 16) | nc);
}
}
}
function canReach(limit) {
if (dist[0][0] < limit) return false;
const vis = Array.from({ length: n }, () => new Uint8Array(n));
const bq = [(0 << 16) | 0];
let bqHead = 0;
vis[0][0] = 1;
while (bqHead < bq.length) {
const cur = bq[bqHead++];
const r = cur >> 16;
const c = cur & 0xFFFF;
if (r === n - 1 && c === n - 1) return true;
for (const [dr, dc] of dirs) {
const nr = r + dr;
const nc = c + dc;
if (nr >= 0 && nr < n && nc >= 0 && nc < n &&
!vis[nr][nc] && dist[nr][nc] >= limit) {
vis[nr][nc] = 1;
bq.push((nr << 16) | nc);
}
}
}
return false;
}
let low = 0, high = 2 * n;
while (low <= high) {
const mid = Math.floor((low + high) / 2);
if (canReach(mid)) {
low = mid + 1;
} else {
high = mid - 1;
}
}
return high;
};