This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
#ifndef _LOCAL
//#pragma GCC optimize("O3,Ofast")
#else
#pragma GCC optimize("O0")
#endif
template<typename t> inline void umin(t &a, const t b) {a = min(a, b);}
template<typename t> inline void umax(t &a, const t b) {a = max(a, b);}
typedef pair<int, int> pii;
typedef long long ll;
typedef long double ld;
typedef int8_t byte;
ll time() {return chrono::system_clock().now().time_since_epoch().count();}
mt19937 rnd(time());
#define ft first
#define sd second
#define len(f) int((f).size())
#define bnd(f) (f).begin(), (f).end()
#define _ <<' '<<
const int inf = 1e9 + 5;
const ll inf64 = 4e18 + 5;
const int md = 998244353;
namespace MD {
void add(int &a, const int b) {if((a += b) >= md) a -= md;}
void sub(int &a, const int b) {if((a -= b) < 0) a += md;}
int prod(const int a, const int b) {return ll(a) * b % md;}
};
int n, m, sx, sy, fx, fy;
const int N = 2e3 + 5;
string a[N];
int d[N][N];
pii p[N][N];
const pair<char, pii> st[4] = {
{'^', {-1, -0}},
{'>', {+0, +1}},
{'v', {+1, +0}},
{'<', {-0, -1}}
};
void solve() {
cin >> n >> m;
for(int i = 0; i < n; ++i) cin >> a[i];
for(int i = 0; i < n; ++i) {
for(int j = 0; j < m; ++j) {
d[i][j] = n + m;
if(a[i][j] == 'o')
sx = i, sy = j,
d[i][j] = 0;
if(a[i][j] == 'x')
fx = i, fy = j;
}
}
deque<pii> q;
q.push_back({sx, sy});
while(!q.empty()) {
pii v = q[0];
q.pop_front();
for(int i = 0; i < 4; ++i) {
int add = st[i].ft != a[v.ft][v.sd];
int w = d[v.ft][v.sd] + add;
int x = v.ft + st[i].sd.ft;
int y = v.sd + st[i].sd.sd;
if(min(x, y) < 0 || x == n || y == m) continue;
if(d[x][y] <= w) continue;
d[x][y] = w;
p[x][y] = v;
if(add)
q.push_back({x, y});
else
q.push_front({x, y});
}
}
cout << d[fx][fy] - 1 << '\n';
while(fx != sx || fy != sy) {
pii v = p[fx][fy];
int i = v.ft, j = v.sd;
for(int t = 0; t < 4; ++t) {
if(fx - i == st[t].sd.ft &&
fy - j == st[t].sd.sd)
if(a[i][j] != 'o')
a[i][j] = st[t].ft;
}
fx = i;
fy = j;
}
for(int i = 0; i < n; ++i, cout << '\n')
for(int j = 0; j < m; ++j) cout << a[i][j];
}
signed main() {
ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
#ifndef _LOCAL
// freopen("file.in", "r", stdin);
// freopen("file.out", "w", stdout);
#else
system("color a");
freopen("in.txt", "r", stdin);
int t; cin >> t;
while(t--)
#endif
solve();
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |