#include <bits/stdc++.h>
using namespace std;
const int oo = INT_MAX;
int dx[] = {0,0,1,0,-1};
int dy[] = {0,1,0,-1,0};
int n,m,c[505][505];
priority_queue<pair<int,pair<int,int>>,vector<pair<int,pair<int,int>>>, greater<pair<int,pair<int,int>>>> h;
bool sel[505][505];
int d[505][505];
void Dijkstra(int x, int y)
{
for(int i=1; i<=n; i++)
{
for(int j=1; j<=m; j++)
{
d[i][j] = oo;
}
}
d[1][1] = 0;
if(c[x][y])
{
h.push({0,{x,y}});
}
int cnt = 0;
while(!h.empty())
{
while(!h.empty() && sel[h.top().second.first][h.top().second.second])
{
h.pop();
}
if(h.empty())
{
break;
}
int k = h.top().first;
int x = h.top().second.first;
int y = h.top().second.second;
h.pop();
sel[x][y] = true;
for(int p=1; p<=4; p++)
{
int xx = x + dx[p];
int yy = y + dy[p];
if(xx==1 && yy==1)
{
continue;
}
if(xx<1 || yy<1 || xx>n || yy>m)
{
continue;
}
int cost = 0;
if(p>=c[x][y])
{
cost = p-c[x][y];
}
else
{
cost = 4-c[x][y]+p;
}
if(xx==n && yy==m)
{
++cnt;
}
if(d[x][y]+cost<=d[xx][yy])
{
d[xx][yy] = d[x][y]+cost;
if(c[xx][yy])
{
h.push({d[xx][yy],{xx,yy}});
}
}
if(cnt==2)
{
return;
}
}
}
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
cin>>n>>m;
for(int i=1; i<=n; i++)
{
for(int j=1; j<=m; j++)
{
char ch;
cin>>ch;
if(ch=='E')
{
c[i][j] = 1;
}
else if(ch=='S')
{
c[i][j] = 2;
}
else if(ch=='W')
{
c[i][j] = 3;
}
else if(ch=='N')
{
c[i][j] = 4;
}
}
}
Dijkstra(1,1);
if(d[n][m]==oo)
{
cout<<-1<<'\n';
return 0;
}
cout<<d[n][m]<<'\n';
return 0;
}
Compilation message
adventure.cpp: In function 'void Dijkstra(int, int)':
adventure.cpp:36:13: warning: unused variable 'k' [-Wunused-variable]
36 | int k = h.top().first;
| ^
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
0 ms |
332 KB |
Output is correct |
6 |
Correct |
1 ms |
332 KB |
Output is correct |
7 |
Correct |
1 ms |
332 KB |
Output is correct |
8 |
Correct |
0 ms |
332 KB |
Output is correct |
9 |
Correct |
1 ms |
332 KB |
Output is correct |
10 |
Correct |
1 ms |
336 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
0 ms |
332 KB |
Output is correct |
6 |
Correct |
1 ms |
332 KB |
Output is correct |
7 |
Correct |
1 ms |
332 KB |
Output is correct |
8 |
Correct |
0 ms |
332 KB |
Output is correct |
9 |
Correct |
1 ms |
332 KB |
Output is correct |
10 |
Correct |
1 ms |
336 KB |
Output is correct |
11 |
Correct |
1 ms |
332 KB |
Output is correct |
12 |
Correct |
1 ms |
332 KB |
Output is correct |
13 |
Correct |
0 ms |
332 KB |
Output is correct |
14 |
Correct |
1 ms |
332 KB |
Output is correct |
15 |
Correct |
1 ms |
332 KB |
Output is correct |
16 |
Correct |
1 ms |
332 KB |
Output is correct |
17 |
Correct |
1 ms |
332 KB |
Output is correct |
18 |
Correct |
0 ms |
332 KB |
Output is correct |
19 |
Correct |
1 ms |
332 KB |
Output is correct |
20 |
Correct |
1 ms |
332 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
1 ms |
332 KB |
Output is correct |
7 |
Correct |
1 ms |
332 KB |
Output is correct |
8 |
Correct |
1 ms |
332 KB |
Output is correct |
9 |
Correct |
1 ms |
332 KB |
Output is correct |
10 |
Correct |
1 ms |
332 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
0 ms |
332 KB |
Output is correct |
6 |
Correct |
1 ms |
332 KB |
Output is correct |
7 |
Correct |
1 ms |
332 KB |
Output is correct |
8 |
Correct |
0 ms |
332 KB |
Output is correct |
9 |
Correct |
1 ms |
332 KB |
Output is correct |
10 |
Correct |
1 ms |
336 KB |
Output is correct |
11 |
Correct |
1 ms |
332 KB |
Output is correct |
12 |
Correct |
1 ms |
332 KB |
Output is correct |
13 |
Correct |
0 ms |
332 KB |
Output is correct |
14 |
Correct |
1 ms |
332 KB |
Output is correct |
15 |
Correct |
1 ms |
332 KB |
Output is correct |
16 |
Correct |
1 ms |
332 KB |
Output is correct |
17 |
Correct |
1 ms |
332 KB |
Output is correct |
18 |
Correct |
0 ms |
332 KB |
Output is correct |
19 |
Correct |
1 ms |
332 KB |
Output is correct |
20 |
Correct |
1 ms |
332 KB |
Output is correct |
21 |
Correct |
1 ms |
332 KB |
Output is correct |
22 |
Correct |
1 ms |
332 KB |
Output is correct |
23 |
Correct |
1 ms |
332 KB |
Output is correct |
24 |
Correct |
1 ms |
332 KB |
Output is correct |
25 |
Correct |
1 ms |
332 KB |
Output is correct |
26 |
Correct |
1 ms |
332 KB |
Output is correct |
27 |
Correct |
1 ms |
332 KB |
Output is correct |
28 |
Correct |
1 ms |
332 KB |
Output is correct |
29 |
Correct |
1 ms |
332 KB |
Output is correct |
30 |
Correct |
1 ms |
332 KB |
Output is correct |
31 |
Correct |
1 ms |
332 KB |
Output is correct |
32 |
Correct |
1 ms |
332 KB |
Output is correct |
33 |
Correct |
1 ms |
332 KB |
Output is correct |
34 |
Correct |
1 ms |
332 KB |
Output is correct |
35 |
Correct |
6 ms |
588 KB |
Output is correct |
36 |
Correct |
1 ms |
332 KB |
Output is correct |
37 |
Correct |
7 ms |
844 KB |
Output is correct |
38 |
Correct |
2 ms |
1356 KB |
Output is correct |
39 |
Correct |
65 ms |
2756 KB |
Output is correct |
40 |
Correct |
68 ms |
2800 KB |
Output is correct |
41 |
Correct |
9 ms |
2508 KB |
Output is correct |
42 |
Correct |
67 ms |
2724 KB |
Output is correct |
43 |
Correct |
63 ms |
5920 KB |
Output is correct |
44 |
Correct |
9 ms |
2508 KB |
Output is correct |