RainX_69

Shortest Path to Get All Keys | MUST DO | OA LEVEL | HARD AND TRICKY BFS

Apr 19th, 2023
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.65 KB | Source Code | 0 0
  1. https://leetcode.com/problems/shortest-path-to-get-all-keys/submissions/
  2.  
  3. You are given an m x n grid grid where:
  4.  
  5. '.' is an empty cell.
  6. '#' is a wall.
  7. '@' is the starting point.
  8. Lowercase letters represent keys.
  9. Uppercase letters represent locks.
  10.  
  11. You start at the starting point and one move consists of walking one space in one of the four cardinal directions. You cannot walk outside the grid, or walk into a wall.
  12. If you walk over a key, you can pick it up and you cannot walk over a lock unless you have its corresponding key.
  13. For some 1 <= k <= 6, there is exactly one lowercase and one uppercase letter of the first k letters of the English alphabet in the grid. This means that there is exactly one key for each lock, and one lock for each key; and also that the letters used to represent the keys and locks were chosen in the same order as the English alphabet.
  14. Return the lowest number of moves to acquire all keys. If it is impossible, return -1.
  15.  
  16. Example 1:
  17. Input: grid = ["@.a..","###.#","b.A.B"]
  18. Output: 8
  19. Explanation: Note that the goal is to obtain all the keys not to open all the locks.
  20.  
  21. Example 2:
  22. Input: grid = ["@..aA","..B#.","....b"]
  23. Output: 6
  24.  
  25. Example 3:
  26. Input: grid = ["@Aa"]
  27. Output: -1
  28.  
  29.  
  30. Constraints:
  31. m == grid.length
  32. n == grid[i].length
  33. 1 <= m, n <= 30
  34. grid[i][j] is either an English letter, '.', '#', or '@'.
  35. The number of keys in the grid is in the range [1, 6].
  36. Each key in the grid is unique.
  37. Each key in the grid has a matching lock.
  38. ---------------------------------------------------------------------------------------------------------------------------------------
  39.  
  40. struct Info {
  41.     int x;
  42.     int y;
  43.     int mask;
  44. };
  45.  
  46. class Solution {
  47. public:
  48.     int shortestPathAllKeys(vector<string>& grid) {
  49.         int n = grid.size(), m = grid[0].size(), keys = 0;
  50.         set<pair<pair<int,int>,int>> vis;
  51.         queue<Info> q;
  52.  
  53.         // find starting position and count keys
  54.         for(int i=0;i<n;i++) {
  55.             for(int j=0;j<m;j++) {
  56.                 if(grid[i][j] == '@') {
  57.                     q.push({i, j, 0});
  58.                     vis.insert({{i, j}, 0});
  59.                 }
  60.                 else if(grid[i][j] >= 'a' && grid[i][j] <= 'z') {
  61.                     keys++;
  62.                 }
  63.             }
  64.         }
  65.  
  66.         int X[4] = {-1, 1, 0, 0};
  67.         int Y[4] = {0, 0, 1, -1};
  68.  
  69.         // BFS
  70.         int steps=0;
  71.         while(!q.empty()) {
  72.             int size=q.size();
  73.             while(size--){
  74.                 auto curr = q.front();
  75.                 q.pop();
  76.                 if(__builtin_popcount(curr.mask) == keys) {
  77.                     return steps;
  78.                 }
  79.                 for(int i=0;i<4;i++) {
  80.                     int newX = curr.x + X[i];
  81.                     int newY = curr.y + Y[i];
  82.                     if(newX < 0 || newY < 0 || newX == n || newY == m || grid[newX][newY] == '#') {
  83.                         continue;
  84.                     }
  85.                     int newMask = curr.mask;
  86.                     if(grid[newX][newY] >= 'a' && grid[newX][newY] <= 'z') {
  87.                         newMask |= (1 << (grid[newX][newY] - 'a'));
  88.                     }
  89.                     if(grid[newX][newY] >= 'A' && grid[newX][newY] <= 'Z' && (curr.mask & (1 << (grid[newX][newY] - 'A')))==0) {
  90.                         continue;
  91.                     }
  92.                     if(vis.find({{newX, newY}, newMask})!=vis.end()) {
  93.                         continue;
  94.                     }
  95.                     q.push({newX, newY, newMask});
  96.                     vis.insert({{newX, newY}, newMask});
  97.                 }
  98.             }
  99.             steps++;
  100.         }
  101.  
  102.         return -1;
  103.     }
  104. };
  105.  
Advertisement
Add Comment
Please, Sign In to add comment