Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using namespace std;
- typedef pair<int, int> ii;
- const int MAX = 1010;
- const int INF = 0x3f3f3f3f;
- int n, m, k;
- string gr[MAX];
- int main() {
- cin.tie(0)->sync_with_stdio(0);
- cin >> n >> m >> k;
- int pi = -1, pj = -1;
- for (int i = 0; i < n; ++i) {
- cin >> gr[i];
- for (int j = 0; j < m; ++j) {
- if (gr[i][j] == 'X') {
- pi = i; pj = j;
- }
- }
- }
- if (k & 1) {
- cout << "IMPOSSIBLE\n";
- exit(0);
- }
- int tam = k/2;
- k /= 2;
- string ans = "";
- while (k--) {
- if (pi+1 <= n-1 and gr[pi+1][pj] != '*') {
- ans += "D";
- pi++;
- } else if (pj-1 >= 0 and gr[pi][pj-1] != '*') {
- ans += "L";
- pj--;
- } else if (pj+1 <= m-1 and gr[pi][pj+1] != '*') {
- ans += "R";
- pj++;
- } else if (pi-1 >= 0 and gr[pi-1][pj] != '*') {
- ans += "U";
- pi--;
- }
- }
- if (ans.size() != tam) {
- cout << "IMPOSSIBLE\n";
- exit(0);
- }
- string aux = ans;
- reverse(aux.begin(), aux.end());
- for (char &c : aux) {
- if (c == 'L') c = 'R';
- else if (c == 'R') c = 'L';
- else if (c == 'U') c = 'D';
- else if (c == 'D') c = 'U';
- }
- cout << ans << aux << '\n';
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement