nikunjsoni

1778

May 1st, 2021
152
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.77 KB | None | 0 0
  1. /**
  2.  * // This is the GridMaster's API interface.
  3.  * // You should not implement it, or speculate about its implementation
  4.  * class GridMaster {
  5.  *   public:
  6.  *     bool canMove(char direction);
  7.  *     void move(char direction);
  8.  *     boolean isTarget();
  9.  * };
  10.  */
  11.  
  12. class Solution {
  13. public:
  14.     // 0 is blocked, 1 is empty, 2 is target
  15.     void ConstructGrid(vector<vector<int>>& grid, GridMaster &master, int row, int col, const map<char, pair<int, int>>& directions) {
  16.         grid[row][col] = 1;
  17.         if (master.isTarget()) {
  18.             grid[row][col] = 2;
  19.         }
  20.        
  21.         for (const auto& direction : directions) {
  22.             int new_row = row + direction.second.first;
  23.             int new_col = col + direction.second.second;
  24.             if (grid[new_row][new_col] != -1) {
  25.                 continue;
  26.             }
  27.             if (master.canMove(direction.first)) {
  28.                 master.move(direction.first);
  29.                 ConstructGrid(grid, master, new_row, new_col, directions);
  30.                 if (direction.first == 'U') master.move('D');
  31.                 else if (direction.first == 'D') master.move('U');
  32.                 else if (direction.first == 'L') master.move('R');
  33.                 else master.move('L');
  34.             } else {
  35.                 grid[new_row][new_col] = 0;
  36.             }
  37.         }
  38.     }
  39.    
  40.     int findShortestPath(GridMaster &master) {
  41.         map<char, pair<int, int>> directions = {
  42.             {'U', {0, 1}},
  43.             {'D', {0, -1}},
  44.             {'L', {-1, 0}},
  45.             {'R', {1, 0}},
  46.         };
  47.        
  48.         vector<vector<int>> grid(1001, vector<int>(1001, -1));
  49.         ConstructGrid(grid, master, 501, 501, directions);
  50.        
  51.         queue<pair<int, int>> myqueue;
  52.         myqueue.push({501, 501});
  53.         grid[501][501] = 0;
  54.        
  55.         int distance = 1;
  56.         while (!myqueue.empty()) {
  57.             int queue_size = myqueue.size();
  58.             for (int k = 0; k < queue_size; k++) {
  59.                 pair<int, int> cur = myqueue.front();
  60.                 myqueue.pop();
  61.                
  62.                 for (const auto& direction : directions) {
  63.                     int new_row = cur.first + direction.second.first;
  64.                     int new_col = cur.second + direction.second.second;
  65.                     if (grid[new_row][new_col] == 2) {
  66.                         return distance;
  67.                     }
  68.                     if (grid[new_row][new_col] <= 0) {
  69.                         continue;
  70.                     }
  71.                     // Mark as 'visited'.
  72.                     grid[new_row][new_col] = 0;
  73.                     myqueue.push({new_row, new_col});
  74.                 }
  75.             }
  76.            
  77.             distance++;
  78.         }
  79.        
  80.         return -1;
  81.     }
  82. };
Advertisement
Add Comment
Please, Sign In to add comment