// In the name of God
// Hope is last to die
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
#define pb push_back
// #define int long long
#define S second
#define F first
#define mp make_pair
#define smax(x, y) (x) = max((x), (y))
#define smin(x, y) (x) = min((x), (y))
#define all(x) (x).begin(), (x).end()
#define len(x) ((int)(x).size())
const int maxn = 2000 + 5;
const int inf = 1e9 + 7;
int n, m, a, b, c, d, ti[maxn][maxn], dist[maxn][maxn];
int dx[] = {0, 1, 0, -1};
int dy[] = {1, 0, -1, 0};
int par[maxn][maxn];
string s[maxn];
deque<pii> q;
bool is_val(int x, int y){
return min(x, y) >= 0 && x < n && y < m;
}
void bfs(){
memset(dist, 63, sizeof dist);
memset(par, -1, sizeof par);
dist[a][b] = 0;
for(int i = 0; i < 4; i++){
int xx = a + dx[i];
int yy = b + dy[i];
if(!is_val(xx, yy)) continue;
dist[xx][yy] = 0;
q.push_front(mp(xx, yy));
}
while(q.size()){
int x = q.front().F, y = q.front().S;
q.pop_front();
if(x == c && y == d) break;
for(int i = 0; i < 4; i++){
int xx = x + dx[i];
int yy = y + dy[i];
if(!is_val(xx, yy)) continue;
int w = (ti[x][y] != i);
if(dist[xx][yy] > w + dist[x][y]){
dist[xx][yy] = w + dist[x][y];
par[xx][yy] = i;
if(w) q.push_back(mp(xx, yy));
else q.push_front(mp(xx, yy));
}
}
}
}
int32_t main()
{
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
cin >> n >> m;
for(int i = 0; i < n; i++){
cin >> s[i];
for(int j = 0; j < m; j++){
if(s[i][j] == '.') ti[i][j] = 4;
if(s[i][j] == '>') ti[i][j] = 0;
if(s[i][j] == '<') ti[i][j] = 2;
if(s[i][j] == '^') ti[i][j] = 3;
if(s[i][j] == 'v') ti[i][j] = 1;
if(s[i][j] == 'o'){
ti[i][j] = 5;
a = i, b = j;
}
if(s[i][j] == 'x'){
ti[i][j] = 6;
c = i, d = j;
}
}
}
bfs();
cout << dist[c][d] << '\n';
while(par[c][d] != -1){
int dir = par[c][d];
c = c - dx[dir];
d = d - dy[dir];
if(dir == 0) s[c][d] = '>';
if(dir == 1) s[c][d] = 'v';
if(dir == 2) s[c][d] = '<';
if(dir == 3) s[c][d] = '^';
}
for(int i = 0; i < n; i++){
for(int j = 0; j < m; j++) cout << s[i][j];
cout << '\n';
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
33368 KB |
Output is correct |
2 |
Correct |
4 ms |
33372 KB |
Output is correct |
3 |
Correct |
4 ms |
33372 KB |
Output is correct |
4 |
Correct |
5 ms |
33372 KB |
Output is correct |
5 |
Correct |
4 ms |
33372 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
33368 KB |
Output is correct |
2 |
Correct |
4 ms |
33372 KB |
Output is correct |
3 |
Correct |
4 ms |
33372 KB |
Output is correct |
4 |
Correct |
5 ms |
33372 KB |
Output is correct |
5 |
Correct |
4 ms |
33372 KB |
Output is correct |
6 |
Correct |
28 ms |
38368 KB |
Output is correct |
7 |
Correct |
29 ms |
41564 KB |
Output is correct |
8 |
Correct |
69 ms |
44252 KB |
Output is correct |
9 |
Correct |
107 ms |
50640 KB |
Output is correct |
10 |
Correct |
164 ms |
52080 KB |
Output is correct |
11 |
Correct |
209 ms |
60240 KB |
Output is correct |