#include <bits/stdc++.h>
#define NMAX 250001
#define int long long
#define MAX 1001
#define pb push_back
#define eb emplace_back
#define MOD 1000000007
#define nl '\n'
#define INF 1000000007
#define LLONG_MAX 9223372036854775807
#define pii pair<int,int>
#define tpl tuple<int,int,int,int>
#pragma GCC optimize("O3")
using namespace std;
ifstream fin("aib.in");
ofstream fout("aib.out");
int n,m;
int mat[501][501];
bool vis[501][501];
int dp[501][501];
bool inmat(int i,int j)
{
return i>=1 && i<=n && j>=1 && j<=m;
}
vector<vector<pii>>G(NMAX);
int ind;
vector<int>dist(NMAX);
int calc_pos(int i,int j)
{
return (i-1)*m+j;
}
void dijkstra(int node)
{
priority_queue<pii,vector<pii>,greater<pii>>pq;
pq.push({0,node});
dist[node]=0;
while(!pq.empty())
{
auto [rot,first]=pq.top();
pq.pop();
for(auto [x,w]:G[first])
{
if(dist[x]>dist[first]+w)
{
dist[x]=dist[first]+w;
pq.push({dist[x],x});
}
}
}
}
signed main()
{
cin>>n>>m;
for(int i=1;i<=n;++i)
{
for(int j=1;j<=m;++j)
{
char x;
cin>>x;
if(x=='N')
mat[i][j]=1;
else if(x=='E')
mat[i][j]=2;
else if(x=='S')
mat[i][j]=3;
else if(x=='W')
mat[i][j]=4;
else
mat[i][j]=5;
}
}
for(int i=1;i<=n;++i)
{
for(int j=1;j<=m;++j)
{
dist[calc_pos(i,j)]=INF;
if(mat[i][j]==1)
{
if(inmat(i-1,j))
G[calc_pos(i,j)].pb({calc_pos(i-1,j),0});
if(inmat(i,j+1))
G[calc_pos(i,j)].pb({calc_pos(i,j+1),1});
if(inmat(i+1,j))
G[calc_pos(i,j)].pb({calc_pos(i+1,j),2});
if(inmat(i,j-1))
G[calc_pos(i,j)].pb({calc_pos(i,j-1),3});
}
else if(mat[i][j]==2)
{
if(inmat(i,j+1))
G[calc_pos(i,j)].pb({calc_pos(i,j+1),0});
if(inmat(i+1,j))
G[calc_pos(i,j)].pb({calc_pos(i+1,j),1});
if(inmat(i,j-1))
G[calc_pos(i,j)].pb({calc_pos(i,j-1),2});
if(inmat(i-1,j))
G[calc_pos(i,j)].pb({calc_pos(i-1,j),3});
}
else if(mat[i][j]==3)
{
if(inmat(i+1,j))
G[calc_pos(i,j)].pb({calc_pos(i+1,j),0});
if(inmat(i,j-1))
G[calc_pos(i,j)].pb({calc_pos(i,j-1),1});
if(inmat(i-1,j))
G[calc_pos(i,j)].pb({calc_pos(i-1,j),2});
if(inmat(i,j+1))
G[calc_pos(i,j)].pb({calc_pos(i,j+1),3});
}
else if(mat[i][j]==4)
{
if(inmat(i,j-1))
G[calc_pos(i,j)].pb({calc_pos(i,j-1),0});
if(inmat(i-1,j))
G[calc_pos(i,j)].pb({calc_pos(i-1,j),1});
if(inmat(i,j+1))
G[calc_pos(i,j)].pb({calc_pos(i,j+1),2});
if(inmat(i+1,j))
G[calc_pos(i,j)].pb({calc_pos(i+1,j),3});
}
}
}
if(mat[1][1]==5)
{
cout<<-1;
return 0;
}
dijkstra(1);
if(dist[calc_pos(n,m)]==INF)
cout<<-1;
else
cout<<dist[calc_pos(n,m)];
return 0;
}
Compilation message
adventure.cpp:10: warning: "LLONG_MAX" redefined
10 | #define LLONG_MAX 9223372036854775807
|
In file included from /usr/lib/gcc/x86_64-linux-gnu/10/include/limits.h:195,
from /usr/lib/gcc/x86_64-linux-gnu/10/include/syslimits.h:7,
from /usr/lib/gcc/x86_64-linux-gnu/10/include/limits.h:34,
from /usr/include/c++/10/climits:42,
from /usr/include/x86_64-linux-gnu/c++/10/bits/stdc++.h:39,
from adventure.cpp:1:
/usr/include/limits.h:135: note: this is the location of the previous definition
135 | # define LLONG_MAX __LONG_LONG_MAX__
|
adventure.cpp: In function 'void dijkstra(long long int)':
adventure.cpp:39:14: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
39 | auto [rot,first]=pq.top();
| ^
adventure.cpp:41:18: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
41 | for(auto [x,w]:G[first])
| ^
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
10328 KB |
Output is correct |
2 |
Correct |
2 ms |
10332 KB |
Output is correct |
3 |
Correct |
2 ms |
10332 KB |
Output is correct |
4 |
Correct |
2 ms |
10332 KB |
Output is correct |
5 |
Correct |
2 ms |
10332 KB |
Output is correct |
6 |
Correct |
2 ms |
10332 KB |
Output is correct |
7 |
Correct |
4 ms |
10332 KB |
Output is correct |
8 |
Correct |
2 ms |
10332 KB |
Output is correct |
9 |
Correct |
3 ms |
10584 KB |
Output is correct |
10 |
Correct |
3 ms |
10332 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
10328 KB |
Output is correct |
2 |
Correct |
2 ms |
10332 KB |
Output is correct |
3 |
Correct |
2 ms |
10332 KB |
Output is correct |
4 |
Correct |
2 ms |
10332 KB |
Output is correct |
5 |
Correct |
2 ms |
10332 KB |
Output is correct |
6 |
Correct |
2 ms |
10332 KB |
Output is correct |
7 |
Correct |
4 ms |
10332 KB |
Output is correct |
8 |
Correct |
2 ms |
10332 KB |
Output is correct |
9 |
Correct |
3 ms |
10584 KB |
Output is correct |
10 |
Correct |
3 ms |
10332 KB |
Output is correct |
11 |
Correct |
3 ms |
10328 KB |
Output is correct |
12 |
Correct |
2 ms |
10332 KB |
Output is correct |
13 |
Correct |
2 ms |
10332 KB |
Output is correct |
14 |
Correct |
3 ms |
10328 KB |
Output is correct |
15 |
Correct |
3 ms |
10332 KB |
Output is correct |
16 |
Correct |
2 ms |
10332 KB |
Output is correct |
17 |
Correct |
2 ms |
10332 KB |
Output is correct |
18 |
Correct |
4 ms |
10332 KB |
Output is correct |
19 |
Correct |
2 ms |
10332 KB |
Output is correct |
20 |
Correct |
3 ms |
10332 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
10332 KB |
Output is correct |
2 |
Correct |
2 ms |
10332 KB |
Output is correct |
3 |
Correct |
2 ms |
10328 KB |
Output is correct |
4 |
Correct |
2 ms |
10332 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
10332 KB |
Output is correct |
2 |
Correct |
2 ms |
10332 KB |
Output is correct |
3 |
Correct |
2 ms |
10332 KB |
Output is correct |
4 |
Correct |
2 ms |
10332 KB |
Output is correct |
5 |
Correct |
2 ms |
10332 KB |
Output is correct |
6 |
Correct |
2 ms |
10584 KB |
Output is correct |
7 |
Correct |
2 ms |
10328 KB |
Output is correct |
8 |
Correct |
3 ms |
10332 KB |
Output is correct |
9 |
Correct |
2 ms |
10332 KB |
Output is correct |
10 |
Correct |
2 ms |
10328 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
10328 KB |
Output is correct |
2 |
Correct |
2 ms |
10332 KB |
Output is correct |
3 |
Correct |
2 ms |
10332 KB |
Output is correct |
4 |
Correct |
2 ms |
10332 KB |
Output is correct |
5 |
Correct |
2 ms |
10332 KB |
Output is correct |
6 |
Correct |
2 ms |
10332 KB |
Output is correct |
7 |
Correct |
4 ms |
10332 KB |
Output is correct |
8 |
Correct |
2 ms |
10332 KB |
Output is correct |
9 |
Correct |
3 ms |
10584 KB |
Output is correct |
10 |
Correct |
3 ms |
10332 KB |
Output is correct |
11 |
Correct |
3 ms |
10328 KB |
Output is correct |
12 |
Correct |
2 ms |
10332 KB |
Output is correct |
13 |
Correct |
2 ms |
10332 KB |
Output is correct |
14 |
Correct |
3 ms |
10328 KB |
Output is correct |
15 |
Correct |
3 ms |
10332 KB |
Output is correct |
16 |
Correct |
2 ms |
10332 KB |
Output is correct |
17 |
Correct |
2 ms |
10332 KB |
Output is correct |
18 |
Correct |
4 ms |
10332 KB |
Output is correct |
19 |
Correct |
2 ms |
10332 KB |
Output is correct |
20 |
Correct |
3 ms |
10332 KB |
Output is correct |
21 |
Correct |
3 ms |
10332 KB |
Output is correct |
22 |
Correct |
2 ms |
10332 KB |
Output is correct |
23 |
Correct |
2 ms |
10328 KB |
Output is correct |
24 |
Correct |
2 ms |
10332 KB |
Output is correct |
25 |
Correct |
3 ms |
10332 KB |
Output is correct |
26 |
Correct |
2 ms |
10332 KB |
Output is correct |
27 |
Correct |
2 ms |
10332 KB |
Output is correct |
28 |
Correct |
2 ms |
10332 KB |
Output is correct |
29 |
Correct |
2 ms |
10332 KB |
Output is correct |
30 |
Correct |
2 ms |
10584 KB |
Output is correct |
31 |
Correct |
2 ms |
10328 KB |
Output is correct |
32 |
Correct |
3 ms |
10332 KB |
Output is correct |
33 |
Correct |
2 ms |
10332 KB |
Output is correct |
34 |
Correct |
2 ms |
10328 KB |
Output is correct |
35 |
Correct |
7 ms |
11864 KB |
Output is correct |
36 |
Correct |
3 ms |
10332 KB |
Output is correct |
37 |
Correct |
8 ms |
12428 KB |
Output is correct |
38 |
Correct |
7 ms |
13916 KB |
Output is correct |
39 |
Correct |
67 ms |
27804 KB |
Output is correct |
40 |
Correct |
66 ms |
28192 KB |
Output is correct |
41 |
Correct |
35 ms |
27992 KB |
Output is correct |
42 |
Correct |
69 ms |
27988 KB |
Output is correct |
43 |
Correct |
76 ms |
37572 KB |
Output is correct |
44 |
Correct |
35 ms |
27988 KB |
Output is correct |