/* ==========================
| Bismillahirrahmanirrahim | ⠀⠀
==========================
⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⣀⡀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠸⠁⠸⢳⡄⠀⠀⠀⠀⠀⠀⠀⠀
⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢠⠃⠀⠀⢸⠸⠀⡠⣄⠀⠀⠀⠀⠀
⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⡠⠃⠀⠀⢠⣞⣀⡿⠀⠀⣧⠀⠀⠀⠀
⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⣀⣠⡖⠁⠀⠀⠀⢸⠈⢈⡇⠀⢀⡏⠀⠀⠀⠀
⠀⠀⠀⠀⠀⠀⠀⠀⠀⡴⠩⢠⡴⠀⠀⠀⠀⠀⠈⡶⠉⠀⠀⡸⠀⠀⠀⠀⠀
⠀⠀⠀⠀⠀⠀⠀⢀⠎⢠⣇⠏⠀⠀⠀⠀⠀⠀⠀⠁⠀⢀⠄⡇⠀⠀⠀⠀⠀
⠀⠀⠀⠀⠀⠀⢠⠏⠀⢸⣿⣴⠀⠀⠀⠀⠀⠀⣆⣀⢾⢟⠴⡇⠀⠀⠀⠀⠀<===== this is wolf, not everest
⠀⠀⠀⠀⠀⢀⣿⠀⠠⣄⠸⢹⣦⠀⠀⡄⠀⠀⢋⡟⠀⠀⠁⣇⠀⠀⠀⠀⠀
⠀⠀⠀⠀⢀⡾⠁⢠⠀⣿⠃⠘⢹⣦⢠⣼⠀⠀⠉⠀⠀⠀⠀⢸⡀⠀⠀⠀⠀
⠀⠀⢀⣴⠫⠤⣶⣿⢀⡏⠀⠀⠘⢸⡟⠋⠀⠀⠀⠀⠀⠀⠀⠀⢳⠀⠀⠀⠀
⠐⠿⢿⣿⣤⣴⣿⣣⢾⡄⠀⠀⠀⠀⠳⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢣⠀⠀⠀
⠀⠀⠀⣨⣟⡍⠉⠚⠹⣇⡄⠀⠀⠀⠀⠀⠀⠀⠀⠈⢦⠀⠀⢀⡀⣾⡇⠀⠀
⠀⠀⢠⠟⣹⣧⠃⠀⠀⢿⢻⡀⢄⠀⠀⠀⠀⠐⣦⡀⣸⣆⠀⣾⣧⣯⢻⠀⠀
⠀⠀⠘⣰⣿⣿⡄⡆⠀⠀⠀⠳⣼⢦⡘⣄⠀⠀⡟⡷⠃⠘⢶⣿⡎⠻⣆⠀⠀
⠀⠀⠀⡟⡿⢿⡿⠀⠀⠀⠀⠀⠙⠀⠻⢯⢷⣼⠁⠁⠀⠀⠀⠙⢿⡄⡈⢆⠀
⠀⠀⠀⠀⡇⣿⡅⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠙⠦⠀⠀⠀⠀⠀⠀⡇⢹⢿⡀
⠀⠀⠀⠀⠁⠛⠓⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠼⠇⠁
m : 11059739 -> l ~23
p : 4567896467
*/
#pragma GCC optimize("O3", "inline")
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
using namespace std;
#define all(v) v.begin(), v.end()
#define ins insert
#define pb push_back
#define int long long int
#define pii pair<int, int>
#define str string
#define endl '\n'
#define drop(x) cout<<(x)<<endl;return;
// mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
template <class T> using ot = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
const int mod = 998244353, sze =1e6 + 10, inf = 1e9, prime = 4567896467;
vector<pair<int,int>> graph[sze];
int dist[sze];
int yc[sze];
int cc(char a,char b){
if(yc[a]<=yc[b]){
return yc[b]-yc[a];
}
return 4 - yc[a] + yc[b];
}
int used[sze];
pii p[600][600];
void mal(){
int n,m;
cin>>n>>m;
yc['N']=0;
yc['E']=1;
yc['S']=2;
yc['W']=3;
vector<char> yonler = {'N','E','S','W'};
auto idx = [&](int a,int b){
return a *m +b;
};
for(int i=0;i<sze;i++){
dist[i]=inf;
}
vector<vector<char>> arr(n,vector<char>(m));
for(int i=0;i<n;i++){
for(int j=0;j<m;j++){
cin>>arr[i][j];
}
}
// cout<<cc('W','S')<<endl;
for(int i=0;i<n;i++){
for(int j=0;j<m;j++){
if(arr[i][j]!='X'){
for(auto v:yonler){
int c = cc(arr[i][j],v);
int i1 = i;
int j1 = j;
if(v=='E'){
j1++;
}
else if(v=='S'){
i1++;
}
else if(v=='N'){
i1--;
}
else{
j1--;
}
if(i1>=0 && j1>=0 && i1<n && j1<m){
// cout<<i<<":"<<j<<" -> "<<i1<<":"<<j1<<" cost:"<<c<< " p-----p " <<v<<endl;
graph[idx(i,j)].pb( { idx(i1,j1) , c });
}
}
}
}
}
set<pair<int,int>> q;
dist[idx(0,0)]=0;
q.insert({0,idx(0,0)});
p[0][0]={-1,-1};
while(!q.empty()){
pii node = *q.begin();
q.erase(q.begin());
for(auto v:graph[node.second]){
if(dist[v.first] > dist[node.second] + v.second){
q.erase({dist[v.first],v.first});
dist[v.first]=dist[node.second]+v.second;
p[v.first/m][v.first%m] = {node.second/m,node.second%m};
// cout<<node.second/m<<":"<<node.second%m<<" ->"<< v.first/m<<":"<<v.first%m<<" c:"<<dist[v.first] << endl;
q.insert({dist[v.first],v.first});
}
}
}
// cout<<p[n-1][m-1].first<<" "<<p[n-1][m-1].second<<endl;
int ans=dist[idx(n-1,m-1)];
if(ans>=inf){
ans=-1;
}
drop(ans);
}
signed main() {
cin.tie(0)->sync_with_stdio(0);
int tt = 1;
// cin>>tt;
while (tt--) mal();
}
Compilation message
adventure.cpp: In function 'long long int cc(char, char)':
adventure.cpp:51:11: warning: array subscript has type 'char' [-Wchar-subscripts]
51 | if(yc[a]<=yc[b]){
| ^
adventure.cpp:51:18: warning: array subscript has type 'char' [-Wchar-subscripts]
51 | if(yc[a]<=yc[b]){
| ^
adventure.cpp:52:19: warning: array subscript has type 'char' [-Wchar-subscripts]
52 | return yc[b]-yc[a];
| ^
adventure.cpp:52:25: warning: array subscript has type 'char' [-Wchar-subscripts]
52 | return yc[b]-yc[a];
| ^
adventure.cpp:54:19: warning: array subscript has type 'char' [-Wchar-subscripts]
54 | return 4 - yc[a] + yc[b];
| ^
adventure.cpp:54:27: warning: array subscript has type 'char' [-Wchar-subscripts]
54 | return 4 - yc[a] + yc[b];
| ^
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
34396 KB |
Output is correct |
2 |
Correct |
7 ms |
34252 KB |
Output is correct |
3 |
Correct |
8 ms |
34396 KB |
Output is correct |
4 |
Correct |
7 ms |
34648 KB |
Output is correct |
5 |
Correct |
8 ms |
34396 KB |
Output is correct |
6 |
Correct |
7 ms |
34396 KB |
Output is correct |
7 |
Correct |
7 ms |
34264 KB |
Output is correct |
8 |
Correct |
7 ms |
34396 KB |
Output is correct |
9 |
Correct |
7 ms |
34264 KB |
Output is correct |
10 |
Correct |
8 ms |
34396 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
34396 KB |
Output is correct |
2 |
Correct |
7 ms |
34252 KB |
Output is correct |
3 |
Correct |
8 ms |
34396 KB |
Output is correct |
4 |
Correct |
7 ms |
34648 KB |
Output is correct |
5 |
Correct |
8 ms |
34396 KB |
Output is correct |
6 |
Correct |
7 ms |
34396 KB |
Output is correct |
7 |
Correct |
7 ms |
34264 KB |
Output is correct |
8 |
Correct |
7 ms |
34396 KB |
Output is correct |
9 |
Correct |
7 ms |
34264 KB |
Output is correct |
10 |
Correct |
8 ms |
34396 KB |
Output is correct |
11 |
Correct |
8 ms |
34396 KB |
Output is correct |
12 |
Correct |
7 ms |
34396 KB |
Output is correct |
13 |
Correct |
8 ms |
34396 KB |
Output is correct |
14 |
Correct |
8 ms |
34396 KB |
Output is correct |
15 |
Correct |
7 ms |
34396 KB |
Output is correct |
16 |
Correct |
7 ms |
34436 KB |
Output is correct |
17 |
Correct |
8 ms |
34252 KB |
Output is correct |
18 |
Correct |
7 ms |
34408 KB |
Output is correct |
19 |
Correct |
8 ms |
34396 KB |
Output is correct |
20 |
Correct |
7 ms |
34396 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
34392 KB |
Output is correct |
2 |
Correct |
8 ms |
34424 KB |
Output is correct |
3 |
Correct |
8 ms |
34236 KB |
Output is correct |
4 |
Correct |
8 ms |
34392 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
34396 KB |
Output is correct |
2 |
Correct |
8 ms |
34396 KB |
Output is correct |
3 |
Correct |
7 ms |
34396 KB |
Output is correct |
4 |
Correct |
7 ms |
34460 KB |
Output is correct |
5 |
Correct |
7 ms |
34396 KB |
Output is correct |
6 |
Correct |
7 ms |
34396 KB |
Output is correct |
7 |
Correct |
7 ms |
34248 KB |
Output is correct |
8 |
Correct |
8 ms |
34392 KB |
Output is correct |
9 |
Correct |
8 ms |
34648 KB |
Output is correct |
10 |
Correct |
7 ms |
34396 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
34396 KB |
Output is correct |
2 |
Correct |
7 ms |
34252 KB |
Output is correct |
3 |
Correct |
8 ms |
34396 KB |
Output is correct |
4 |
Correct |
7 ms |
34648 KB |
Output is correct |
5 |
Correct |
8 ms |
34396 KB |
Output is correct |
6 |
Correct |
7 ms |
34396 KB |
Output is correct |
7 |
Correct |
7 ms |
34264 KB |
Output is correct |
8 |
Correct |
7 ms |
34396 KB |
Output is correct |
9 |
Correct |
7 ms |
34264 KB |
Output is correct |
10 |
Correct |
8 ms |
34396 KB |
Output is correct |
11 |
Correct |
8 ms |
34396 KB |
Output is correct |
12 |
Correct |
7 ms |
34396 KB |
Output is correct |
13 |
Correct |
8 ms |
34396 KB |
Output is correct |
14 |
Correct |
8 ms |
34396 KB |
Output is correct |
15 |
Correct |
7 ms |
34396 KB |
Output is correct |
16 |
Correct |
7 ms |
34436 KB |
Output is correct |
17 |
Correct |
8 ms |
34252 KB |
Output is correct |
18 |
Correct |
7 ms |
34408 KB |
Output is correct |
19 |
Correct |
8 ms |
34396 KB |
Output is correct |
20 |
Correct |
7 ms |
34396 KB |
Output is correct |
21 |
Correct |
7 ms |
34392 KB |
Output is correct |
22 |
Correct |
8 ms |
34424 KB |
Output is correct |
23 |
Correct |
8 ms |
34236 KB |
Output is correct |
24 |
Correct |
8 ms |
34392 KB |
Output is correct |
25 |
Correct |
8 ms |
34396 KB |
Output is correct |
26 |
Correct |
8 ms |
34396 KB |
Output is correct |
27 |
Correct |
7 ms |
34396 KB |
Output is correct |
28 |
Correct |
7 ms |
34460 KB |
Output is correct |
29 |
Correct |
7 ms |
34396 KB |
Output is correct |
30 |
Correct |
7 ms |
34396 KB |
Output is correct |
31 |
Correct |
7 ms |
34248 KB |
Output is correct |
32 |
Correct |
8 ms |
34392 KB |
Output is correct |
33 |
Correct |
8 ms |
34648 KB |
Output is correct |
34 |
Correct |
7 ms |
34396 KB |
Output is correct |
35 |
Correct |
13 ms |
37980 KB |
Output is correct |
36 |
Correct |
7 ms |
34512 KB |
Output is correct |
37 |
Correct |
15 ms |
38236 KB |
Output is correct |
38 |
Correct |
12 ms |
37080 KB |
Output is correct |
39 |
Correct |
81 ms |
56924 KB |
Output is correct |
40 |
Correct |
76 ms |
56912 KB |
Output is correct |
41 |
Correct |
32 ms |
50396 KB |
Output is correct |
42 |
Correct |
76 ms |
56876 KB |
Output is correct |
43 |
Correct |
70 ms |
60752 KB |
Output is correct |
44 |
Correct |
30 ms |
50528 KB |
Output is correct |