#include<bits/stdc++.h>
using namespace std;
typedef pair<int,int> ii;
typedef vector<int>vi;
typedef vector<ii> vii;
vector<vii> vec;
vi dist;
priority_queue<ii,vii,greater<ii>> pq;
#define INF 1000000007
void dijkstra(int s)
{
pq.push(ii(0,s));
dist[s]=0;
while(!pq.empty())
{
ii fr=pq.top();
pq.pop();
int w=fr.first;
int u=fr.second;
if(w>dist[u])
continue;
for(int i=0; i<vec[u].size(); ++i)
{
ii v=vec[u][i];
if(dist[u]+v.second<dist[v.first])
{
dist[v.first]=dist[u]+v.second;
pq.push(ii(dist[v.first],v.first));
}
}
}
}
int cost(char a,char b)
{
int aa,bb;
if(a=='E')
aa=1;
else if(a=='S')
aa=2;
else if(a=='W')
aa=3;
else
aa=4;
if(b=='E')
bb=1;
else if(b=='S')
bb=2;
else if(b=='W')
bb=3;
else
bb=4;
if(aa<=bb)
return bb-aa;
return 4-aa+bb;
}
int main()
{
int n,m;
cin>>n>>m;
char arr[n][m];
for(int i=0; i<n; ++i)
for(int j=0; j<m; ++j)
cin>>arr[i][j];
vec.assign(n*m+1,vii());
dist.assign(n*m+1,INF);
for(int i=0; i<n; ++i)
{
for(int j=0; j<m; ++j)
{
if(arr[i][j]!='X')
{
if(i-1>=0&&((arr[i-1][j]=='X'&&i-1==n-1&&j==m-1)||arr[i][j]!='X'))
vec[i*m+j].push_back(ii((i-1)*m+j,cost(arr[i][j],'N')));
if(i+1<n&&((arr[i-1][j]=='X'&&i-1==n-1&&j==m-1)||arr[i][j]!='X'))
vec[i*m+j].push_back(ii((i+1)*m+j,cost(arr[i][j],'S')));
if(j-1>=0&&((arr[i-1][j]=='X'&&i-1==n-1&&j==m-1)||arr[i][j]!='X'))
vec[i*m+j].push_back(ii(i*m+j-1,cost(arr[i][j],'W')));
if(j+1<m&&((arr[i-1][j]=='X'&&i-1==n-1&&j==m-1)||arr[i][j]!='X'))
vec[i*m+j].push_back(ii(i*m+j+1,cost(arr[i][j],'E')));
}
}
}
dijkstra(0);
if(dist[n*m-1]==INF)
cout<<-1;
else
cout<<dist[n*m-1];
}
Compilation message
adventure.cpp: In function 'void dijkstra(int)':
adventure.cpp:22:15: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i=0; i<vec[u].size(); ++i)
~^~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
384 KB |
Output is correct |
2 |
Correct |
1 ms |
384 KB |
Output is correct |
3 |
Correct |
1 ms |
384 KB |
Output is correct |
4 |
Correct |
1 ms |
384 KB |
Output is correct |
5 |
Correct |
1 ms |
384 KB |
Output is correct |
6 |
Correct |
1 ms |
384 KB |
Output is correct |
7 |
Correct |
1 ms |
368 KB |
Output is correct |
8 |
Correct |
1 ms |
384 KB |
Output is correct |
9 |
Correct |
1 ms |
384 KB |
Output is correct |
10 |
Correct |
1 ms |
384 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
384 KB |
Output is correct |
2 |
Correct |
1 ms |
384 KB |
Output is correct |
3 |
Correct |
1 ms |
384 KB |
Output is correct |
4 |
Correct |
1 ms |
384 KB |
Output is correct |
5 |
Correct |
1 ms |
384 KB |
Output is correct |
6 |
Correct |
1 ms |
384 KB |
Output is correct |
7 |
Correct |
1 ms |
368 KB |
Output is correct |
8 |
Correct |
1 ms |
384 KB |
Output is correct |
9 |
Correct |
1 ms |
384 KB |
Output is correct |
10 |
Correct |
1 ms |
384 KB |
Output is correct |
11 |
Correct |
1 ms |
256 KB |
Output is correct |
12 |
Correct |
1 ms |
384 KB |
Output is correct |
13 |
Correct |
1 ms |
288 KB |
Output is correct |
14 |
Correct |
1 ms |
256 KB |
Output is correct |
15 |
Correct |
1 ms |
384 KB |
Output is correct |
16 |
Correct |
1 ms |
384 KB |
Output is correct |
17 |
Correct |
1 ms |
384 KB |
Output is correct |
18 |
Correct |
1 ms |
384 KB |
Output is correct |
19 |
Correct |
1 ms |
384 KB |
Output is correct |
20 |
Correct |
1 ms |
384 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
256 KB |
Output is correct |
2 |
Correct |
1 ms |
256 KB |
Output is correct |
3 |
Correct |
0 ms |
256 KB |
Output is correct |
4 |
Correct |
1 ms |
256 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
384 KB |
Output is correct |
2 |
Correct |
1 ms |
384 KB |
Output is correct |
3 |
Correct |
1 ms |
384 KB |
Output is correct |
4 |
Correct |
1 ms |
384 KB |
Output is correct |
5 |
Correct |
1 ms |
384 KB |
Output is correct |
6 |
Correct |
1 ms |
384 KB |
Output is correct |
7 |
Correct |
1 ms |
384 KB |
Output is correct |
8 |
Correct |
1 ms |
384 KB |
Output is correct |
9 |
Correct |
1 ms |
384 KB |
Output is correct |
10 |
Correct |
1 ms |
256 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
384 KB |
Output is correct |
2 |
Correct |
1 ms |
384 KB |
Output is correct |
3 |
Correct |
1 ms |
384 KB |
Output is correct |
4 |
Correct |
1 ms |
384 KB |
Output is correct |
5 |
Correct |
1 ms |
384 KB |
Output is correct |
6 |
Correct |
1 ms |
384 KB |
Output is correct |
7 |
Correct |
1 ms |
368 KB |
Output is correct |
8 |
Correct |
1 ms |
384 KB |
Output is correct |
9 |
Correct |
1 ms |
384 KB |
Output is correct |
10 |
Correct |
1 ms |
384 KB |
Output is correct |
11 |
Correct |
1 ms |
256 KB |
Output is correct |
12 |
Correct |
1 ms |
384 KB |
Output is correct |
13 |
Correct |
1 ms |
288 KB |
Output is correct |
14 |
Correct |
1 ms |
256 KB |
Output is correct |
15 |
Correct |
1 ms |
384 KB |
Output is correct |
16 |
Correct |
1 ms |
384 KB |
Output is correct |
17 |
Correct |
1 ms |
384 KB |
Output is correct |
18 |
Correct |
1 ms |
384 KB |
Output is correct |
19 |
Correct |
1 ms |
384 KB |
Output is correct |
20 |
Correct |
1 ms |
384 KB |
Output is correct |
21 |
Correct |
0 ms |
256 KB |
Output is correct |
22 |
Correct |
1 ms |
256 KB |
Output is correct |
23 |
Correct |
0 ms |
256 KB |
Output is correct |
24 |
Correct |
1 ms |
256 KB |
Output is correct |
25 |
Correct |
1 ms |
384 KB |
Output is correct |
26 |
Correct |
1 ms |
384 KB |
Output is correct |
27 |
Correct |
1 ms |
384 KB |
Output is correct |
28 |
Correct |
1 ms |
384 KB |
Output is correct |
29 |
Correct |
1 ms |
384 KB |
Output is correct |
30 |
Correct |
1 ms |
384 KB |
Output is correct |
31 |
Correct |
1 ms |
384 KB |
Output is correct |
32 |
Correct |
1 ms |
384 KB |
Output is correct |
33 |
Correct |
1 ms |
384 KB |
Output is correct |
34 |
Correct |
1 ms |
256 KB |
Output is correct |
35 |
Correct |
10 ms |
1920 KB |
Output is correct |
36 |
Correct |
1 ms |
512 KB |
Output is correct |
37 |
Correct |
12 ms |
2048 KB |
Output is correct |
38 |
Correct |
11 ms |
3072 KB |
Output is correct |
39 |
Correct |
119 ms |
17144 KB |
Output is correct |
40 |
Correct |
114 ms |
17016 KB |
Output is correct |
41 |
Correct |
65 ms |
17016 KB |
Output is correct |
42 |
Correct |
117 ms |
17144 KB |
Output is correct |
43 |
Correct |
135 ms |
21500 KB |
Output is correct |
44 |
Correct |
65 ms |
17016 KB |
Output is correct |