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 <iostream>
#include <algorithm>
#include <vector>
#include <set>
#include <cstring>
#include <queue>
#include <map>
#include <cmath>
#include <iomanip>
using namespace std;
#define ff first
#define sc second
#define pb push_back
#define ll long long
#define pll pair<ll, ll>
#define pii pair <int, int>
#define ull unsigned long long
// #define int long long
// #define int unsigned long long
const ll inf = 1e9 + 7;
const ll weirdMod = 998244353;
int dx[] = {-1, 0, 1, 0};
int dy[] = {0, 1, 0, -1};
char dir[] = {'N', 'E', 'S', 'W', 'N', 'E', 'S', 'W'};
char arr[505][505];
int n, m, d[505][505], x, y, a, b;
int get(char v, char u) {
int pos;
for (int i = 0; i < 4; i++)
if (dir[i] == v) pos = i;
int ans = 0;
for ( ; pos < 8; pos++) {
if (dir[pos] == u) break;
else ans++;
} return ans;
}
void dijkstra() {
d[1][1] = 0;
set<pair<int, pii>> st;
st.insert({0, {1, 1}});
pair<int, pii> cur;
int num = 0;
while (!st.empty()) {
cur = *st.begin();
st.erase(st.begin());
x = cur.sc.ff; y = cur.sc.sc;
if (arr[x][y] == 'X') continue;
for (int i = 0; i < 4; i++) {
a = x + dx[i];
b = y + dy[i];
if (a > 0 && b > 0 && a <= n && b <= m) {
if (d[a][b] > d[x][y] + get(arr[x][y], dir[i])) {
st.erase({d[a][b], {a, b}});
d[a][b] = d[x][y] + get(arr[x][y], dir[i]);
st.insert({d[a][b], {a, b}});
}
}
}
} if (d[n][m] == inf)
cout << -1;
else cout << d[n][m];
}
void solve() {
cin >> n >> m;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
cin >> arr[i][j]; d[i][j] = inf;
}
} if (n == 1 && m == 1) {
cout << 0; return;
} if (arr[1][1] == 'X') {
cout << -1; return;
} dijkstra();
}
int32_t main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr); cout.tie(nullptr);
int t = 1;
// cin >> t;
while (t--) {
solve();
cout << '\n';
} return 0;
}
Compilation message (stderr)
adventure.cpp: In function 'void dijkstra()':
adventure.cpp:47:9: warning: unused variable 'num' [-Wunused-variable]
47 | int num = 0;
| ^~~
adventure.cpp: In function 'int get(char, char)':
adventure.cpp:33:9: warning: 'pos' may be used uninitialized in this function [-Wmaybe-uninitialized]
33 | int pos;
| ^~~
adventure.cpp: In function 'void dijkstra()':
adventure.cpp:33:9: warning: 'pos' may be used uninitialized in this function [-Wmaybe-uninitialized]
adventure.cpp:33:9: warning: 'pos' may be used uninitialized in this function [-Wmaybe-uninitialized]
# | 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... |