Skip to content

Commit

Permalink
2024-12-03 Minimum Time to Visit a Cell In a Grid
Browse files Browse the repository at this point in the history
  • Loading branch information
InSange committed Dec 2, 2024
1 parent 9159fe2 commit 1ade156
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 3 deletions.
57 changes: 57 additions & 0 deletions InSange/BFS/2577. Minimum Time to Visit a Cell In a Grid.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
#include <vector>
#include <queue>

using namespace std;

class Solution {
public:
int minimumTime(vector<vector<int>>& grid) {
if (grid[0][1] > 1 && grid[1][0] > 1) // ๊ฐˆ ์ˆ˜ ์žˆ๋Š” ๊ธธ์ด ์ฒ˜์Œ๋ถ€ํ„ฐ ์—†์œผ๋ฉด
{
return -1;
}

int row = grid.size();
int col = grid[0].size();

vector<vector<int>> val;
val.assign(row, vector<int>(col, INT_MAX));

priority_queue<vector<int>, vector<vector<int>>, greater<>> pq;
int dy[4] = { 0, 0, -1, 1 };
int dx[4] = { 1, -1, 0, 0 };

pq.push({ 0, 0, 0 });
val[0][0] = 0;

while (!pq.empty())
{
int curY = pq.top()[1];
int curX = pq.top()[2];
int curTime = pq.top()[0];
pq.pop();

if (curY == row - 1 && curX == col - 1)
{
return curTime;
}

for (int i = 0; i < 4; i++)
{
int nextY = curY + dy[i];
int nextX = curX + dx[i];

if (nextY < 0 || nextY >= row || nextX < 0 || nextX >= col) continue; // Out of Bounds
int nextTime = max(curTime + 1, grid[nextY][nextX] + ((grid[nextY][nextX] - curTime) % 2 == 0 ? 1 : 0));

if (val[nextY][nextX] > nextTime)
{
pq.push({ nextTime, nextY, nextX });
val[nextY][nextX] = nextTime;
}
}
}

return -1;
}
};
4 changes: 1 addition & 3 deletions InSange/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,5 @@
| 29์ฐจ์‹œ | 2024.08.25 | ๋ฌธ์ž์—ด | [Find the Closest Palindrome](https://leetcode.com/problems/find-the-closest-palindrome/) | [#29](https://github.com/AlgoLeadMe/AlgoLeadMe-8/pull/98)]
| 30์ฐจ์‹œ | 2024.09.06 | ๋ฌธ์ž์—ด | [Delete Nodes From Linked List Present in Array](https://leetcode.com/problems/delete-nodes-from-linked-list-present-in-array/) | [#30](https://github.com/AlgoLeadMe/AlgoLeadMe-8/pull/100)]
| 31์ฐจ์‹œ | 2024.12.01 | DFS | [Valid Arrangement of Pairs](https://leetcode.com/problems/valid-arrangement-of-pairs/) | [#31](https://github.com/AlgoLeadMe/AlgoLeadMe-8/pull/101)]
| 32์ฐจ์‹œ | 2024.12.03 | BFS | [Minimum Time to Visit a Cell In a Grid](https://leetcode.com/problems/minimum-time-to-visit-a-cell-in-a-grid/) | [#32](https://github.com/AlgoLeadMe/AlgoLeadMe-8/pull/102)]
---

https://leetcode.com/problems/robot-collisions/
Find the Closest Palindrome

0 comments on commit 1ade156

Please sign in to comment.