Samkit5025

Untitled

Aug 15th, 2022
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.45 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. int minimumSweatingFactor(vector<vector<int>> &a)
  5. {
  6.     int n = a.size();
  7.     priority_queue<pair<int, pair<int, int>>, vector<pair<int, pair<int, int>>>, greater<pair<int, pair<int, int>>>> pq;
  8.     pq.push({a[0][0], {0, 0}});
  9.     vector<vector<bool>> visited(n, vector<bool>(n, false));
  10.     int res = 0;
  11.     while (!pq.empty())
  12.     {
  13.         auto temp = pq.top();
  14.         pq.pop();
  15.         int h = temp.first;
  16.         int i = temp.second.first;
  17.         int j = temp.second.second;
  18.         visited[i][j] = true;
  19.         res = max(res, h);
  20.         if (i == n - 1 && j == n - 1)
  21.         {
  22.             return res;
  23.         }
  24.  
  25.         if (i + 1 < n && !visited[i + 1][j])
  26.         {
  27.             pq.push({a[i + 1][j], {i + 1, j}});
  28.         }
  29.         if (j + 1 < n && !visited[i][j + 1])
  30.         {
  31.             pq.push({a[i][j + 1], {i, j + 1}});
  32.         }
  33.         if (i - 1 >= 0 && !visited[i - 1][j])
  34.         {
  35.             pq.push({a[i - 1][j], {i - 1, j}});
  36.         }
  37.         if (j - 1 >= 0 && !visited[i][j - 1])
  38.         {
  39.             pq.push({a[i][j - 1], {i, j - 1}});
  40.         }
  41.     }
  42.     return 0;
  43. }
  44.  
  45. signed main()
  46. {
  47.     int N;
  48.     cin >> N;
  49.     vector<vector<int>> grid(N, vector<int>(N));
  50.  
  51.     for (int i = 0; i < N; i++)
  52.     {
  53.         for (int j = 0; j < N; j++)
  54.         {
  55.             cin >> grid[i][j];
  56.         }
  57.     }
  58.  
  59.     cout << minimumSweatingFactor(grid) << endl;
  60. }
  61.  
Advertisement
Add Comment
Please, Sign In to add comment