2024-05-15 Daily Challenge
Today I have done leetcode's May LeetCoding Challenge with cpp
.
May LeetCoding Challenge 15
Description
Find the Safest Path in a Grid
You are given a 0-indexed 2D matrix grid
of size n x n
, where (r, c)
represents:
- A cell containing a thief if
grid[r][c] = 1
- An empty cell if
grid[r][c] = 0
You are initially positioned at cell (0, 0)
. In one move, you can move to any adjacent cell in the grid, including cells containing thieves.
The safeness factor of a path on the grid is defined as the minimum manhattan distance from any cell in the path to any thief in the grid.
Return the maximum safeness factor of all paths leading to cell (n - 1, n - 1)
.
An adjacent cell of cell (r, c)
, is one of the cells (r, c + 1)
, (r, c - 1)
, (r + 1, c)
and (r - 1, c)
if it exists.
The Manhattan distance between two cells (a, b)
and (x, y)
is equal to |a - x| + |b - y|
, where |val|
denotes the absolute value of val.
Example 1:
Input: grid = [[1,0,0],[0,0,0],[0,0,1]] Output: 0 Explanation: All paths from (0, 0) to (n - 1, n - 1) go through the thieves in cells (0, 0) and (n - 1, n - 1).
Example 2:
Input: grid = [[0,0,1],[0,0,0],[0,0,0]] Output: 2 Explanation: The path depicted in the picture above has a safeness factor of 2 since: - The closest cell of the path to the thief at cell (0, 2) is cell (0, 0). The distance between them is | 0 - 0 | + | 0 - 2 | = 2. It can be shown that there are no other paths with a higher safeness factor.
Example 3:
Input: grid = [[0,0,0,1],[0,0,0,0],[0,0,0,0],[1,0,0,0]] Output: 2 Explanation: The path depicted in the picture above has a safeness factor of 2 since: - The closest cell of the path to the thief at cell (0, 3) is cell (1, 2). The distance between them is | 0 - 1 | + | 3 - 2 | = 2. - The closest cell of the path to the thief at cell (3, 0) is cell (3, 2). The distance between them is | 3 - 3 | + | 0 - 2 | = 2. It can be shown that there are no other paths with a higher safeness factor.
Constraints:
1 <= grid.length == n <= 400
grid[i].length == n
grid[i][j]
is either0
or1
.- There is at least one thief in the
grid
.
Solution
class Solution {
const int MOVES[4][2] = {{0, 1}, {1, 0}, {0, -1}, {-1, 0}};
void computeGrid(vector<vector<int>>& grid, queue<pair<int, int>> &q) {
int n = grid.size();
set<int> visited;
while(q.size()) {
auto [row, col] = q.front();
q.pop();
visited.insert(row * 400 + col);
for(int i = 0; i < 4; ++i) {
int newRow = row + MOVES[i][0];
int newCol = col + MOVES[i][1];
if(newRow < 0 || newCol < 0 || newRow >= n || newCol >= n) continue;
if(visited.count(newRow * 400 + newCol)) continue;
if(grid[newRow][newCol] <= grid[row][col] + 1) continue;
grid[newRow][newCol] = grid[row][col] + 1;
q.push({newRow, newCol});
}
}
}
public:
int maximumSafenessFactor(vector<vector<int>>& grid) {
for(auto &row : grid) {
for(auto &c : row) {
c = c ? 0 : INT_MAX;
}
}
int n = grid.size();
queue<pair<int, int>> q;
for(int i = 0; i < n; ++i) {
for(int j = 0; j < n; ++j) {
if(!grid[i][j]) q.push({i, j});
}
}
computeGrid(grid, q);
priority_queue<pair<int, int>> pq;
set<int> visited;
visited.insert(0);
pq.push({grid[0][0], 0});
while(pq.size()) {
auto [score, position] = pq.top();
pq.pop();
int row = position / 400;
int col = position % 400;
if(row == n - 1 && col == n - 1) return score;
for(int i = 0; i < 4; ++i) {
int newRow = row + MOVES[i][0];
int newCol = col + MOVES[i][1];
if(newRow < 0 || newCol < 0 || newRow >= n || newCol >= n) continue;
if(visited.count(newRow * 400 + newCol)) continue;
pq.push({min(grid[newRow][newCol], score), newRow * 400 + newCol});
visited.insert(newRow * 400 + newCol);
}
}
return -1;
}
};