Advertisement
asdfg0998

Untitled

Mar 6th, 2025
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.77 KB | None | 0 0
  1. struct State {
  2.     int row, col;         // Current position
  3.     int direction;        // Current direction: 0=none, 1=up, 2=right, 3=down, 4=left
  4.     int lastJumpLength;   // Length of the last jump
  5.     int jumps;            // Number of jumps taken so far
  6.    
  7.     // Constructor
  8.     State(int r, int c, int dir, int jumpLen, int j)
  9.         : row(r), col(c), direction(dir), lastJumpLength(jumpLen), jumps(j) {}
  10.    
  11.     // Create a unique key for this state (used for visited set)
  12.     std::string getKey() const {
  13.         return std::to_string(row) + "," + std::to_string(col) + "," +
  14.                std::to_string(direction) + "," + std::to_string(lastJumpLength);
  15.     }
  16. };
  17.  
  18. int getMinJumps(std::vector<std::string>& grid) {
  19.     int n = grid.size();
  20.     if (n == 0) return -1;
  21.     int m = grid[0].size();
  22.    
  23.     // Find the start and end positions
  24.     int startRow = -1, startCol = -1;
  25.     int endRow = -1, endCol = -1;
  26.    
  27.     for (int i = 0; i < n; i++) {
  28.         for (int j = 0; j < m; j++) {
  29.             if (grid[i][j] == 'S') {
  30.                 startRow = i;
  31.                 startCol = j;
  32.                 // Replace 'S' with '*' to simplify checks
  33.                 grid[i][j] = '*';
  34.             } else if (grid[i][j] == 'E') {
  35.                 endRow = i;
  36.                 endCol = j;
  37.                 // Replace 'E' with '*' to simplify checks
  38.                 grid[i][j] = '*';
  39.             }
  40.         }
  41.     }
  42.    
  43.     // If start or end isn't found, return -1
  44.     if (startRow == -1 || endRow == -1) {
  45.         return -1;
  46.     }
  47.    
  48.     // Directions: none, up, right, down, left
  49.     const int dr[] = {0, -1, 0, 1, 0};
  50.     const int dc[] = {0, 0, 1, 0, -1};
  51.    
  52.     // BFS queue
  53.     std::queue<State> q;
  54.     q.emplace(startRow, startCol, 0, 0, 0); // Start with no direction and 0 jumps
  55.    
  56.     // Keep track of visited states
  57.     std::unordered_map<std::string, bool> visited;
  58.    
  59.     while (!q.empty()) {
  60.         State current = q.front();
  61.         q.pop();
  62.        
  63.         // Check if we've reached the end
  64.         if (current.row == endRow && current.col == endCol) {
  65.             return current.jumps;
  66.         }
  67.        
  68.         // Skip if we've already visited this state
  69.         std::string key = current.getKey();
  70.         if (visited.find(key) != visited.end()) {
  71.             continue;
  72.         }
  73.         visited[key] = true;
  74.        
  75.         // If we're in the middle of a multi-unit jump, we must continue in the same direction
  76.         if (current.lastJumpLength > 1) {
  77.             int dir = current.direction;
  78.             for (int k = 1; k <= m + n; k++) { // Maximum jump length could be m+n (grid diagonal)
  79.                 int newRow = current.row + dr[dir] * k;
  80.                 int newCol = current.col + dc[dir] * k;
  81.                
  82.                 // Check if the landing cell is valid and empty
  83.                 if (newRow >= 0 && newRow < n && newCol >= 0 && newCol < m && grid[newRow][newCol] == '*') {
  84.                     q.emplace(newRow, newCol, dir, k, current.jumps + 1);
  85.                 }
  86.             }
  87.         } else {
  88.             // We can choose any direction
  89.             for (int dir = 1; dir <= 4; dir++) {
  90.                 for (int k = 1; k <= m + n; k++) { // Maximum jump length could be m+n
  91.                     int newRow = current.row + dr[dir] * k;
  92.                     int newCol = current.col + dc[dir] * k;
  93.                    
  94.                     // Check if the landing cell is valid and empty
  95.                     if (newRow >= 0 && newRow < n && newCol >= 0 && newCol < m && grid[newRow][newCol] == '*') {
  96.                         q.emplace(newRow, newCol, dir, k, current.jumps + 1);
  97.                     }
  98.                 }
  99.             }
  100.         }
  101.     }
  102.    
  103.     // If we cannot reach the end
  104.     return -1;
  105. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement