#include<bits/stdc++.h>
// #pragma GCC target ("avx,avx2,fma")
// #pragma GCC optimize("Ofast")
// #pragma GCC optimize("unroll-loops")
typedef long long ll;
typedef long double ld;
#define SPEED ios_base::sync_with_stdio(false); cin.tie(0), cout.tie(0)
#define rall(v) (v).rbegin(),(v).rend()
#define all(v) (v).begin(),(v).end()
#define OK cerr<<"OK"<<endl<<flush
#define pii pair<int, int>
#define pll pair<ll, ll>
#define pb push_back
#define F first
#define S second
#define y0 jahdakdh
#define y1 jahsadakdakdh
#define endl '\n'
using namespace std;
const ll MOD=1e9+7;
// mt19937 rng(std::chrono::system_clock::now().time_since_epoch().count());
int n, m, r[4][4]={ {0,1,2,3},{3,0,1,2},{2,3,0,1},{1,2,3,0} }, dis[505][505];
char a[505][505];
vector<pair<pii, int> > g[505][505];
map<char, int>c;
bool used[505][505];
priority_queue<pair<int, pii> >q;
int main()
{
SPEED;
cin>>n>>m;
c['N']=0; c['E']=1; c['S']=2; c['W']=3;
for(int i=1; i<=n; i++)
for(int j=1; j<=m; j++)
cin>>a[i][j];
for(int i=1; i<=n; i++)
for(int j=1; j<=m; j++)
{
if(a[i][j]=='X') continue;
if(i+1<=n) g[i][j].pb({{i+1, j}, r[c[a[i][j]]][2]});
if(j+1<=m) g[i][j].pb({{i, j+1}, r[c[a[i][j]]][1]});
if(i-1>=1) g[i][j].pb({{i-1, j}, r[c[a[i][j]]][0]});
if(j-1>=1) g[i][j].pb({{i, j-1}, r[c[a[i][j]]][3]});
}
memset(dis, 63, sizeof(dis));
dis[1][1]=0; q.push({0, {1, 1}});
while(!q.empty())
{
int x=q.top().S.F, y=q.top().S.S; q.pop();
if(used[x][y]) continue;
used[x][y]=1;
for(pair<pii, int> u : g[x][y])
{
if(dis[x][y]+u.S<dis[u.F.F][u.F.S])
{
dis[u.F.F][u.F.S]=dis[x][y]+u.S;
q.push({-dis[u.F.F][u.F.S], {u.F.F, u.F.S}});
}
}
}/*
for(int i=1; i<=n; i++, cout<<endl)
for(int j=1; j<=m; j++)
cout<<dis[i][j]<<' ';*/
if(dis[n][m]==1061109567) cout<<-1<<endl;
else cout<<dis[n][m]<<endl;
return 0;
}
/*
N - 0 - up
E - 1 - right →
S - 2 - down
W - 3 - left ←
*/
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
7244 KB |
Output is correct |
2 |
Correct |
5 ms |
7244 KB |
Output is correct |
3 |
Correct |
4 ms |
7244 KB |
Output is correct |
4 |
Correct |
4 ms |
7244 KB |
Output is correct |
5 |
Correct |
5 ms |
7316 KB |
Output is correct |
6 |
Correct |
5 ms |
7244 KB |
Output is correct |
7 |
Correct |
5 ms |
7244 KB |
Output is correct |
8 |
Correct |
4 ms |
7244 KB |
Output is correct |
9 |
Correct |
5 ms |
7244 KB |
Output is correct |
10 |
Correct |
4 ms |
7244 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
7244 KB |
Output is correct |
2 |
Correct |
5 ms |
7244 KB |
Output is correct |
3 |
Correct |
4 ms |
7244 KB |
Output is correct |
4 |
Correct |
4 ms |
7244 KB |
Output is correct |
5 |
Correct |
5 ms |
7316 KB |
Output is correct |
6 |
Correct |
5 ms |
7244 KB |
Output is correct |
7 |
Correct |
5 ms |
7244 KB |
Output is correct |
8 |
Correct |
4 ms |
7244 KB |
Output is correct |
9 |
Correct |
5 ms |
7244 KB |
Output is correct |
10 |
Correct |
4 ms |
7244 KB |
Output is correct |
11 |
Correct |
4 ms |
7244 KB |
Output is correct |
12 |
Correct |
5 ms |
7244 KB |
Output is correct |
13 |
Correct |
4 ms |
7244 KB |
Output is correct |
14 |
Correct |
4 ms |
7244 KB |
Output is correct |
15 |
Correct |
4 ms |
7244 KB |
Output is correct |
16 |
Correct |
5 ms |
7244 KB |
Output is correct |
17 |
Correct |
5 ms |
7244 KB |
Output is correct |
18 |
Correct |
5 ms |
7244 KB |
Output is correct |
19 |
Correct |
5 ms |
7244 KB |
Output is correct |
20 |
Correct |
5 ms |
7244 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
7244 KB |
Output is correct |
2 |
Correct |
5 ms |
7244 KB |
Output is correct |
3 |
Correct |
4 ms |
7244 KB |
Output is correct |
4 |
Correct |
4 ms |
7244 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
7244 KB |
Output is correct |
2 |
Correct |
5 ms |
7244 KB |
Output is correct |
3 |
Correct |
5 ms |
7244 KB |
Output is correct |
4 |
Correct |
5 ms |
7244 KB |
Output is correct |
5 |
Correct |
5 ms |
7244 KB |
Output is correct |
6 |
Correct |
5 ms |
7244 KB |
Output is correct |
7 |
Correct |
5 ms |
7244 KB |
Output is correct |
8 |
Correct |
6 ms |
7244 KB |
Output is correct |
9 |
Correct |
5 ms |
7244 KB |
Output is correct |
10 |
Correct |
4 ms |
7244 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
7244 KB |
Output is correct |
2 |
Correct |
5 ms |
7244 KB |
Output is correct |
3 |
Correct |
4 ms |
7244 KB |
Output is correct |
4 |
Correct |
4 ms |
7244 KB |
Output is correct |
5 |
Correct |
5 ms |
7316 KB |
Output is correct |
6 |
Correct |
5 ms |
7244 KB |
Output is correct |
7 |
Correct |
5 ms |
7244 KB |
Output is correct |
8 |
Correct |
4 ms |
7244 KB |
Output is correct |
9 |
Correct |
5 ms |
7244 KB |
Output is correct |
10 |
Correct |
4 ms |
7244 KB |
Output is correct |
11 |
Correct |
4 ms |
7244 KB |
Output is correct |
12 |
Correct |
5 ms |
7244 KB |
Output is correct |
13 |
Correct |
4 ms |
7244 KB |
Output is correct |
14 |
Correct |
4 ms |
7244 KB |
Output is correct |
15 |
Correct |
4 ms |
7244 KB |
Output is correct |
16 |
Correct |
5 ms |
7244 KB |
Output is correct |
17 |
Correct |
5 ms |
7244 KB |
Output is correct |
18 |
Correct |
5 ms |
7244 KB |
Output is correct |
19 |
Correct |
5 ms |
7244 KB |
Output is correct |
20 |
Correct |
5 ms |
7244 KB |
Output is correct |
21 |
Correct |
5 ms |
7244 KB |
Output is correct |
22 |
Correct |
5 ms |
7244 KB |
Output is correct |
23 |
Correct |
4 ms |
7244 KB |
Output is correct |
24 |
Correct |
4 ms |
7244 KB |
Output is correct |
25 |
Correct |
5 ms |
7244 KB |
Output is correct |
26 |
Correct |
5 ms |
7244 KB |
Output is correct |
27 |
Correct |
5 ms |
7244 KB |
Output is correct |
28 |
Correct |
5 ms |
7244 KB |
Output is correct |
29 |
Correct |
5 ms |
7244 KB |
Output is correct |
30 |
Correct |
5 ms |
7244 KB |
Output is correct |
31 |
Correct |
5 ms |
7244 KB |
Output is correct |
32 |
Correct |
6 ms |
7244 KB |
Output is correct |
33 |
Correct |
5 ms |
7244 KB |
Output is correct |
34 |
Correct |
4 ms |
7244 KB |
Output is correct |
35 |
Correct |
13 ms |
8500 KB |
Output is correct |
36 |
Correct |
5 ms |
7372 KB |
Output is correct |
37 |
Correct |
15 ms |
8652 KB |
Output is correct |
38 |
Correct |
12 ms |
9508 KB |
Output is correct |
39 |
Correct |
106 ms |
20368 KB |
Output is correct |
40 |
Correct |
108 ms |
20324 KB |
Output is correct |
41 |
Correct |
48 ms |
19968 KB |
Output is correct |
42 |
Correct |
111 ms |
20388 KB |
Output is correct |
43 |
Correct |
135 ms |
26448 KB |
Output is correct |
44 |
Correct |
47 ms |
20036 KB |
Output is correct |