This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
/*
www.youtube.com/YugiHackerChannel
oj.vnoi.info/user/YugiHackerKhongCopCode
*/
#include<bits/stdc++.h>
#define el cout<<"\n"
#define f0(i,n) for(int i=0;i<n;++i)
#define f1(i,n) for(int i=1;i<=n;++i)
#define maxn 2003
using namespace std;
int n, m, s[2], t[2];
char a[maxn][maxn];
int d[maxn][maxn], tr[maxn][maxn];
int dx[] = {0, 0, -1, 1};
int dy[] = {-1, 1, 0, 0};
char dir[] = {'<', '>', '^', 'v'};
main()
{
ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
cin >> n >> m;
f1 (i, n) f1 (j, m)
{
cin >> a[i][j];
if (a[i][j] == 'o') s[0] = i, s[1] = j;
if (a[i][j] == 'x') t[0] = i, t[1] = j;
d[i][j] = 1e4;
}
d[s[0]][s[1]] = 0;
deque <pair<int, int>> q;
q.push_front({s[0], s[1]});
while (q.size())
{
pair <int, int> p = q.front();
q.pop_front();
int u = p.first;
int v = p.second;
f0 (i, 4)
{
int uu = u + dx[i], vv = v + dy[i];
int cost = (dir[i] != a[u][v]);
if (a[u][v] == 'o') cost = 0;
if (uu > 0 && vv > 0 && uu <= n && vv <= m && a[uu][vv] != 'o')
{
if (d[u][v] + cost < d[uu][vv])
{
d[uu][vv] = d[u][v] + cost;
tr[uu][vv] = i;
if (cost == 0) q.push_front({uu, vv});
else q.push_back({uu, vv});
}
}
}
}
int u = t[0], v = t[1];
cout << d[u][v]; el;
while (u != s[0] || v != s[1])
{
int x = tr[u][v];
u -= dx[x];
v -= dy[x];
if (a[u][v] == 'o') break;
a[u][v] = dir[x];
}
for (int i=1; i<=n; i++)
{
for (int j=1; j<=m; j++) cout << a[i][j];
el;
}
}
Compilation message (stderr)
Main.cpp:17:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
17 | main()
| ^~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |