#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;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
1516 KB |
Output is correct |
2 |
Correct |
1 ms |
1388 KB |
Output is correct |
3 |
Correct |
1 ms |
1388 KB |
Output is correct |
4 |
Correct |
1 ms |
1388 KB |
Output is correct |
5 |
Correct |
1 ms |
1388 KB |
Output is correct |
6 |
Correct |
1 ms |
1388 KB |
Output is correct |
7 |
Correct |
1 ms |
1388 KB |
Output is correct |
8 |
Correct |
1 ms |
1388 KB |
Output is correct |
9 |
Correct |
1 ms |
1388 KB |
Output is correct |
10 |
Correct |
1 ms |
1388 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
1516 KB |
Output is correct |
2 |
Correct |
1 ms |
1388 KB |
Output is correct |
3 |
Correct |
1 ms |
1388 KB |
Output is correct |
4 |
Correct |
1 ms |
1388 KB |
Output is correct |
5 |
Correct |
1 ms |
1388 KB |
Output is correct |
6 |
Correct |
1 ms |
1388 KB |
Output is correct |
7 |
Correct |
1 ms |
1388 KB |
Output is correct |
8 |
Correct |
1 ms |
1388 KB |
Output is correct |
9 |
Correct |
1 ms |
1388 KB |
Output is correct |
10 |
Correct |
1 ms |
1388 KB |
Output is correct |
11 |
Correct |
1 ms |
1388 KB |
Output is correct |
12 |
Correct |
1 ms |
1388 KB |
Output is correct |
13 |
Correct |
1 ms |
1388 KB |
Output is correct |
14 |
Correct |
1 ms |
1388 KB |
Output is correct |
15 |
Correct |
1 ms |
1388 KB |
Output is correct |
16 |
Correct |
1 ms |
1388 KB |
Output is correct |
17 |
Correct |
1 ms |
1388 KB |
Output is correct |
18 |
Correct |
1 ms |
1388 KB |
Output is correct |
19 |
Correct |
1 ms |
1388 KB |
Output is correct |
20 |
Correct |
1 ms |
1388 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
1536 KB |
Output is correct |
2 |
Correct |
1 ms |
1388 KB |
Output is correct |
3 |
Correct |
1 ms |
1388 KB |
Output is correct |
4 |
Correct |
1 ms |
1388 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
1388 KB |
Output is correct |
2 |
Correct |
1 ms |
1388 KB |
Output is correct |
3 |
Correct |
1 ms |
1388 KB |
Output is correct |
4 |
Correct |
1 ms |
1388 KB |
Output is correct |
5 |
Correct |
1 ms |
1388 KB |
Output is correct |
6 |
Correct |
1 ms |
1388 KB |
Output is correct |
7 |
Correct |
1 ms |
1388 KB |
Output is correct |
8 |
Correct |
1 ms |
1388 KB |
Output is correct |
9 |
Correct |
1 ms |
1388 KB |
Output is correct |
10 |
Correct |
2 ms |
1388 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
1516 KB |
Output is correct |
2 |
Correct |
1 ms |
1388 KB |
Output is correct |
3 |
Correct |
1 ms |
1388 KB |
Output is correct |
4 |
Correct |
1 ms |
1388 KB |
Output is correct |
5 |
Correct |
1 ms |
1388 KB |
Output is correct |
6 |
Correct |
1 ms |
1388 KB |
Output is correct |
7 |
Correct |
1 ms |
1388 KB |
Output is correct |
8 |
Correct |
1 ms |
1388 KB |
Output is correct |
9 |
Correct |
1 ms |
1388 KB |
Output is correct |
10 |
Correct |
1 ms |
1388 KB |
Output is correct |
11 |
Correct |
1 ms |
1388 KB |
Output is correct |
12 |
Correct |
1 ms |
1388 KB |
Output is correct |
13 |
Correct |
1 ms |
1388 KB |
Output is correct |
14 |
Correct |
1 ms |
1388 KB |
Output is correct |
15 |
Correct |
1 ms |
1388 KB |
Output is correct |
16 |
Correct |
1 ms |
1388 KB |
Output is correct |
17 |
Correct |
1 ms |
1388 KB |
Output is correct |
18 |
Correct |
1 ms |
1388 KB |
Output is correct |
19 |
Correct |
1 ms |
1388 KB |
Output is correct |
20 |
Correct |
1 ms |
1388 KB |
Output is correct |
21 |
Correct |
1 ms |
1536 KB |
Output is correct |
22 |
Correct |
1 ms |
1388 KB |
Output is correct |
23 |
Correct |
1 ms |
1388 KB |
Output is correct |
24 |
Correct |
1 ms |
1388 KB |
Output is correct |
25 |
Correct |
1 ms |
1388 KB |
Output is correct |
26 |
Correct |
1 ms |
1388 KB |
Output is correct |
27 |
Correct |
1 ms |
1388 KB |
Output is correct |
28 |
Correct |
1 ms |
1388 KB |
Output is correct |
29 |
Correct |
1 ms |
1388 KB |
Output is correct |
30 |
Correct |
1 ms |
1388 KB |
Output is correct |
31 |
Correct |
1 ms |
1388 KB |
Output is correct |
32 |
Correct |
1 ms |
1388 KB |
Output is correct |
33 |
Correct |
1 ms |
1388 KB |
Output is correct |
34 |
Correct |
2 ms |
1388 KB |
Output is correct |
35 |
Correct |
5 ms |
1516 KB |
Output is correct |
36 |
Correct |
1 ms |
1388 KB |
Output is correct |
37 |
Correct |
6 ms |
1644 KB |
Output is correct |
38 |
Correct |
1 ms |
1388 KB |
Output is correct |
39 |
Correct |
56 ms |
3052 KB |
Output is correct |
40 |
Correct |
55 ms |
2924 KB |
Output is correct |
41 |
Correct |
2 ms |
1900 KB |
Output is correct |
42 |
Correct |
55 ms |
2924 KB |
Output is correct |
43 |
Correct |
92 ms |
5852 KB |
Output is correct |
44 |
Correct |
2 ms |
1900 KB |
Output is correct |