#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef pair<ll, ll> ii;
typedef pair<ll, ii> iii;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<ii> vii;
typedef vector<vi> vvi;
typedef vector<vll> vvll;
typedef vector<vii> vvii;
#define task "test"
#define fastIO ios::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL)
#define forw(i, l, r) for( ll i = (l) ; i < (r) ; i++ )
#define forb(i, r, l) for( ll i = (r) ; i >= (l) ; i-- )
#define numBit(x) (__builtin_popcountll(1ll * (x)))
#define getBit(x, i) ((x) >> (i) & 1)
#define sz(x) (int)x.size()
#define mp make_pair
#define fi first
#define se second
#define pb push_back
#define pf push_front
#define pob pop_back
#define pof pop_front
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define debug(x) cerr << #x << " = " << x << '\n';
const int N = 2e3 + 7;
const ll inf = 0x3f3f3f3f;
int dx[4] = {-1, 0, 1, 0};
int dy[4] = {0, -1, 0, 1};
int n, m, d[N][N];
ii st, des, tr[N][N];
char ch[N][N];
int get(char ch) {
if (ch == '^') return 0;
if (ch == '<') return 1;
if (ch == 'v') return 2;
return 3;
}
int getindx(ii x, ii y) {
forw(i, 0, 4) {
ii tmp = mp(x.fi + dx[i], x.se + dy[i]);
if (tmp == y) return i;
}
return 0;
}
bool valid(ii u) {
return u.fi >= 0 && u.fi < n && u.se >= 0 && u.se < m;
}
void dijktra() {
memset(d, 0x3f, sizeof d);
priority_queue<iii, vector<iii>, greater<iii>> pq;
pq.push(mp(0, st));
d[st.fi][st.se] = 0;
while (sz(pq)) {
ii u = pq.top().se;
int w = pq.top().fi;
pq.pop();
int idx = get(ch[u.fi][u.se]);
if (ch[u.fi][u.se] == 'o') idx = -1;
if (ch[u.fi][u.se] == 'x') return;
forw(i, 0, 4) {
ii v = mp(u.fi + dx[i], u.se + dy[i]);
if (valid(v) && d[v.fi][v.se] > d[u.fi][u.se] + (i != idx)) {
d[v.fi][v.se] = d[u.fi][u.se] + (i != idx);
tr[v.fi][v.se] = mp(u.fi, u.se);
pq.push(mp(d[v.fi][v.se], mp(v.fi, v.se)));
}
}
}
}
void trace() {
des = tr[des.fi][des.se];
for (; tr[des.fi][des.se].fi != -1; des = tr[des.fi][des.se]) {
// cerr << x << ' ' << y << endl;
int idx = getindx(mp(des.fi, des.se), tr[des.fi][des.se]);
if (!idx) ch[des.fi][des.se] = 'v';
else if (idx == 1) ch[des.fi][des.se] = '>';
else if (idx == 2) ch[des.fi][des.se] = '^';
else ch[des.fi][des.se] = '<';
}
}
int main() {
fastIO;
memset(tr, -1, sizeof tr);
cin >> n >> m;
forw(i, 0, n) forw(j, 0, m) {
cin >> ch[i][j];
if (ch[i][j] == 'o') st = mp(i, j);
if (ch[i][j] == 'x') des = mp(i, j);
}
dijktra();
cout << d[n - 1][m - 1] << endl;
trace();
forw(i, 0, n) {
forw(j, 0, m) cout << ch[i][j];
cout << '\n';
}
}
Compilation message
Main.cpp: In function 'void dijktra()':
Main.cpp:71:9: warning: unused variable 'w' [-Wunused-variable]
71 | int w = pq.top().fi;
| ^
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
38 ms |
79044 KB |
Integer 1061109567 violates the range [0, 63] |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
38 ms |
79044 KB |
Integer 1061109567 violates the range [0, 63] |
2 |
Halted |
0 ms |
0 KB |
- |