#pragma GCC optimize("O3")
#include <bits/stdc++.h>
using namespace std;
#define ll long long
// #define int ll
#define fi first
#define se second
#define ld long double
#define pb push_back
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
char v[2010][2010];
bool vis[2010][2010];
int dd[2010][2010];
int prec[2010][2010];
int N, M;
void solve(int start) {
deque<tuple<int, int, int> > q;
q.push_front({0, start, -1});
while (!q.empty()) {
auto [d, pos, last] = q.front();
q.pop_front();
int x = pos / 10000, y = pos % 10000;
if (vis[x][y]) continue;
vis[x][y] = 1;
dd[x][y] = d;
prec[x][y] = last;
if (v[x][y] == 'x') return;
if (x) {
(v[x][y] != '^') ? q.push_back({d + 1, (x - 1) * 10000 + y, pos}) : q.push_front({d, (x - 1) * 10000 + y, pos});
}
if (x < N - 1) {
(v[x][y] != 'v') ? q.push_back({d + 1, (x + 1) * 10000 + y, pos}) : q.push_front({d, (x + 1) * 10000 + y, pos});
}
if (y) {
(v[x][y] != '<') ? q.push_back({d + 1, x * 10000 + y - 1, pos}) : q.push_front({d, x * 10000 + y - 1, pos});
}
if (y < M - 1) {
(v[x][y] != '>') ? q.push_back({d + 1, x * 10000 + y + 1, pos}) : q.push_front({d, x * 10000 + y + 1, pos});
}
}
}
signed main() {
ios::sync_with_stdio(false);
cin.tie(0);
cin >> N >> M;
pair<int, int> start, fine;
for (int i = 0; i < N; i++) {
string a;
cin >> a;
for (int j = 0; j < M; j++) {
v[i][j] = a[j];
if (a[j] == 'o') start = {i, j};
if (a[j] == 'x') fine = {i, j};
}
}
solve(start.fi * 10000 + start.se);
// for (int i = 0; i < N; i++) {
// for (int j = 0; j < M; j++) {
// dd[i][j]--;
// cout << dd[i][j] << " ";
// }
// cout << endl;
// }
pair<int, int> pos = fine;
while (1) {
int k = prec[pos.fi][pos.se];
if (k == start.fi * 10000 + start.se) break;
int x = k / 10000, y = k % 10000;
if (x < pos.fi) v[x][y] = 'v';
if (x > pos.fi) v[x][y] = '^';
if (y < pos.se) v[x][y] = '>';
if (y > pos.se) v[x][y] = '<';
pos = {x, y};
}
cout << dd[fine.fi][fine.se] - 1 << '\n';
for (int i = 0; i < N; i++) {
for (int j = 0; j < M; j++) {
cout << v[i][j];
}
cout << '\n';
}
}
Compilation message
Main.cpp: In function 'void solve(int)':
Main.cpp:20:14: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
20 | auto [d, pos, last] = q.front();
| ^
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
452 KB |
Output is correct |
3 |
Correct |
0 ms |
460 KB |
Output is correct |
4 |
Correct |
0 ms |
460 KB |
Output is correct |
5 |
Correct |
0 ms |
332 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
452 KB |
Output is correct |
3 |
Correct |
0 ms |
460 KB |
Output is correct |
4 |
Correct |
0 ms |
460 KB |
Output is correct |
5 |
Correct |
0 ms |
332 KB |
Output is correct |
6 |
Correct |
31 ms |
9568 KB |
Output is correct |
7 |
Correct |
34 ms |
21108 KB |
Output is correct |
8 |
Correct |
93 ms |
21624 KB |
Output is correct |
9 |
Correct |
160 ms |
46176 KB |
Output is correct |
10 |
Correct |
251 ms |
38628 KB |
Output is correct |
11 |
Correct |
294 ms |
74832 KB |
Output is correct |