#include <bits/stdc++.h>
using namespace std;
const long long MAX=250010;
vector<pair<long long,long long> > graph[MAX];
long long n,m;
char mat[501][501];
long long dist[MAX];
bool vis[MAX];
long long get(long long i, long long j)
{
return i*m+j;
}
struct node
{
long long idx,cost;
node() {};
node(long long i, long long c)
{
idx=i;
cost=c;
}
bool operator< (const node &tmp)const
{
return cost>tmp.cost;
}
};
long long dijkstra(long long si, long long sj)
{
priority_queue<node> q;
for(long long i=0; i<n*m; i++)
{
vis[i]=false;
dist[i]=INT_MAX;
}
q.push(node{si,0});
dist[si]=0;
while(!q.empty())
{
auto curr=q.top();
q.pop();
if(vis[curr.idx])continue;
vis[curr.idx]=true;
if(curr.idx==sj)return dist[curr.idx];
for(long long i=0; i<graph[curr.idx].size(); i++)
{
long long neighbour=graph[curr.idx][i].first;
long long val=graph[curr.idx][i].second;
if(dist[neighbour]>curr.cost+val)
{
dist[neighbour]=curr.cost+val;
q.push(node{neighbour,dist[neighbour]});
}
}
}
return -1;
}
int main()
{
cin>>n>>m;
for(long long i=0; i<n; i++)
{
for(long long j=0; j<m; j++)
{
cin>>mat[i][j];
}
}
for(long long i=0; i<n; i++)
{
for(long long j=0; j<m; j++)
{
if(mat[i][j]=='X')continue;
if(i+1<n)
{
long long a=get(i,j);
long long b=get(i+1,j);
if(mat[i][j]=='S')
{
graph[a].push_back({b,0});
}
else
{
if(mat[i][j]=='E')
{
graph[a].push_back({b,1});
}
else if(mat[i][j]=='N')
{
graph[a].push_back({b,2});
}
else if(mat[i][j]=='W')
{
graph[a].push_back({b,3});
}
}
}
if(j+1<m)
{
long long a=get(i,j);
long long b=get(i,j+1);
if(mat[i][j]=='E')
{
graph[a].push_back({b,0});
}
else
{
if(mat[i][j]=='N')
{
graph[a].push_back({b,1});
}
else if(mat[i][j]=='W')
{
graph[a].push_back({b,2});
}
else if(mat[i][j]=='S')
{
graph[a].push_back({b,3});
}
}
}
if(j-1>=0)
{
long long a=get(i,j);
long long b=get(i,j-1);
if(mat[i][j]=='W')
{
graph[a].push_back({b,0});
}
else
{
if(mat[i][j]=='S')
{
graph[a].push_back({b,1});
}
else if(mat[i][j]=='E')
{
graph[a].push_back({b,2});
}
else if(mat[i][j]=='N')
{
graph[a].push_back({b,3});
}
}
}
if(i-1>=0)
{
long long a=get(i,j);
long long b=get(i-1,j);
if(mat[i][j]=='N')
{
graph[a].push_back({b,0});
}
else
{
if(mat[i][j]=='W')
{
graph[a].push_back({b,1});
}
else if(mat[i][j]=='S')
{
graph[a].push_back({b,2});
}
else if(mat[i][j]=='E')
{
graph[a].push_back({b,3});
}
}
}
}
}
long long res=dijkstra(0,get(n-1,m-1));
if(res==INT_MAX)res=-1;
cout<<res<<endl;
return 0;
}
Compilation message
adventure.cpp: In function 'long long int dijkstra(long long int, long long int)':
adventure.cpp:45:29: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
45 | for(long long i=0; i<graph[curr.idx].size(); i++)
| ~^~~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
8536 KB |
Output is correct |
2 |
Correct |
2 ms |
8540 KB |
Output is correct |
3 |
Correct |
2 ms |
8540 KB |
Output is correct |
4 |
Correct |
2 ms |
8548 KB |
Output is correct |
5 |
Correct |
2 ms |
8536 KB |
Output is correct |
6 |
Correct |
2 ms |
8540 KB |
Output is correct |
7 |
Correct |
2 ms |
8540 KB |
Output is correct |
8 |
Correct |
2 ms |
8796 KB |
Output is correct |
9 |
Correct |
2 ms |
8540 KB |
Output is correct |
10 |
Correct |
2 ms |
8572 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
8536 KB |
Output is correct |
2 |
Correct |
2 ms |
8540 KB |
Output is correct |
3 |
Correct |
2 ms |
8540 KB |
Output is correct |
4 |
Correct |
2 ms |
8548 KB |
Output is correct |
5 |
Correct |
2 ms |
8536 KB |
Output is correct |
6 |
Correct |
2 ms |
8540 KB |
Output is correct |
7 |
Correct |
2 ms |
8540 KB |
Output is correct |
8 |
Correct |
2 ms |
8796 KB |
Output is correct |
9 |
Correct |
2 ms |
8540 KB |
Output is correct |
10 |
Correct |
2 ms |
8572 KB |
Output is correct |
11 |
Correct |
2 ms |
8576 KB |
Output is correct |
12 |
Correct |
2 ms |
8540 KB |
Output is correct |
13 |
Correct |
2 ms |
8540 KB |
Output is correct |
14 |
Correct |
2 ms |
8536 KB |
Output is correct |
15 |
Correct |
2 ms |
8540 KB |
Output is correct |
16 |
Correct |
2 ms |
8540 KB |
Output is correct |
17 |
Correct |
2 ms |
8540 KB |
Output is correct |
18 |
Correct |
2 ms |
8580 KB |
Output is correct |
19 |
Correct |
2 ms |
8536 KB |
Output is correct |
20 |
Correct |
2 ms |
8540 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
8540 KB |
Output is correct |
2 |
Correct |
2 ms |
8540 KB |
Output is correct |
3 |
Correct |
2 ms |
8540 KB |
Output is correct |
4 |
Correct |
1 ms |
8540 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
8540 KB |
Output is correct |
2 |
Correct |
2 ms |
8540 KB |
Output is correct |
3 |
Correct |
2 ms |
8600 KB |
Output is correct |
4 |
Correct |
2 ms |
8540 KB |
Output is correct |
5 |
Correct |
2 ms |
8540 KB |
Output is correct |
6 |
Correct |
2 ms |
8540 KB |
Output is correct |
7 |
Correct |
2 ms |
8540 KB |
Output is correct |
8 |
Correct |
2 ms |
8540 KB |
Output is correct |
9 |
Correct |
2 ms |
8540 KB |
Output is correct |
10 |
Correct |
2 ms |
8540 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
8536 KB |
Output is correct |
2 |
Correct |
2 ms |
8540 KB |
Output is correct |
3 |
Correct |
2 ms |
8540 KB |
Output is correct |
4 |
Correct |
2 ms |
8548 KB |
Output is correct |
5 |
Correct |
2 ms |
8536 KB |
Output is correct |
6 |
Correct |
2 ms |
8540 KB |
Output is correct |
7 |
Correct |
2 ms |
8540 KB |
Output is correct |
8 |
Correct |
2 ms |
8796 KB |
Output is correct |
9 |
Correct |
2 ms |
8540 KB |
Output is correct |
10 |
Correct |
2 ms |
8572 KB |
Output is correct |
11 |
Correct |
2 ms |
8576 KB |
Output is correct |
12 |
Correct |
2 ms |
8540 KB |
Output is correct |
13 |
Correct |
2 ms |
8540 KB |
Output is correct |
14 |
Correct |
2 ms |
8536 KB |
Output is correct |
15 |
Correct |
2 ms |
8540 KB |
Output is correct |
16 |
Correct |
2 ms |
8540 KB |
Output is correct |
17 |
Correct |
2 ms |
8540 KB |
Output is correct |
18 |
Correct |
2 ms |
8580 KB |
Output is correct |
19 |
Correct |
2 ms |
8536 KB |
Output is correct |
20 |
Correct |
2 ms |
8540 KB |
Output is correct |
21 |
Correct |
2 ms |
8540 KB |
Output is correct |
22 |
Correct |
2 ms |
8540 KB |
Output is correct |
23 |
Correct |
2 ms |
8540 KB |
Output is correct |
24 |
Correct |
1 ms |
8540 KB |
Output is correct |
25 |
Correct |
2 ms |
8540 KB |
Output is correct |
26 |
Correct |
2 ms |
8540 KB |
Output is correct |
27 |
Correct |
2 ms |
8600 KB |
Output is correct |
28 |
Correct |
2 ms |
8540 KB |
Output is correct |
29 |
Correct |
2 ms |
8540 KB |
Output is correct |
30 |
Correct |
2 ms |
8540 KB |
Output is correct |
31 |
Correct |
2 ms |
8540 KB |
Output is correct |
32 |
Correct |
2 ms |
8540 KB |
Output is correct |
33 |
Correct |
2 ms |
8540 KB |
Output is correct |
34 |
Correct |
2 ms |
8540 KB |
Output is correct |
35 |
Correct |
7 ms |
9820 KB |
Output is correct |
36 |
Correct |
2 ms |
8724 KB |
Output is correct |
37 |
Correct |
8 ms |
10076 KB |
Output is correct |
38 |
Correct |
7 ms |
11100 KB |
Output is correct |
39 |
Correct |
66 ms |
24508 KB |
Output is correct |
40 |
Correct |
74 ms |
24656 KB |
Output is correct |
41 |
Correct |
35 ms |
24400 KB |
Output is correct |
42 |
Correct |
62 ms |
24536 KB |
Output is correct |
43 |
Correct |
60 ms |
33988 KB |
Output is correct |
44 |
Correct |
32 ms |
24668 KB |
Output is correct |