#include<bits/stdc++.h>
using namespace std;
int n, m;
const int MAXN = 2.5e5+10, inf = 2e9;
vector<int> G[MAXN], W[MAXN];
int dis[MAXN];
inline bool valid(int x, int y)
{
int i = x / m, j = x % m;
int iy = y / m, jy = y % m;
return (i>=0 and j>=0 and i<n and j<m and (i==iy or j==jy));
}
inline int moves(char a, char b)
{
string s = "NESW";
int m = 0, i;
for(i=0;i<4;i++)
if(s[i]==a)
break;
while(s[(i+m)%4]!=b)
m++;
return m;
}
void Dij()
{
set<pair<int,int> > s;
dis[0] = 0;
for(int i=0;i<n*m;i++)
s.insert({dis[i], i});
while(s.size())
{
int v = s.begin()->second;
s.erase(s.begin());
for(int i=0;i<G[v].size(); i++)
{
int u = G[v][i], w = W[v][i];
if(dis[v]+w < dis[u])
{
s.erase({dis[u], u});
dis[u] = dis[v] + w;
s.insert({dis[u], u});
}
}
}
}
int main()
{
cin >> n >> m;
for(int i=0;i<n;i++)
for(int j=0;j<m;j++)
{
int num = i*m + j;
dis[num] = inf;
char c;
cin >> c;
if(c == 'X')
continue;
string dir = "EWNS";
int d[] = {1, -1, -m, m};
for(int k=0;k<4;k++)
if(valid(num+d[k], num))
{
//cerr << "edge( " << num << ", " << num+d[k] << ")\n";
//cerr << "move( " << c << ", " << dir[k] << ") = " << moves(c, dir[k]) << endl;
G[num].push_back(num + d[k]);
W[num].push_back(moves(c, dir[k]));
}
}
Dij();
int l = n*m-1;
dis[l] = (dis[l]==inf)?-1:dis[l];
cout << dis[l] << endl;
return 0;
}
Compilation message
adventure.cpp: In function 'void Dij()':
adventure.cpp:38:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
38 | for(int i=0;i<G[v].size(); i++)
| ~^~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
11988 KB |
Output is correct |
2 |
Correct |
6 ms |
12052 KB |
Output is correct |
3 |
Correct |
6 ms |
12008 KB |
Output is correct |
4 |
Correct |
6 ms |
11988 KB |
Output is correct |
5 |
Correct |
6 ms |
11988 KB |
Output is correct |
6 |
Correct |
7 ms |
12116 KB |
Output is correct |
7 |
Correct |
8 ms |
11988 KB |
Output is correct |
8 |
Correct |
6 ms |
11988 KB |
Output is correct |
9 |
Correct |
7 ms |
12024 KB |
Output is correct |
10 |
Correct |
6 ms |
11988 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
11988 KB |
Output is correct |
2 |
Correct |
6 ms |
12052 KB |
Output is correct |
3 |
Correct |
6 ms |
12008 KB |
Output is correct |
4 |
Correct |
6 ms |
11988 KB |
Output is correct |
5 |
Correct |
6 ms |
11988 KB |
Output is correct |
6 |
Correct |
7 ms |
12116 KB |
Output is correct |
7 |
Correct |
8 ms |
11988 KB |
Output is correct |
8 |
Correct |
6 ms |
11988 KB |
Output is correct |
9 |
Correct |
7 ms |
12024 KB |
Output is correct |
10 |
Correct |
6 ms |
11988 KB |
Output is correct |
11 |
Correct |
5 ms |
11988 KB |
Output is correct |
12 |
Correct |
5 ms |
11988 KB |
Output is correct |
13 |
Correct |
5 ms |
11988 KB |
Output is correct |
14 |
Correct |
6 ms |
12052 KB |
Output is correct |
15 |
Correct |
6 ms |
11988 KB |
Output is correct |
16 |
Correct |
6 ms |
11988 KB |
Output is correct |
17 |
Correct |
6 ms |
11988 KB |
Output is correct |
18 |
Correct |
6 ms |
12056 KB |
Output is correct |
19 |
Correct |
6 ms |
11988 KB |
Output is correct |
20 |
Correct |
6 ms |
12052 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
11988 KB |
Output is correct |
2 |
Correct |
6 ms |
11988 KB |
Output is correct |
3 |
Correct |
6 ms |
12056 KB |
Output is correct |
4 |
Correct |
8 ms |
12116 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
12116 KB |
Output is correct |
2 |
Correct |
6 ms |
12044 KB |
Output is correct |
3 |
Correct |
5 ms |
12048 KB |
Output is correct |
4 |
Correct |
6 ms |
11988 KB |
Output is correct |
5 |
Correct |
6 ms |
12116 KB |
Output is correct |
6 |
Correct |
7 ms |
12092 KB |
Output is correct |
7 |
Correct |
6 ms |
11988 KB |
Output is correct |
8 |
Correct |
6 ms |
12052 KB |
Output is correct |
9 |
Correct |
7 ms |
12040 KB |
Output is correct |
10 |
Correct |
6 ms |
12048 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
11988 KB |
Output is correct |
2 |
Correct |
6 ms |
12052 KB |
Output is correct |
3 |
Correct |
6 ms |
12008 KB |
Output is correct |
4 |
Correct |
6 ms |
11988 KB |
Output is correct |
5 |
Correct |
6 ms |
11988 KB |
Output is correct |
6 |
Correct |
7 ms |
12116 KB |
Output is correct |
7 |
Correct |
8 ms |
11988 KB |
Output is correct |
8 |
Correct |
6 ms |
11988 KB |
Output is correct |
9 |
Correct |
7 ms |
12024 KB |
Output is correct |
10 |
Correct |
6 ms |
11988 KB |
Output is correct |
11 |
Correct |
5 ms |
11988 KB |
Output is correct |
12 |
Correct |
5 ms |
11988 KB |
Output is correct |
13 |
Correct |
5 ms |
11988 KB |
Output is correct |
14 |
Correct |
6 ms |
12052 KB |
Output is correct |
15 |
Correct |
6 ms |
11988 KB |
Output is correct |
16 |
Correct |
6 ms |
11988 KB |
Output is correct |
17 |
Correct |
6 ms |
11988 KB |
Output is correct |
18 |
Correct |
6 ms |
12056 KB |
Output is correct |
19 |
Correct |
6 ms |
11988 KB |
Output is correct |
20 |
Correct |
6 ms |
12052 KB |
Output is correct |
21 |
Correct |
5 ms |
11988 KB |
Output is correct |
22 |
Correct |
6 ms |
11988 KB |
Output is correct |
23 |
Correct |
6 ms |
12056 KB |
Output is correct |
24 |
Correct |
8 ms |
12116 KB |
Output is correct |
25 |
Correct |
6 ms |
12116 KB |
Output is correct |
26 |
Correct |
6 ms |
12044 KB |
Output is correct |
27 |
Correct |
5 ms |
12048 KB |
Output is correct |
28 |
Correct |
6 ms |
11988 KB |
Output is correct |
29 |
Correct |
6 ms |
12116 KB |
Output is correct |
30 |
Correct |
7 ms |
12092 KB |
Output is correct |
31 |
Correct |
6 ms |
11988 KB |
Output is correct |
32 |
Correct |
6 ms |
12052 KB |
Output is correct |
33 |
Correct |
7 ms |
12040 KB |
Output is correct |
34 |
Correct |
6 ms |
12048 KB |
Output is correct |
35 |
Correct |
22 ms |
14292 KB |
Output is correct |
36 |
Correct |
6 ms |
12276 KB |
Output is correct |
37 |
Correct |
26 ms |
14748 KB |
Output is correct |
38 |
Correct |
23 ms |
16160 KB |
Output is correct |
39 |
Correct |
235 ms |
37724 KB |
Output is correct |
40 |
Correct |
208 ms |
37504 KB |
Output is correct |
41 |
Correct |
110 ms |
37432 KB |
Output is correct |
42 |
Correct |
210 ms |
37524 KB |
Output is correct |
43 |
Correct |
196 ms |
40536 KB |
Output is correct |
44 |
Correct |
110 ms |
37472 KB |
Output is correct |