This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#pragma GCC optimize("O3")
#include <bits/stdc++.h>
using namespace std;
#define ll long long
// #define int ll
#define fi first
#define se second
#define ld long double
#define pb push_back
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
char v[2010][2010];
bool vis[2010][2010];
int dd[2010][2010];
int N, M;
void solve(int start) {
priority_queue<pair<int, int>, vector<pair<int, int> >, greater<pair<int, int> > > q;
q.push({0, start});
while (!q.empty()) {
auto [d, pos] = q.top();
q.pop();
int x = pos / 10000, y = pos % 10000;
if (vis[x][y]) continue;
vis[x][y] = 1;
dd[x][y] = d;
if (v[x][y] == 'x') return;
if (x) q.push({d + (v[x][y] != '^'), (x - 1) * 10000 + y});
if (x < N - 1) q.push({d + (v[x][y] != 'v'), (x + 1) * 10000 + y});
if (y) q.push({d + (v[x][y] != '<'), x * 10000 + y - 1});
if (y < M - 1) q.push({d + (v[x][y] != '>'), x * 10000 + y + 1});
}
}
signed main() {
ios::sync_with_stdio(false);
cin.tie(0);
cin >> N >> M;
pair<int, int> start, fine;
for (int i = 0; i < N; i++) {
string a;
cin >> a;
for (int j = 0; j < M; j++) {
v[i][j] = a[j];
if (a[j] == 'o') start = {i, j};
if (a[j] == 'x') fine = {i, j};
}
}
solve(start.fi * 10000 + start.se);
// for (int i = 0; i < N; i++) {
// for (int j = 0; j < M; j++) {
// dd[i][j]--;
// cout << dd[i][j] << " ";
// }
// cout << endl;
// }
cout << dd[fine.fi][fine.se] - 1 << '\n';
}
Compilation message (stderr)
Main.cpp: In function 'void solve(int)':
Main.cpp:19:14: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
19 | auto [d, pos] = q.top();
| ^
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |