/**
* Problem: LeetCode 3286 - Find a Safe Walk Through a Grid
* Difficulty: Medium
* Language: Java
* Author: CodeByTushu
*/
import java.util.*;
/**
* Problem: LeetCode 3286 - Find a Safe Walk Through a Grid
* Difficulty: Medium
* Language: Java
* Author: CodeByTushu
*/
class Solution {
public boolean findSafeWalk(List<List<Integer>> grid, int health) {
int m = grid.size(), n = grid.get(0).size();
int[][] best = new int[m][n];
for (int[] row : best) Arrays.fill(row, -1);
PriorityQueue<int[]> pq = new PriorityQueue<>((a, b) -> b[2] - a[2]);
int startHealth = health - grid.get(0).get(0);
if (startHealth <= 0) return false;
best[0][0] = startHealth;
pq.offer(new int[]{0, 0, startHealth});
int[][] dir = {{1,0},{-1,0},{0,1},{0,-1}};
while (!pq.isEmpty()) {
int[] cur = pq.poll();
int x = cur[0], y = cur[1], h = cur[2];
if (h != best[x][y]) continue;
if (x == m - 1 && y == n - 1) return true;
for (int[] d : dir) {
int nx = x + d[0];
int ny = y + d[1];
if (nx < 0 || ny < 0 || nx >= m || ny >= n) continue;
int nh = h - grid.get(nx).get(ny);
if (nh > 0 && nh > best[nx][ny]) {
best[nx][ny] = nh;
pq.offer(new int[]{nx, ny, nh});
}
}
}
return false;
}
}
"""
Problem: LeetCode 3286 - Find a Safe Walk Through a Grid
Difficulty: Medium
Language: Python
Author: CodeByTushu
"""
import heapq
from typing import List
class Solution:
def findSafeWalk(self, grid: List[List[int]], health: int) -> bool:
m, n = len(grid), len(grid[0])
best = [[-1] * n for _ in range(m)]
start_health = health - grid[0][0]
if start_health <= 0:
return False
best[0][0] = start_health
# Max-heap storing (-health, x, y)
pq = [(-start_health, 0, 0)]
dirs = [(1, 0), (-1, 0), (0, 1), (0, -1)]
while pq:
neg_h, x, y = heapq.heappop(pq)
h = -neg_h
if h != best[x][y]:
continue
if x == m - 1 and y == n - 1:
return True
for dx, dy in dirs:
nx, ny = x + dx, y + dy
if 0 <= nx < m and 0 <= ny < n:
nh = h - grid[nx][ny]
if nh > 0 and nh > best[nx][ny]:
best[nx][ny] = nh
heapq.heappush(pq, (-nh, nx, ny))
return False
/**
* Problem: LeetCode 3286 - Find a Safe Walk Through a Grid
* Difficulty: Medium
* Language: C++
* Author: CodeByTushu
*/
#include <vector>
#include <queue>
#include <tuple>
using namespace std;
/**
* Problem: LeetCode 3286 - Find a Safe Walk Through a Grid
* Difficulty: Medium
* Language: C++
* Author: CodeByTushu
*/
class Solution {
public:
bool findSafeWalk(vector<vector<int>>& grid, int health) {
int m = grid.size(), n = grid[0].size();
vector<vector<int>> best(m, vector<int>(n, -1));
// Priority queue storing {health, x, y} in descending order
priority_queue<tuple<int, int, int>> pq;
int startHealth = health - grid[0][0];
if (startHealth <= 0) return false;
best[0][0] = startHealth;
pq.push({startHealth, 0, 0});
const int dir[4][2] = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}};
while (!pq.empty()) {
auto [h, x, y] = pq.top();
pq.pop();
if (h != best[x][y]) continue;
if (x == m - 1 && y == n - 1) return true;
for (const auto& d : dir) {
int nx = x + d[0];
int ny = y + d[1];
if (nx < 0 || ny < 0 || nx >= m || ny >= n) continue;
int nh = h - grid[nx][ny];
if (nh > 0 && nh > best[nx][ny]) {
best[nx][ny] = nh;
pq.push({nh, nx, ny});
}
}
}
return false;
}
};
/**
* Problem: LeetCode 3286 - Find a Safe Walk Through a Grid
* Difficulty: Medium
* Language: JavaScript
* Author: CodeByTushu
*/
class MaxHeap {
constructor() {
this.heap = [];
}
push(val) {
this.heap.push(val);
this._up(this.heap.length - 1);
}
pop() {
if (this.heap.length === 0) return null;
const top = this.heap[0];
const bottom = this.heap.pop();
if (this.heap.length > 0) {
this.heap[0] = bottom;
this._down(0);
}
return top;
}
isEmpty() {
return this.heap.length === 0;
}
_up(i) {
while (i > 0) {
const p = (i - 1) >> 1;
if (this.heap[i][2] > this.heap[p][2]) {
[this.heap[i], this.heap[p]] = [this.heap[p], this.heap[i]];
i = p;
} else {
break;
}
}
}
_down(i) {
const len = this.heap.length;
while ((i << 1) + 1 < len) {
let left = (i << 1) + 1;
let right = left + 1;
let best = i;
if (this.heap[left][2] > this.heap[best][2]) best = left;
if (right < len && this.heap[right][2] > this.heap[best][2]) best = right;
if (best !== i) {
[this.heap[i], this.heap[best]] = [this.heap[best], this.heap[i]];
i = best;
} else {
break;
}
}
}
}
/**
* @param {number[][]} grid
* @param {number} health
* @return {boolean}
*/
var findSafeWalk = function(grid, health) {
const m = grid.length;
const n = grid[0].length;
const best = Array.from({ length: m }, () => new Int32Array(n).fill(-1));
const pq = new MaxHeap();
const startHealth = health - grid[0][0];
if (startHealth <= 0) return false;
best[0][0] = startHealth;
pq.push([0, 0, startHealth]);
const dirs = [[1, 0], [-1, 0], [0, 1], [0, -1]];
while (!pq.isEmpty()) {
const [x, y, h] = pq.pop();
if (h !== best[x][y]) continue;
if (x === m - 1 && y === n - 1) return true;
for (const [dx, dy] of dirs) {
const nx = x + dx;
const ny = y + dy;
if (nx < 0 || ny < 0 || nx >= m || ny >= n) continue;
const nh = h - grid[nx][ny];
if (nh > 0 && nh > best[nx][ny]) {
best[nx][ny] = nh;
pq.push([nx, ny, nh]);
}
}
}
return false;
};