This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#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 (stderr)
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])
| ^
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |