#define _CRT_SECURE_NO_WARNINGS
#include <bits/stdc++.h>
using namespace std;
#define forin for(int i = 1; i <= n; i++)
#define stforin for(int i = 0; i < n; i++)
#define forim for(int i = 1; i <= m; i++)
#define forjn for(int j = 1; j <= n; j++)
#define forch(j, n) for(int i = j; i <= n; i++)
#define forch2(i, j, n) for(int i = j; i <= n; i++)
#define forjm for(int j = 1; j <= m; j++)
#define lol long long
#define lb long double
#define all(a) (x).begin(), (x).end();
#define endl '\n'
#define debug cout << "Completed" << endl;
#define fix(n, m) cout << fixed; cout.precision(m); cout << n << endl
#define Yes cout << "YES" << endl
#define No cout << "NO" << endl
#define bads cout << -1 << endl
#define pll pair<lol, lol>
#define mod 1000000007
#define fst first
#define snd second
#define inf 1e15
#define tofix cin
; string sbuf;
ostringstream buf(sbuf);
istringstream atcin(sbuf);
lol gcd(lol a, lol b) {
    while (a != 0 && b != 0) if (a > b) a %= b; else b %= a;
    return a + b;
}
lol lcm(lol a, lol b) {
    return a / gcd(a, b) * b;
}
bool issqrt(lol n) {
    lb x = sqrt(n);
    if (x == (lol)x) return 1;
    return 0;
}
lol easy(lol n) {
    if (n == 1) return 0;
    for (int i = 2; i * i <= n; i++) if (n % i == 0) return 0;
    return 1;
}
string bin(lol v) {
    string ans;
    while (v != 0) {
        ans += to_string((v % 2));
        v /= 2;
    }
    return ans;
}
//priority_queue <pll, vector<pll>, greater<pll>> q
const long long N = 1e3 + 10;
lol m, n, d[N][N], x, y, di, dx[4] = { -1, 0, 1, 0 }, dy[4] = { 0, 1, 0, -1 }, el, tox, toy;
char dir[4] = { 'N', 'E', 'S', 'W' };
string s[N];
struct dk {
    lol x, y, d;
    bool operator > (const dk& v1) const {
        return d > v1.d;
    }
};
priority_queue <dk, vector<dk>, greater<dk>> q;
lol deika() {
    while (!q.empty()) {
        auto v = q.top();
        q.pop();
        x = v.x; y = v.y; di = v.d;
        if (x == m - 1 && y == n - 1) return di;
        if (di > d[x][y] || s[x][y] == 'X') continue;
        forch(0, 3) {
            tox = x + dx[i];
            toy = y + dy[i];
            if (tox < 0 || toy < 0 || tox >= m || toy >= n) continue;
            if (s[x][y] == dir[i]) el = 0;
            else if (s[x][y] == dir[(i + 3) % 4]) el = 1;
            else if (s[x][y] == dir[(i + 2) % 4]) el = 2;
            else el = 3;
            if (d[tox][toy] > di + el) {
                d[tox][toy] = di + el;
                q.push({ tox, toy, d[tox][toy] });
            }
        }
    }
    return -1;
}
int main() {
    cin >> m >> n;
    forch(0, m - 1) cin >> s[i];
    forch(0, m - 1) forch2(j, 0, n - 1) d[i][j] = inf;
    q.push({ 0, 0, 0 });
    d[0][0] = 0;
    cout << deika() << endl;
    return 0;
}
| # | 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... |