#include <bits/stdc++.h>
using namespace std;
#define nl "\n"
#define nf endl
#define ll long long
#define pb push_back
#define _ << ' ' <<
#define y1 treapdidsu
#define INF (ll)1e18
#define mod 998244353
#define maxn 2010
ll i, i1, j, k, k1, t, n, m, res, flag[10], a, b;
ll mt[maxn][maxn], pv[maxn][maxn], dist[maxn][maxn], x, y, x1, y1;
array<ll, 2> st, nd, s;
char rs[maxn][maxn], c;
deque<array<ll, 2>> dq;
ll d1[4] = {1, 0, -1, 0};
ll d2[4] = {0, 1, 0, -1};
char nc[4] = {'v', '>', '^', '<'};
bool oob(ll x, ll y) {
if (x < 1 || x > n || y < 1 || y > m) return true;
return false;
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
/* #if !ONLINE_JUDGE && !EVAL
ifstream cin("input.txt");
ofstream cout("output.txt");
#endif */
cin >> n >> m;
for (i = 1; i <= n; i++) {
for (j = 1; j <= m; j++) {
cin >> c; rs[i][j] = c;
if (c == 'o') {
st = {i, j}; mt[i][j] = 9;
} else if (c == 'x') {
nd = {i, j}; mt[i][j] = -1;
} else if (c == 'v') {
mt[i][j] = 0;
} else if (c == '>') {
mt[i][j] = 1;
} else if (c == '^') {
mt[i][j] = 2;
} else if (c == '<') {
mt[i][j] = 3;
} else if (c == '.') {
mt[i][j] = -1;
}
dist[i][j] = INF; pv[i][j] = 5;
}
}
dq.push_back(st); dist[st[0]][st[1]] = 0;
while (!dq.empty()) {
s = dq.front(); dq.pop_front();
x = s[0]; y = s[1];
for (i = 0; i < 4; i++) {
x1 = x + d1[i]; y1 = y + d2[i];
if (oob(x1, y1)) continue;
if (mt[x][y] == i || mt[x][y] == 9) k = 0;
else k = 1;
if (dist[x1][y1] > dist[x][y] + k) {
dist[x1][y1] = dist[x][y] + k;
pv[x1][y1] = i;
if (k == 0) dq.push_front({x1, y1});
else dq.push_back({x1, y1});
}
}
}
cout << dist[nd[0]][nd[1]] << nl;
/* for (i = 1; i <= n; i++) {
for (j = 1; j <= m; j++) {
cout << pv[i][j] << ' ';
}
cout << nl;
} */
x = nd[0]; y = nd[1];
while (true) {
k = (pv[x][y] + 2) % 4;
x += d1[k]; y += d2[k];
if (x == st[0] && y == st[1]) break;
k = (k + 2) % 4;
rs[x][y] = nc[k];
}
for (i = 1; i <= n; i++) {
for (j = 1; j <= m; j++) {
cout << rs[i][j];
}
cout << nl;
}
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
460 KB |
Output is correct |
3 |
Correct |
1 ms |
460 KB |
Output is correct |
4 |
Correct |
1 ms |
576 KB |
Output is correct |
5 |
Correct |
0 ms |
332 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
460 KB |
Output is correct |
3 |
Correct |
1 ms |
460 KB |
Output is correct |
4 |
Correct |
1 ms |
576 KB |
Output is correct |
5 |
Correct |
0 ms |
332 KB |
Output is correct |
6 |
Correct |
75 ms |
21412 KB |
Output is correct |
7 |
Correct |
84 ms |
28060 KB |
Output is correct |
8 |
Correct |
183 ms |
48180 KB |
Output is correct |
9 |
Correct |
283 ms |
79476 KB |
Output is correct |
10 |
Correct |
472 ms |
92476 KB |
Output is correct |
11 |
Correct |
502 ms |
116236 KB |
Output is correct |