#include <bits/stdc++.h>
using namespace std;
long long int INF=1e15;
long long int dist[300000];
vector<vector<pair<int,int>>> adjl;
void dijsktra(int s,long long int *dist,vector<vector<pair<int,int>>>adjl){
using pii=pair<int,int>;
priority_queue<pii,vector<pii>,greater<pii>>pq;
dist[s]=0;
pq.push({0,s});
while(!pq.empty()){
pii trenutni=pq.top();
pq.pop();
int d=trenutni.first;
int u=trenutni.second;
if(d>dist[u]) continue;
for(int i=0;i<adjl[u].size();i++){
pii v=adjl[u][i];
if(dist[u]+v.first <dist[v.second]){
dist[v.second]=dist[u]+v.first;
pq.push({dist[v.second],v.second});
}
}
}
}
int trans(char a1,char a2){
int x1;
int x2;
if(a1=='S'){
x1=1;
}
if(a1=='W'){
x1=2;
}
if(a1=='N'){
x1=3;
}
if(a1=='E'){
x1=4;
} if(a2=='S'){
x2=1;
}
if(a2=='W'){
x2=2;
}
if(a2=='N'){
x2=3;
}
if(a2=='E'){
x2=4;
}
if(x2>=x1){
return (x2-x1);
}else{
return (x2+4-x1);
}
}
int main(){
int n,m;
cin>>n>>m;
adjl.resize(n*m);
char matrica[n][m];
int a[n][m];
int brojac=0;
for(int i=0;i<n;i++){
string s;
cin>>s;
for(int j=0;j<m;j++){
matrica[i][j]=s[j];
a[i][j]=brojac;
brojac++;
}
}
brojac=0;
for(int i=0;i<n;i++){
for(int j=0;j<m;j++){
dist[brojac]=INF;
brojac++;
if(matrica[i][j]!='X'){
if(i>0){
adjl[a[i][j]].push_back(make_pair(trans(matrica[i][j],'N'),a[i-1][j]));
}
if(j>0){
adjl[a[i][j]].push_back(make_pair(trans(matrica[i][j],'W'),a[i][j-1]));
}
if(i<(n-1)){
adjl[a[i][j]].push_back(make_pair(trans(matrica[i][j],'S'),a[i+1][j]));
}
if(j<(m-1)){
adjl[a[i][j]].push_back(make_pair(trans(matrica[i][j],'E'),a[i][j+1]));
}
}
}
}
dijsktra(0,dist,adjl);
if(dist[n*m-1]==INF){
cout<<-1;
}else{
cout<<dist[n*m-1];
}
}
Compilation message
adventure.cpp: In function 'void dijsktra(int, long long int*, std::vector<std::vector<std::pair<int, int> > >)':
adventure.cpp:17:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
17 | for(int i=0;i<adjl[u].size();i++){
| ~^~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
384 KB |
Output is correct |
2 |
Correct |
0 ms |
384 KB |
Output is correct |
3 |
Correct |
0 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 |
0 ms |
384 KB |
Output is correct |
7 |
Correct |
1 ms |
384 KB |
Output is correct |
8 |
Correct |
1 ms |
416 KB |
Output is correct |
9 |
Correct |
1 ms |
384 KB |
Output is correct |
10 |
Correct |
0 ms |
384 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
384 KB |
Output is correct |
2 |
Correct |
0 ms |
384 KB |
Output is correct |
3 |
Correct |
0 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 |
0 ms |
384 KB |
Output is correct |
7 |
Correct |
1 ms |
384 KB |
Output is correct |
8 |
Correct |
1 ms |
416 KB |
Output is correct |
9 |
Correct |
1 ms |
384 KB |
Output is correct |
10 |
Correct |
0 ms |
384 KB |
Output is correct |
11 |
Correct |
1 ms |
384 KB |
Output is correct |
12 |
Correct |
1 ms |
384 KB |
Output is correct |
13 |
Correct |
1 ms |
384 KB |
Output is correct |
14 |
Correct |
1 ms |
384 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 |
1 ms |
384 KB |
Output is correct |
2 |
Correct |
0 ms |
384 KB |
Output is correct |
3 |
Correct |
0 ms |
384 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 |
384 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
384 KB |
Output is correct |
2 |
Correct |
0 ms |
384 KB |
Output is correct |
3 |
Correct |
0 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 |
0 ms |
384 KB |
Output is correct |
7 |
Correct |
1 ms |
384 KB |
Output is correct |
8 |
Correct |
1 ms |
416 KB |
Output is correct |
9 |
Correct |
1 ms |
384 KB |
Output is correct |
10 |
Correct |
0 ms |
384 KB |
Output is correct |
11 |
Correct |
1 ms |
384 KB |
Output is correct |
12 |
Correct |
1 ms |
384 KB |
Output is correct |
13 |
Correct |
1 ms |
384 KB |
Output is correct |
14 |
Correct |
1 ms |
384 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 |
1 ms |
384 KB |
Output is correct |
22 |
Correct |
0 ms |
384 KB |
Output is correct |
23 |
Correct |
0 ms |
384 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 |
384 KB |
Output is correct |
35 |
Correct |
11 ms |
3456 KB |
Output is correct |
36 |
Correct |
1 ms |
640 KB |
Output is correct |
37 |
Correct |
13 ms |
3968 KB |
Output is correct |
38 |
Correct |
14 ms |
5888 KB |
Output is correct |
39 |
Correct |
130 ms |
34296 KB |
Output is correct |
40 |
Correct |
130 ms |
34424 KB |
Output is correct |
41 |
Correct |
79 ms |
34168 KB |
Output is correct |
42 |
Correct |
130 ms |
34296 KB |
Output is correct |
43 |
Correct |
149 ms |
40944 KB |
Output is correct |
44 |
Correct |
89 ms |
34296 KB |
Output is correct |