-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Create 2577_Minimum_Time_to_Visit_a_Cell_In_a_Grid.cpp
- Loading branch information
1 parent
021eeb9
commit 1350323
Showing
1 changed file
with
50 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} | ||
} | ||
} | ||
}; |