#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define int long long
#define double long double
#define endl '\n'
#define sz(a) (int)a.size()
#define pb push_back
#define fs first
#define sc second
#define all(v) v.begin(), v.end()
#define rall(v) v.rbegin(), v.rend()
int const INF = LONG_LONG_MAX;
int n, m;
string s[505];
vector<vector<int>> dist(505, vector<int>(505, INF));
priority_queue<pair<int, pair<int, int>>, vector<pair<int, pair<int, int>>>, greater<pair<int, pair<int, int>>>> pq;
int dirx[4]={-1, 0, 1, 0}, diry[4]={0, 1, 0, -1};
bool cost(char c, int* arr)
{
if(c=='X'){return 1;}
else if(c=='N')
{
arr[0]=0;arr[1]=1;arr[2]=2;arr[3]=3;
}
else if(c=='E')
{
arr[0]=3;arr[1]=0;arr[2]=1;arr[3]=2;
}
else if(c=='S')
{
arr[0]=2;arr[1]=3;arr[2]=0;arr[3]=1;
}
else if(c=='W')
{
arr[0]=1;arr[1]=2;arr[2]=3;arr[3]=0;
}
return 0;
}
void dikj()
{
while(!pq.empty())
{
pair<int, pair<int, int>> p;
p=pq.top();
pq.pop();
int d=p.fs, cx=p.sc.fs, cy=p.sc.sc;
if(d!=dist[cx][cy]){continue;}
int val[4];
if(cost(s[cx][cy], val)){continue;}
for(int i=0;i<4;i++)
{
int tox=cx+dirx[i], toy=cy+diry[i];
if(tox>=0 && tox<n && toy>=0 && toy<m)
{
if(dist[tox][toy]>d+val[i])
{
dist[tox][toy]=d+val[i];
pq.push({dist[tox][toy], {tox, toy}});
}
}
}
}
}
void solve()
{
cin>>n>>m;
for(int i=0;i<n;i++)
{
cin>>s[i];
}
dist[0][0]=0;
pq.push({dist[0][0], {0, 0}});
dikj();
if(dist[n-1][m-1]==INF){cout<<-1;}else{cout<<dist[n-1][m-1];}
}
int32_t main()
{
ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
int tc=1;
//cin>>tc;
while(tc--)
{
solve();
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
2260 KB |
Output is correct |
2 |
Correct |
2 ms |
2260 KB |
Output is correct |
3 |
Correct |
2 ms |
2260 KB |
Output is correct |
4 |
Correct |
1 ms |
2260 KB |
Output is correct |
5 |
Correct |
2 ms |
2260 KB |
Output is correct |
6 |
Correct |
2 ms |
2356 KB |
Output is correct |
7 |
Correct |
2 ms |
2260 KB |
Output is correct |
8 |
Correct |
2 ms |
2356 KB |
Output is correct |
9 |
Correct |
1 ms |
2344 KB |
Output is correct |
10 |
Correct |
1 ms |
2260 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
2260 KB |
Output is correct |
2 |
Correct |
2 ms |
2260 KB |
Output is correct |
3 |
Correct |
2 ms |
2260 KB |
Output is correct |
4 |
Correct |
1 ms |
2260 KB |
Output is correct |
5 |
Correct |
2 ms |
2260 KB |
Output is correct |
6 |
Correct |
2 ms |
2356 KB |
Output is correct |
7 |
Correct |
2 ms |
2260 KB |
Output is correct |
8 |
Correct |
2 ms |
2356 KB |
Output is correct |
9 |
Correct |
1 ms |
2344 KB |
Output is correct |
10 |
Correct |
1 ms |
2260 KB |
Output is correct |
11 |
Correct |
1 ms |
2356 KB |
Output is correct |
12 |
Correct |
1 ms |
2260 KB |
Output is correct |
13 |
Correct |
2 ms |
2352 KB |
Output is correct |
14 |
Correct |
1 ms |
2260 KB |
Output is correct |
15 |
Correct |
1 ms |
2260 KB |
Output is correct |
16 |
Correct |
2 ms |
2260 KB |
Output is correct |
17 |
Correct |
2 ms |
2284 KB |
Output is correct |
18 |
Correct |
2 ms |
2260 KB |
Output is correct |
19 |
Correct |
2 ms |
2260 KB |
Output is correct |
20 |
Correct |
2 ms |
2260 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
2352 KB |
Output is correct |
2 |
Correct |
1 ms |
2360 KB |
Output is correct |
3 |
Correct |
2 ms |
2348 KB |
Output is correct |
4 |
Correct |
2 ms |
2260 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2352 KB |
Output is correct |
2 |
Correct |
1 ms |
2260 KB |
Output is correct |
3 |
Correct |
1 ms |
2260 KB |
Output is correct |
4 |
Correct |
2 ms |
2352 KB |
Output is correct |
5 |
Correct |
2 ms |
2260 KB |
Output is correct |
6 |
Correct |
2 ms |
2260 KB |
Output is correct |
7 |
Correct |
2 ms |
2260 KB |
Output is correct |
8 |
Correct |
1 ms |
2260 KB |
Output is correct |
9 |
Correct |
2 ms |
2260 KB |
Output is correct |
10 |
Correct |
1 ms |
2260 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
2260 KB |
Output is correct |
2 |
Correct |
2 ms |
2260 KB |
Output is correct |
3 |
Correct |
2 ms |
2260 KB |
Output is correct |
4 |
Correct |
1 ms |
2260 KB |
Output is correct |
5 |
Correct |
2 ms |
2260 KB |
Output is correct |
6 |
Correct |
2 ms |
2356 KB |
Output is correct |
7 |
Correct |
2 ms |
2260 KB |
Output is correct |
8 |
Correct |
2 ms |
2356 KB |
Output is correct |
9 |
Correct |
1 ms |
2344 KB |
Output is correct |
10 |
Correct |
1 ms |
2260 KB |
Output is correct |
11 |
Correct |
1 ms |
2356 KB |
Output is correct |
12 |
Correct |
1 ms |
2260 KB |
Output is correct |
13 |
Correct |
2 ms |
2352 KB |
Output is correct |
14 |
Correct |
1 ms |
2260 KB |
Output is correct |
15 |
Correct |
1 ms |
2260 KB |
Output is correct |
16 |
Correct |
2 ms |
2260 KB |
Output is correct |
17 |
Correct |
2 ms |
2284 KB |
Output is correct |
18 |
Correct |
2 ms |
2260 KB |
Output is correct |
19 |
Correct |
2 ms |
2260 KB |
Output is correct |
20 |
Correct |
2 ms |
2260 KB |
Output is correct |
21 |
Correct |
2 ms |
2352 KB |
Output is correct |
22 |
Correct |
1 ms |
2360 KB |
Output is correct |
23 |
Correct |
2 ms |
2348 KB |
Output is correct |
24 |
Correct |
2 ms |
2260 KB |
Output is correct |
25 |
Correct |
1 ms |
2352 KB |
Output is correct |
26 |
Correct |
1 ms |
2260 KB |
Output is correct |
27 |
Correct |
1 ms |
2260 KB |
Output is correct |
28 |
Correct |
2 ms |
2352 KB |
Output is correct |
29 |
Correct |
2 ms |
2260 KB |
Output is correct |
30 |
Correct |
2 ms |
2260 KB |
Output is correct |
31 |
Correct |
2 ms |
2260 KB |
Output is correct |
32 |
Correct |
1 ms |
2260 KB |
Output is correct |
33 |
Correct |
2 ms |
2260 KB |
Output is correct |
34 |
Correct |
1 ms |
2260 KB |
Output is correct |
35 |
Correct |
5 ms |
2388 KB |
Output is correct |
36 |
Correct |
1 ms |
2356 KB |
Output is correct |
37 |
Correct |
7 ms |
2380 KB |
Output is correct |
38 |
Correct |
2 ms |
2364 KB |
Output is correct |
39 |
Correct |
55 ms |
2832 KB |
Output is correct |
40 |
Correct |
51 ms |
2924 KB |
Output is correct |
41 |
Correct |
3 ms |
2836 KB |
Output is correct |
42 |
Correct |
53 ms |
2984 KB |
Output is correct |
43 |
Correct |
86 ms |
9020 KB |
Output is correct |
44 |
Correct |
2 ms |
2748 KB |
Output is correct |