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;
#define X first
#define Y second
#define sz(a) (int)a.size()
#define ll long long
const int N = 1e7;
const int T = 20;
vector <int> dx = {-1, 0, 1, 0};
vector <int> dy = {0, -1, 0, 1};
vector <char> p = {'^', '<', 'v', '>'};
signed main()
{
// ifstream cin("input1.txt.4c");
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int n, m;
cin >> n >> m;
char A[n][m];
int stx, sty;
int endx, endy;
for(int i = 0; i < n; ++i)
{
for(int j = 0; j < m; j++)
{
cin >> A[i][j];
if(A[i][j] == 'o')
{
stx = i;
sty = j;
}
if(A[i][j] == 'x')
{
endx = i;
endy = j;
}
}
}
vector <vector <int> > d(n, vector <int> (m, 1e9));
vector <vector <int> > pred(n, vector <int> (m, -1));
d[stx][sty] = 0;
priority_queue <pair <int, pair <int, int> > > q;
q.push({0, {stx, sty}});
while(sz(q) != 0)
{
pair <int, int> t = q.top().Y;
int d1 = q.top().X;
q.pop();
if(d1 != -d[t.X][t.Y])
{
continue;
}
int x = t.X;
int y = t.Y;
for(int t = 0; t < 4; t++)
{
int d_new = d[x][y] + (p[t] != A[x][y] && A[x][y] != 'o');
int x1 = dx[t] + x;
int y1 = dy[t] + y;
if(x1 >= 0 && x1 < n && y1 >= 0 && y1 < m && d[x1][y1] > d_new)
{
d[x1][y1] = d_new;
pred[x1][y1] = t;
q.push({-d_new, {x1, y1}});
}
}
}
cout << d[endx][endy] << "\n";
pair <int, int> st = {endx, endy};
while(st != make_pair(stx, sty))
{
int k = pred[st.X][st.Y];
pair <int, int> st_new = {st.X - dx[k], st.Y - dy[k]};
if(st_new == make_pair(stx, sty))
{
break;
}
A[st_new.X][st_new.Y] = p[k];
st = st_new;
}
for(int i = 0; i < n; i++)
{
for(int j = 0; j < m; j++)
{
cout << A[i][j];
}
cout << "\n";
}
return 0;
}
Compilation message (stderr)
Main.cpp: In function 'int main()':
Main.cpp:69:25: warning: 'endy' may be used uninitialized in this function [-Wmaybe-uninitialized]
69 | cout << d[endx][endy] << "\n";
| ^
Main.cpp:69:19: warning: 'endx' may be used uninitialized in this function [-Wmaybe-uninitialized]
69 | cout << d[endx][endy] << "\n";
| ^
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |