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>
using namespace std;
#define endl '\n'
#define ll long long
#define f first
#define s second
#define pll pair<ll, ll>
priority_queue<pair<ll, pll>, vector<pair<ll, pll>>, greater<pair<ll, pll>>>q;
const ll sz=505, mod=1e9+7;
map<char, ll>mp;
ll n, m, used[sz][sz], dis[sz][sz];
char k[sz][sz];
vector<pair<pair<ll, ll>, ll>>g[sz][sz];
ll cnt(char a, char b)
{
ll dif=mp[a]-mp[b];
if(dif<0) dif+=4;
return dif;
}
int main()
{
ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
mp['E']=0;
mp['N']=1;
mp['W']=2;
mp['S']=3;
cin>>n>>m;
for(int i=1; i<=n; i++){
for(int j=1; j<=m; j++){
cin>>k[i][j];
if(k[i][j]=='X') continue;
if(i>1){
g[i][j].push_back({{i-1, j}, cnt(k[i][j], 'N')});
}
if(i<n){
g[i][j].push_back({{i+1, j}, cnt(k[i][j], 'S')});
}
if(j>1){
g[i][j].push_back({{i, j-1}, cnt(k[i][j], 'W')});
}
if(j<m){
g[i][j].push_back({{i, j+1}, cnt(k[i][j], 'E')});
}
}
}
memset(dis, 32, sizeof(dis));
q.push({0, {1, 1}});
dis[1][1]=0;
while(q.size()){
ll x=q.top().s.first;
ll y=q.top().s.second;
q.pop();
if(used[x][y]) continue;
used[x][y]=1;
for(auto j:g[x][y]){
ll u=j.f.f, v=j.f.s, w=j.s;
if(dis[u][v]>dis[x][y]+w){
dis[u][v]=dis[x][y]+w;
q.push({dis[u][v], {u, v}});
}
}
}
if(!used[n][m]) cout<<-1;
else cout<<dis[n][m];
}
# | 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... |