danielvitor23

Untitled

Nov 6th, 2021 (edited)
283
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.70 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. const int INF = 0x3f3f3f3f;
  5. const int MAX = 51;
  6. const int dx[] = {0, 0, -1, 1};
  7. const int dy[] = {-1, 1, 0, 0};
  8. const char dir[] = {'L', 'R', 'U', 'D'};
  9.  
  10. int n, m, ex, ey, rx, ry;
  11. bitset<2502> dp[MAX][MAX][MAX];
  12. bitset<2502> calc[MAX][MAX][MAX];
  13. char ch[MAX][MAX];
  14. string str;
  15.  
  16. bool valid(int x, int y) {
  17.   return !(x < 0 or x >= n or y < 0 or y >= m or ch[x][y] == '#');
  18. }
  19.  
  20. bool solve(int x, int y, int qtd, int pos) {
  21.   if (x == ex and y == ey) {
  22.     return true;
  23.   }
  24.   if (calc[x][y][pos][qtd]) return dp[x][y][pos][qtd];
  25.   calc[x][y][pos][qtd] = true;
  26.   bool ans = false;
  27.   if (qtd > 0 and pos < (int)str.size()) ans = max(ans, solve(x, y, qtd-1, pos-1));
  28.   for (int k = 0; k < 4; ++k) {
  29.     int nx = x + dx[k];
  30.     int ny = y + dy[k];
  31.     if (pos < (int)str.size() and dir[k] == str[pos]) {
  32.       if (valid(nx, ny)) {
  33.         ans = max(ans, solve(nx, ny, qtd, pos+1));
  34.       } else {
  35.         ans = max(ans, solve(x, y, qtd, pos+1));
  36.       }
  37.     } else {
  38.       if (valid(nx, ny) and qtd > 0) {
  39.         ans = max(ans, solve(nx, ny, qtd-1, pos));
  40.         if (qtd > 1) ans = max(ans, solve(nx, ny, qtd-2, pos+1));
  41.       }
  42.     }
  43.   }
  44.   dp[x][y][pos].set(qtd, ans);
  45.   return ans;
  46. }
  47.  
  48. int main() {
  49.   cin.tie(0)->sync_with_stdio(0);
  50.   cin >> n >> m;
  51.  
  52.   for (int i = 0; i < n; ++i) {
  53.     for (int j = 0; j < m; ++j) {
  54.       cin >> ch[i][j];
  55.       if (ch[i][j] == 'R') {
  56.         rx = i; ry = j;
  57.       } else if(ch[i][j] == 'E') {
  58.         ex = i; ey = j;
  59.       }
  60.     }
  61.   }
  62.  
  63.   cin >> str;
  64.  
  65.   int ans = 0;
  66.   for (int i = 0; i <= n*m; ++i) {
  67.     if (solve(rx, ry, i, 0)) {
  68.       ans = i;
  69.       break;
  70.     }
  71.   }
  72.  
  73.   cout << ans << '\n';
  74. }
Add Comment
Please, Sign In to add comment