Advertisement
danielvitor23

Cycle In Maze

Oct 7th, 2021 (edited)
892
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.21 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. typedef pair<int, int> ii;
  4.  
  5. const int MAX = 1010;
  6. const int INF = 0x3f3f3f3f;
  7.  
  8. int n, m, k;
  9. string gr[MAX];
  10.  
  11. int main() {
  12.   cin.tie(0)->sync_with_stdio(0);
  13.  
  14.   cin >> n >> m >> k;
  15.  
  16.   int pi = -1, pj = -1;
  17.   for (int i = 0; i < n; ++i) {
  18.     cin >> gr[i];
  19.     for (int j = 0; j < m; ++j) {
  20.       if (gr[i][j] == 'X') {
  21.         pi = i; pj = j;
  22.       }
  23.     }
  24.   }
  25.  
  26.   if (k & 1) {
  27.     cout << "IMPOSSIBLE\n";
  28.     exit(0);
  29.   }
  30.  
  31.   int tam = k/2;
  32.  
  33.   k /= 2;
  34.   string ans = "";
  35.  
  36.   while (k--) {
  37.     if (pi+1 <= n-1 and gr[pi+1][pj] != '*') {
  38.       ans += "D";
  39.       pi++;
  40.     } else if (pj-1 >= 0 and gr[pi][pj-1] != '*') {
  41.       ans += "L";
  42.       pj--;
  43.     } else if (pj+1 <= m-1 and gr[pi][pj+1] != '*') {
  44.       ans += "R";
  45.       pj++;
  46.     } else if (pi-1 >= 0 and gr[pi-1][pj] != '*') {
  47.       ans += "U";
  48.       pi--;
  49.     }
  50.   }
  51.  
  52.   if (ans.size() != tam) {
  53.     cout << "IMPOSSIBLE\n";
  54.     exit(0);
  55.   }
  56.  
  57.   string aux = ans;
  58.   reverse(aux.begin(), aux.end());
  59.   for (char &c : aux) {
  60.     if (c == 'L') c = 'R';
  61.     else if (c == 'R') c = 'L';
  62.     else if (c == 'U') c = 'D';
  63.     else if (c == 'D') c = 'U';
  64.   }
  65.  
  66.   cout << ans << aux << '\n';
  67. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement