Advertisement
asdfg0998

dasfasdf

Nov 20th, 2024
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.59 KB | None | 0 0
  1. struct State {
  2.     int row, col, value, deviceUsed;
  3. };
  4.  
  5. const vector<pair<int, int>> directions = {{1, 0}, {1, -1}, {1, 1}};
  6.  
  7. int getMaxPseudoRandomNumber(vector<vector<int>>& unitType, int n, int m, int k) {
  8.     vector<vector<vector<int>>> dp(n, vector<vector<int>>(m, vector<int>(2, -1)));
  9.     queue<State> q;
  10.  
  11.     for (int j = 0; j < m; j++) {
  12.         q.push({0, j, 0, 0});
  13.         dp[0][j][0] = 0;
  14.     }
  15.  
  16.     int maxValue = -1;
  17.  
  18.     while (!q.empty()) {
  19.         State current = q.front();
  20.         q.pop();
  21.  
  22.         int r = current.row, c = current.col, val = current.value, used = current.deviceUsed;
  23.  
  24.         for (auto [dr, dc] : directions) {
  25.             int nr = r + dr, nc = c + dc;
  26.  
  27.             if (nr < 0 || nr >= n || nc < 0 || nc >= m) continue;
  28.  
  29.             int newValue = val;
  30.             if (unitType[nr][nc] == 1) newValue++;
  31.             else if (unitType[nr][nc] == 2) newValue--;
  32.  
  33.             if (newValue < 0) continue;
  34.  
  35.             if (used == 0 && unitType[nr][nc] == 2) {
  36.                 for (int i = nr; i < min(n, nr + k); i++) {
  37.                     for (int j = 0; j < m; j++) {
  38.                         if (unitType[i][j] == 2) {
  39.                             unitType[i][j] = 0;
  40.                         }
  41.                     }
  42.                 }
  43.                 used = 1;
  44.             }
  45.  
  46.             if (dp[nr][nc][used] < newValue) {
  47.                 dp[nr][nc][used] = newValue;
  48.                 q.push({nr, nc, newValue, used});
  49.                 if (nr == n - 1) maxValue = max(maxValue, newValue);
  50.             }
  51.         }
  52.     }
  53.  
  54.     return maxValue;
  55. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement