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 ll long long
#define endl "\n"
#define INF 1000000000
#define LINF 1000000000000000LL
#define pb push_back
#define all(x) x.begin(), x.end()
#define len(s) (int)s.size()
#define test_case { int t; cin>>t; while(t--)solve(); }
#define input(n, v) {for(int i = 0;i<n;i++) cin>>v[i];}
#define output(n, v) {for(int i = 0;i<n;i++) cout<<v[i]<<" "; cout<<endl;}
#define single_case solve();
#define line cout<<"------------"<<endl;
#define ios { ios_base::sync_with_stdio(false); cin.tie(NULL); }
using namespace std;
const int N = 505;
string mat[N];
int n, m;
int dp[N][N];
int vidjen[N][N];
int main()
{
ios
cin>>n>>m;
for(int i = 0;i<n;i++)
{
cin>>mat[i];
}
for(int i = 0;i<N;i++)
for(int j = 0;j<N;j++)
dp[i][j] = INF;
priority_queue<pair<int, pair<int, int> >, vector<pair<int, pair<int, int> > >, greater<pair<int, pair<int, int> > > > pq;
dp[0][0] = 0;
pq.push({0, {0, 0}});
while(len(pq))
{
int x = pq.top().second.first;
int y = pq.top().second.second;
pq.pop();
if(vidjen[x][y]) continue;
vidjen[x][y] = 1;
if(mat[x][y]=='X') continue;
if(mat[x][y]=='E')
{
if(y+1<m&&dp[x][y+1]>dp[x][y])
{
dp[x][y+1] = dp[x][y];
pq.push({dp[x][y], {x, y+1}});
}
if(x+1<n&&dp[x+1][y]>dp[x][y]+1)
{
dp[x+1][y] = dp[x][y] + 1;
pq.push({dp[x][y]+1, {x+1, y}});
}
if(x-1>=0&&dp[x-1][y]>dp[x][y]+3)
{
dp[x-1][y] = dp[x][y] + 3;
pq.push({dp[x-1][y], {x-1, y}});
}
if(y-1>=0&&dp[x][y-1]>dp[x][y]+2)
{
dp[x][y-1] = dp[x][y] + 2;
pq.push({dp[x][y-1], {x, y-1}});
}
}
if(mat[x][y]=='W')
{
if(y+1<m&&dp[x][y+1]>dp[x][y]+2)
{
dp[x][y+1] = dp[x][y] + 2;
pq.push({dp[x][y] + 2, {x, y+1}});
}
if(x+1<n&&dp[x+1][y]>dp[x][y]+3)
{
dp[x+1][y] = dp[x][y] + 3;
pq.push({dp[x][y] + 3, {x+1, y}});
}
if(x-1>=0&&dp[x-1][y]>dp[x][y]+1)
{
dp[x-1][y] = dp[x][y] + 1;
pq.push({dp[x-1][y], {x-1, y}});
}
if(y-1>=0&&dp[x][y-1]>dp[x][y])
{
dp[x][y-1] = dp[x][y];
pq.push({dp[x][y-1], {x, y-1}});
}
}
if(mat[x][y]=='S')
{
if(y+1<m&&dp[x][y+1]>dp[x][y]+3)
{
dp[x][y+1] = dp[x][y] + 3;
pq.push({dp[x][y] + 3, {x, y+1}});
}
if(x+1<n&&dp[x+1][y]>dp[x][y])
{
dp[x+1][y] = dp[x][y];
pq.push({dp[x][y], {x+1, y}});
}
if(x-1>=0&&dp[x-1][y]>dp[x][y]+2)
{
dp[x-1][y] = dp[x][y] + 2;
pq.push({dp[x-1][y], {x-1, y}});
}
if(y-1>=0&&dp[x][y-1]>dp[x][y]+1)
{
dp[x][y-1] = dp[x][y] + 1;
pq.push({dp[x][y-1], {x, y-1}});
}
}
if(mat[x][y]=='N')
{
if(y+1<m&&dp[x][y+1]>dp[x][y]+1)
{
dp[x][y+1] = dp[x][y] + 1;
pq.push({dp[x][y] + 1, {x, y+1}});
}
if(x+1<n&&dp[x+1][y]>dp[x][y]+2)
{
dp[x+1][y] = dp[x][y] + 2;
pq.push({dp[x][y]+2, {x+1, y}});
}
if(x-1>=0&&dp[x-1][y]>dp[x][y])
{
dp[x-1][y] = dp[x][y];
pq.push({dp[x-1][y], {x-1, y}});
}
if(y-1>=0&&dp[x][y-1]>dp[x][y]+3)
{
dp[x][y-1] = dp[x][y] + 3;
pq.push({dp[x][y-1], {x, y-1}});
}
}
}
/* for(int i = 0;i<n;i++)
{
for(int j = 0;j<m;j++)
cout<<dp[i][j]<<" ";
cout<<endl;
}*/
if(dp[n-1][m-1]==INF) cout<<-1;
else cout<<dp[n-1][m-1];
return 0;
}
# | 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... |