Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using namespace std;
- int minimumSweatingFactor(vector<vector<int>> &a)
- {
- int n = a.size();
- priority_queue<pair<int, pair<int, int>>, vector<pair<int, pair<int, int>>>, greater<pair<int, pair<int, int>>>> pq;
- pq.push({a[0][0], {0, 0}});
- vector<vector<bool>> visited(n, vector<bool>(n, false));
- int res = 0;
- while (!pq.empty())
- {
- auto temp = pq.top();
- pq.pop();
- int h = temp.first;
- int i = temp.second.first;
- int j = temp.second.second;
- visited[i][j] = true;
- res = max(res, h);
- if (i == n - 1 && j == n - 1)
- {
- return res;
- }
- if (i + 1 < n && !visited[i + 1][j])
- {
- pq.push({a[i + 1][j], {i + 1, j}});
- }
- if (j + 1 < n && !visited[i][j + 1])
- {
- pq.push({a[i][j + 1], {i, j + 1}});
- }
- if (i - 1 >= 0 && !visited[i - 1][j])
- {
- pq.push({a[i - 1][j], {i - 1, j}});
- }
- if (j - 1 >= 0 && !visited[i][j - 1])
- {
- pq.push({a[i][j - 1], {i, j - 1}});
- }
- }
- return 0;
- }
- signed main()
- {
- int N;
- cin >> N;
- vector<vector<int>> grid(N, vector<int>(N));
- for (int i = 0; i < N; i++)
- {
- for (int j = 0; j < N; j++)
- {
- cin >> grid[i][j];
- }
- }
- cout << minimumSweatingFactor(grid) << endl;
- }
Advertisement
Add Comment
Please, Sign In to add comment