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>
#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
using namespace std;
using ll = long long;
using ld = long double;
struct custom_hash {
static uint64_t splitmix64(uint64_t x) {
// http://xorshift.di.unimi.it/splitmix64.c
x += 0x9e3779b97f4a7c15;
x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9;
x = (x ^ (x >> 27)) * 0x94d049bb133111eb;
return x ^ (x >> 31);
}
size_t operator()(uint64_t x) const {
static const uint64_t FIXED_RANDOM = chrono::steady_clock::now().time_since_epoch().count();
return splitmix64(x + FIXED_RANDOM);
}
};
gp_hash_table<ll, ll, custom_hash> app;
//const int MOD = 998244353;
//const int MOD = 1e9 + 7;
const ll INF = 3e18;
vector<string> a;
ll n, m, k, q, x, y, z, w, h, T;
string s, t;
double p;
char conv(ll delx, ll dely) {
if (delx == 1) {
return 'S';
}
if (delx == -1) {
return 'N';
}
if (dely == 1) {
return 'E';
}
return 'W';
}
ll distan(char c1, char c2) {
vector<ll> here;
for (char c : {c1, c2}) {
if (c == 'N') here.push_back(0);
else if (c == 'E') here.push_back(1);
else if (c == 'S') here.push_back(2);
else here.push_back(3);
}
return (here[1] - here[0] + 4) % 4;
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(NULL);
cout.tie(NULL);
T = 1;
//cin >> T;
while (T--) {
cin >> n >> m;
a.assign(n, "");
for (int i = 0; i < n; i++) {
cin >> a[i];
}
priority_queue<pair<ll, pair<ll, ll>>, vector<pair<ll, pair<ll, ll>>>, greater<pair<ll, pair<ll, ll>>>> pq;
vector<vector<bool>> vis(n, vector<bool>(m));
vector<vector<ll>> dist(n, vector<ll>(m, INF));
dist[0][0] = 0;
pq.push({0, {0, 0}});
ll ans = -1;
while (!pq.empty()) {
pair<ll, pair<ll, ll>> top = pq.top();
pq.pop();
x = top.second.first;
y = top.second.second;
if (x == n - 1 && y == m - 1) {
ans = top.first;
break;
}
if (vis[x][y] || a[x][y] == 'X') continue;
vis[x][y] = true;
for (ll delx : {-1, 1}) {
ll dely = 0;
if (delx + x < 0 || delx + x >= n || dely + y < 0 || dely + y >= m
|| ((delx + x != n - 1 || dely + y != m - 1) && a[delx + x][dely + y] == 'X')) continue;
ll here = distan(a[x][y], conv(delx, dely));
if (dist[x][y] + here < dist[x + delx][y + dely]) {
dist[x + delx][y + dely] = dist[x][y] + here;
pq.push({dist[x + delx][y + dely], {x + delx, y + dely}});
}
}
for (ll dely : {-1, 1}) {
ll delx = 0;
if (delx + x < 0 || delx + x >= n || dely + y < 0 || dely + y >= m
|| ((delx + x != n - 1 || dely + y != m - 1) && a[delx + x][dely + y] == 'X')) continue;
ll here = distan(a[x][y], conv(delx, dely));
if (dist[x][y] + here < dist[x + delx][y + dely]) {
dist[x + delx][y + dely] = dist[x][y] + here;
pq.push({dist[x + delx][y + dely], {x + delx, y + dely}});
}
}
}
printf("%lld\n", ans);
}
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |