Skip to content

Commit

Permalink
Create 2577_Minimum_Time_to_Visit_a_Cell_In_a_Grid.cpp
Browse files Browse the repository at this point in the history
  • Loading branch information
Nothing-avil authored Nov 29, 2024
1 parent 021eeb9 commit 1350323
Showing 1 changed file with 50 additions and 0 deletions.
50 changes: 50 additions & 0 deletions 2577_Minimum_Time_to_Visit_a_Cell_In_a_Grid.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
// ███████╗ █████╗ ███╗ ██╗ ██████╗ █████╗ ██████╗ ██████╗ ██╗ ██╗
// ██╔════╝ ██╔══██╗ ████╗ ██║ ██╔══██╗ ██╔══██╗ ██╔══██╗ ██╔══██╗ ██║ ██║
// ███████╗ ███████║ ██╔██╗ ██║ ██║ ██║ ███████║ ██████╔╝ ██████╔╝ ███████║
// ╚════██║ ██╔══██║ ██║╚██╗██║ ██║ ██║ ██╔══██║ ██╔═██╗ ██╔══██╗ ██╔══██║
// ███████║ ██║ ██║ ██║ ╚████║ ██████╔╝ ██║ ██║ ██║ ██╗ ██████╔╝ ██║ ██║
// ╚══════╝ ╚═╝ ╚═╝ ╚═╝ ╚═══╝ ╚═════╝ ╚═╝ ╚═╝ ╚═╝ ╚═╝ ╚═════╝ ╚═╝ ╚═╝
#pragma GCC optimize("Ofast", "inline", "ffast-math", "unroll-loops","no-stack-protector")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,avx2,tune=native", "f16c")
auto init = []() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
return 'c';
}();
class Solution {
public:
int minimumTime(vector<vector<int>>& grid) {
if (grid[0][1] > 1 && grid[1][0] > 1) {
return -1;
}
int m = grid.size(), n = grid[0].size();
int dist[m][n];
memset(dist, 0x3f, sizeof dist);
dist[0][0] = 0;
using tii = tuple<int, int, int>;
priority_queue<tii, vector<tii>, greater<tii>> pq;
pq.emplace(0, 0, 0);
int dirs[5] = {-1, 0, 1, 0, -1};
while (1) {
auto [t, i, j] = pq.top();
pq.pop();
if (i == m - 1 && j == n - 1) {
return t;
}
for (int k = 0; k < 4; ++k) {
int x = i + dirs[k], y = j + dirs[k + 1];
if (x >= 0 && x < m && y >= 0 && y < n) {
int nt = t + 1;
if (nt < grid[x][y]) {
nt = grid[x][y] + (grid[x][y] - nt) % 2;
}
if (nt < dist[x][y]) {
dist[x][y] = nt;
pq.emplace(nt, x, y);
}
}
}
}
}
};

0 comments on commit 1350323

Please sign in to comment.