#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
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 |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
204 KB |
Output is correct |
4 |
Correct |
1 ms |
204 KB |
Output is correct |
5 |
Correct |
1 ms |
204 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
204 KB |
Output is correct |
4 |
Correct |
1 ms |
204 KB |
Output is correct |
5 |
Correct |
1 ms |
204 KB |
Output is correct |
6 |
Correct |
134 ms |
5664 KB |
Output is correct |
7 |
Correct |
167 ms |
7220 KB |
Output is correct |
8 |
Correct |
360 ms |
14196 KB |
Output is correct |
9 |
Correct |
746 ms |
27416 KB |
Output is correct |
10 |
Correct |
905 ms |
34112 KB |
Output is correct |
11 |
Correct |
1274 ms |
47144 KB |
Output is correct |