#include <iostream>
#include <vector>
#include <queue>
using namespace std;
typedef pair < int, int > PII;
const int NMAX = 502, INF = 1e9;
const int dx[4] = {-1, 0, 1, 0};
const int dy[4] = {0, 1, 0, -1};
char v[NMAX][NMAX];
int d[NMAX * NMAX], sel[NMAX * NMAX];
int N, M;
vector < PII > G[NMAX * NMAX];
auto cmp = [](PII a, PII b)
{
if(!(a.first < b.first))
return 1;
return 0;
};
priority_queue <PII, vector <PII>, decltype(cmp)> H(cmp);
inline int code(PII a)
{
if(!(1 <= a.first && a.first <= N && 1 <= a.second && a.second <= M))
return 0;
return (a.first - 1) * M + a.second;
}
inline void Read()
{
cin.tie(nullptr);
ios_base :: sync_with_stdio(false);
cin >> N >> M;
for(int i = 1; i <= N; ++i)
{
cin >> (v[i] + 1);
for(int j = 1; j <= M; ++j)
{
if(v[i][j] == 'X')
continue;
PII est = {i, j + 1}, west = {i, j - 1}, sud = {i + 1, j}, nord = {i - 1, j};
int ct = code({i, j});
if(v[i][j] == 'E')
{
G[ct].push_back({0, code (est)});
G[ct].push_back({1, code (sud)});
G[ct].push_back({2, code (west)});
G[ct].push_back({3, code (nord)});
}
if(v[i][j] == 'S')
{
G[ct].push_back({0, code (sud)});
G[ct].push_back({1, code (west)});
G[ct].push_back({2, code (nord)});
G[ct].push_back({3, code (est)});
}
if(v[i][j] == 'W')
{
G[ct].push_back({0, code (west)});
G[ct].push_back({1, code (nord)});
G[ct].push_back({2, code (est)});
G[ct].push_back({3, code (sud)});
}
if(v[i][j] == 'N')
{
G[ct].push_back({0, code (nord)});
G[ct].push_back({1, code (est)});
G[ct].push_back({2, code (sud)});
G[ct].push_back({3, code (west)});
}
}
}
return;
}
inline void Dijkstra(int start)
{
for(int i = 1; i <= N * M; ++i)
d[i] = INF;
for(auto it: G[start])
if(it.second)
H.push(it), d[it.second] = it.first;
d[start] = 0;
sel[start] = 1;
while(!H.empty())
{
while(!H.empty() && sel[H.top().second])
H.pop();
if(H.empty())
return;
int node = H.top().second;
sel[node] = 1;
H.pop();
for(auto it: G[node])
if(it.second)
if(d[node] + it.first < d[it.second])
d[it.second] = d[node] + it.first, H.push({d[it.second], it.second});
}
return;
}
int main()
{
Read();
Dijkstra(1);
cout << (d[N * M] == INF ? -1 : d[N * M]) << '\n';
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
6220 KB |
Output is correct |
2 |
Correct |
4 ms |
6232 KB |
Output is correct |
3 |
Correct |
3 ms |
6304 KB |
Output is correct |
4 |
Correct |
3 ms |
6220 KB |
Output is correct |
5 |
Correct |
3 ms |
6220 KB |
Output is correct |
6 |
Correct |
3 ms |
6236 KB |
Output is correct |
7 |
Correct |
3 ms |
6220 KB |
Output is correct |
8 |
Correct |
3 ms |
6236 KB |
Output is correct |
9 |
Correct |
3 ms |
6240 KB |
Output is correct |
10 |
Correct |
4 ms |
6228 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
6220 KB |
Output is correct |
2 |
Correct |
4 ms |
6232 KB |
Output is correct |
3 |
Correct |
3 ms |
6304 KB |
Output is correct |
4 |
Correct |
3 ms |
6220 KB |
Output is correct |
5 |
Correct |
3 ms |
6220 KB |
Output is correct |
6 |
Correct |
3 ms |
6236 KB |
Output is correct |
7 |
Correct |
3 ms |
6220 KB |
Output is correct |
8 |
Correct |
3 ms |
6236 KB |
Output is correct |
9 |
Correct |
3 ms |
6240 KB |
Output is correct |
10 |
Correct |
4 ms |
6228 KB |
Output is correct |
11 |
Correct |
3 ms |
6220 KB |
Output is correct |
12 |
Correct |
3 ms |
6220 KB |
Output is correct |
13 |
Correct |
4 ms |
6220 KB |
Output is correct |
14 |
Correct |
3 ms |
6220 KB |
Output is correct |
15 |
Correct |
5 ms |
6264 KB |
Output is correct |
16 |
Correct |
3 ms |
6220 KB |
Output is correct |
17 |
Correct |
4 ms |
6220 KB |
Output is correct |
18 |
Correct |
3 ms |
6240 KB |
Output is correct |
19 |
Correct |
3 ms |
6220 KB |
Output is correct |
20 |
Correct |
5 ms |
6220 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
6220 KB |
Output is correct |
2 |
Correct |
4 ms |
6220 KB |
Output is correct |
3 |
Correct |
4 ms |
6220 KB |
Output is correct |
4 |
Correct |
3 ms |
6220 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
6220 KB |
Output is correct |
2 |
Correct |
3 ms |
6240 KB |
Output is correct |
3 |
Correct |
4 ms |
6244 KB |
Output is correct |
4 |
Correct |
3 ms |
6244 KB |
Output is correct |
5 |
Correct |
4 ms |
6220 KB |
Output is correct |
6 |
Correct |
4 ms |
6240 KB |
Output is correct |
7 |
Correct |
3 ms |
6220 KB |
Output is correct |
8 |
Correct |
3 ms |
6220 KB |
Output is correct |
9 |
Correct |
4 ms |
6360 KB |
Output is correct |
10 |
Correct |
4 ms |
6220 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
6220 KB |
Output is correct |
2 |
Correct |
4 ms |
6232 KB |
Output is correct |
3 |
Correct |
3 ms |
6304 KB |
Output is correct |
4 |
Correct |
3 ms |
6220 KB |
Output is correct |
5 |
Correct |
3 ms |
6220 KB |
Output is correct |
6 |
Correct |
3 ms |
6236 KB |
Output is correct |
7 |
Correct |
3 ms |
6220 KB |
Output is correct |
8 |
Correct |
3 ms |
6236 KB |
Output is correct |
9 |
Correct |
3 ms |
6240 KB |
Output is correct |
10 |
Correct |
4 ms |
6228 KB |
Output is correct |
11 |
Correct |
3 ms |
6220 KB |
Output is correct |
12 |
Correct |
3 ms |
6220 KB |
Output is correct |
13 |
Correct |
4 ms |
6220 KB |
Output is correct |
14 |
Correct |
3 ms |
6220 KB |
Output is correct |
15 |
Correct |
5 ms |
6264 KB |
Output is correct |
16 |
Correct |
3 ms |
6220 KB |
Output is correct |
17 |
Correct |
4 ms |
6220 KB |
Output is correct |
18 |
Correct |
3 ms |
6240 KB |
Output is correct |
19 |
Correct |
3 ms |
6220 KB |
Output is correct |
20 |
Correct |
5 ms |
6220 KB |
Output is correct |
21 |
Correct |
4 ms |
6220 KB |
Output is correct |
22 |
Correct |
4 ms |
6220 KB |
Output is correct |
23 |
Correct |
4 ms |
6220 KB |
Output is correct |
24 |
Correct |
3 ms |
6220 KB |
Output is correct |
25 |
Correct |
3 ms |
6220 KB |
Output is correct |
26 |
Correct |
3 ms |
6240 KB |
Output is correct |
27 |
Correct |
4 ms |
6244 KB |
Output is correct |
28 |
Correct |
3 ms |
6244 KB |
Output is correct |
29 |
Correct |
4 ms |
6220 KB |
Output is correct |
30 |
Correct |
4 ms |
6240 KB |
Output is correct |
31 |
Correct |
3 ms |
6220 KB |
Output is correct |
32 |
Correct |
3 ms |
6220 KB |
Output is correct |
33 |
Correct |
4 ms |
6360 KB |
Output is correct |
34 |
Correct |
4 ms |
6220 KB |
Output is correct |
35 |
Correct |
10 ms |
7364 KB |
Output is correct |
36 |
Correct |
4 ms |
6240 KB |
Output is correct |
37 |
Correct |
12 ms |
7512 KB |
Output is correct |
38 |
Correct |
8 ms |
8052 KB |
Output is correct |
39 |
Correct |
64 ms |
18040 KB |
Output is correct |
40 |
Correct |
66 ms |
17996 KB |
Output is correct |
41 |
Correct |
30 ms |
16956 KB |
Output is correct |
42 |
Correct |
64 ms |
17988 KB |
Output is correct |
43 |
Correct |
88 ms |
22400 KB |
Output is correct |
44 |
Correct |
30 ms |
17028 KB |
Output is correct |