#include<bits/stdc++.h>
using namespace std;
//#define int long long
#define ll long long
#define pii pair<int,int>
#define F first
#define S second
#define endl '\n'
#define pb push_back
#define sz(a) (int)a.size()
#define all(a) a.begin(),a.end()
const int mod = 1e9 + 7;
const int N = 502*502;
const ll inf = 1e18;
vector<pii> node[N];
int dis[N];
bool vis[N];
int nm[502][502];
int32_t main(){
ios_base::sync_with_stdio(0);cin.tie(0);
int n,m;
cin >> n >> m;
int t = 1;
for (int i=1;i<=n;i++){
for (int j=1;j<=m;j++){
nm[i][j] = t++;
}
}
for (int i=1;i<=n;i++){
for (int j=1;j<=m;j++){
char x;
cin >> x;
int cur = nm[i][j];
int u = nm[i-1][j];
int r = nm[i][j+1];
int d = nm[i+1][j];
int l = nm[i][j-1];
if (x=='N'){
node[cur].pb({u,0});
node[cur].pb({r,1});
node[cur].pb({d,2});
node[cur].pb({l,3});
}
if (x=='E'){
node[cur].pb({r,0});
node[cur].pb({d,1});
node[cur].pb({l,2});
node[cur].pb({u,3});
}
if (x=='S'){
node[cur].pb({d,0});
node[cur].pb({l,1});
node[cur].pb({u,2});
node[cur].pb({r,3});
}
if (x=='W'){
node[cur].pb({l,0});
node[cur].pb({u,1});
node[cur].pb({r,2});
node[cur].pb({d,3});
}
}
}
memset(dis,0x3f,sizeof(dis));
priority_queue<pii> pq;
pq.push({0,nm[1][1]});
while (sz(pq)){
int u = pq.top().S;
int v = -pq.top().F;
pq.pop();
if (vis[u]) continue;
dis[u] = v;
vis[u] = 1;
for (auto [it,c] : node[u]){
if (v+c < dis[it]){
dis[it] = v+c;
pq.push({-dis[it],it});
}
}
}
if (dis[nm[n][m]]>=1e9) cout << -1 << endl;
else cout << dis[nm[n][m]] << endl;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
7252 KB |
Output is correct |
2 |
Correct |
4 ms |
7252 KB |
Output is correct |
3 |
Correct |
4 ms |
7252 KB |
Output is correct |
4 |
Correct |
5 ms |
7260 KB |
Output is correct |
5 |
Correct |
4 ms |
7176 KB |
Output is correct |
6 |
Correct |
4 ms |
7196 KB |
Output is correct |
7 |
Correct |
4 ms |
7124 KB |
Output is correct |
8 |
Correct |
4 ms |
7252 KB |
Output is correct |
9 |
Correct |
4 ms |
7144 KB |
Output is correct |
10 |
Correct |
4 ms |
7124 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
7252 KB |
Output is correct |
2 |
Correct |
4 ms |
7252 KB |
Output is correct |
3 |
Correct |
4 ms |
7252 KB |
Output is correct |
4 |
Correct |
5 ms |
7260 KB |
Output is correct |
5 |
Correct |
4 ms |
7176 KB |
Output is correct |
6 |
Correct |
4 ms |
7196 KB |
Output is correct |
7 |
Correct |
4 ms |
7124 KB |
Output is correct |
8 |
Correct |
4 ms |
7252 KB |
Output is correct |
9 |
Correct |
4 ms |
7144 KB |
Output is correct |
10 |
Correct |
4 ms |
7124 KB |
Output is correct |
11 |
Correct |
4 ms |
7124 KB |
Output is correct |
12 |
Correct |
4 ms |
7252 KB |
Output is correct |
13 |
Correct |
5 ms |
7252 KB |
Output is correct |
14 |
Correct |
4 ms |
7124 KB |
Output is correct |
15 |
Correct |
4 ms |
7140 KB |
Output is correct |
16 |
Correct |
4 ms |
7252 KB |
Output is correct |
17 |
Correct |
4 ms |
7252 KB |
Output is correct |
18 |
Correct |
5 ms |
7124 KB |
Output is correct |
19 |
Correct |
4 ms |
7144 KB |
Output is correct |
20 |
Correct |
5 ms |
7252 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
7124 KB |
Output is correct |
2 |
Correct |
4 ms |
7124 KB |
Output is correct |
3 |
Correct |
3 ms |
7124 KB |
Output is correct |
4 |
Correct |
4 ms |
7192 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
7252 KB |
Output is correct |
2 |
Correct |
4 ms |
7252 KB |
Output is correct |
3 |
Correct |
5 ms |
7152 KB |
Output is correct |
4 |
Correct |
4 ms |
7252 KB |
Output is correct |
5 |
Correct |
4 ms |
7252 KB |
Output is correct |
6 |
Correct |
4 ms |
7252 KB |
Output is correct |
7 |
Correct |
6 ms |
7172 KB |
Output is correct |
8 |
Correct |
4 ms |
7124 KB |
Output is correct |
9 |
Correct |
4 ms |
7144 KB |
Output is correct |
10 |
Correct |
4 ms |
7124 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
7252 KB |
Output is correct |
2 |
Correct |
4 ms |
7252 KB |
Output is correct |
3 |
Correct |
4 ms |
7252 KB |
Output is correct |
4 |
Correct |
5 ms |
7260 KB |
Output is correct |
5 |
Correct |
4 ms |
7176 KB |
Output is correct |
6 |
Correct |
4 ms |
7196 KB |
Output is correct |
7 |
Correct |
4 ms |
7124 KB |
Output is correct |
8 |
Correct |
4 ms |
7252 KB |
Output is correct |
9 |
Correct |
4 ms |
7144 KB |
Output is correct |
10 |
Correct |
4 ms |
7124 KB |
Output is correct |
11 |
Correct |
4 ms |
7124 KB |
Output is correct |
12 |
Correct |
4 ms |
7252 KB |
Output is correct |
13 |
Correct |
5 ms |
7252 KB |
Output is correct |
14 |
Correct |
4 ms |
7124 KB |
Output is correct |
15 |
Correct |
4 ms |
7140 KB |
Output is correct |
16 |
Correct |
4 ms |
7252 KB |
Output is correct |
17 |
Correct |
4 ms |
7252 KB |
Output is correct |
18 |
Correct |
5 ms |
7124 KB |
Output is correct |
19 |
Correct |
4 ms |
7144 KB |
Output is correct |
20 |
Correct |
5 ms |
7252 KB |
Output is correct |
21 |
Correct |
3 ms |
7124 KB |
Output is correct |
22 |
Correct |
4 ms |
7124 KB |
Output is correct |
23 |
Correct |
3 ms |
7124 KB |
Output is correct |
24 |
Correct |
4 ms |
7192 KB |
Output is correct |
25 |
Correct |
4 ms |
7252 KB |
Output is correct |
26 |
Correct |
4 ms |
7252 KB |
Output is correct |
27 |
Correct |
5 ms |
7152 KB |
Output is correct |
28 |
Correct |
4 ms |
7252 KB |
Output is correct |
29 |
Correct |
4 ms |
7252 KB |
Output is correct |
30 |
Correct |
4 ms |
7252 KB |
Output is correct |
31 |
Correct |
6 ms |
7172 KB |
Output is correct |
32 |
Correct |
4 ms |
7124 KB |
Output is correct |
33 |
Correct |
4 ms |
7144 KB |
Output is correct |
34 |
Correct |
4 ms |
7124 KB |
Output is correct |
35 |
Correct |
12 ms |
8180 KB |
Output is correct |
36 |
Correct |
4 ms |
7272 KB |
Output is correct |
37 |
Correct |
11 ms |
8452 KB |
Output is correct |
38 |
Correct |
12 ms |
9332 KB |
Output is correct |
39 |
Correct |
73 ms |
18048 KB |
Output is correct |
40 |
Correct |
79 ms |
18124 KB |
Output is correct |
41 |
Correct |
37 ms |
17804 KB |
Output is correct |
42 |
Correct |
84 ms |
18120 KB |
Output is correct |
43 |
Correct |
97 ms |
22444 KB |
Output is correct |
44 |
Correct |
36 ms |
17752 KB |
Output is correct |