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>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define fr(x, l, r) for(int x = l; x <= r; x++)
#define rf(x, l, r) for(int x = l; x >= r; x--)
#define fe(x, y) for(auto& x : y)
#define fi first
#define se second
#define m_p make_pair
#define pb push_back
#define pw(x) (ull(1) << ull(x))
#define all(x) (x).begin(),x.end()
#define sz(x) (int)x.size()
#define mt make_tuple
#define ve vector
using namespace std;
using namespace __gnu_pbds;
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
typedef pair <ll,ll> pll;
typedef pair <int,int> pii;
typedef pair <ld,ld> pld;
template<typename T>
using oset = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
#define fbo find_by_order
#define ook order_of_key
template<typename T>
bool umn(T& a, T b) { return a > b ? a = b, 1 : 0; }
template<typename T>
bool umx(T& a, T b) { return a < b ? a = b, 1 : 0; }
const ll inf = 1e18;
const int intf = 1e9;
const ll mod = 1e9 + 7;
const ld eps = 0.00000001;
const ll N = 600;
const char k[4] = {'W', 'S', 'E', 'N'};
char c[N][N];
ll f[N][N];
int n, m;
bool update(int x, int y){
return (x >= 0 && x < n && y >= 0 && y < m);
}
int main(){
#ifdef LOCAL
freopen("input.txt", "r", stdin);
freopen("output.txt","w", stdout);
ios_base::sync_with_stdio(0);
cin.tie(0);
#else
// freopen("mountains.in", "r", stdin);
// freopen("mountains.out","w", stdout);
ios_base::sync_with_stdio(0);
cin.tie(0);
#endif
cin >> n >> m;
fr(i, 0, n - 1){
fill (f[i], f[i] + m, inf);
}
fr(i, 0, n - 1){
fr(j, 0, m - 1){
cin >> c[i][j];
}
}
f[0][0] = 0;
set <pair <ll, pll> > st;
st.insert({0,{0,0}});
while(!st.empty()){
auto it = st.begin();
ll len = (*it).fi, x = (*it).se.fi, y = (*it).se.se;
st.erase(it);
if(c[x][y] == 'X'){
continue;
}
fr(i, 0, 3){
ll l, _x, _y;
if(k[i] == c[x][y]){
l = 0;
}
else if(k[(i + 2)%4] == c[x][y]){
l = 2;
}
else if(k[(i + 1)%4] == c[x][y]){
l = 1;
}
else l = 3;
if(k[i] == 'N'){
_x = -1;
_y = 0;
}
if(k[i] == 'E'){
_x = 0;
_y = 1;
}
if(k[i] == 'S'){
_x = 1;
_y = 0;
}
if(k[i] == 'W'){
_x = 0;
_y = -1;
}
if(update(x + _x, y + _y)){
// cout << c[x][y] << " " << k[i] << " " << l << " " << _x << " " << _y<< endl;
if(f[x + _x][y + _y] > len + l){
st.erase({f[x + _x][y + _y], {x + _x, y + _y}});
f[x + _x][y + _y] = len + l;
st.insert({f[x + _x][y + _y], {x + _x, y + _y}});
}
}
}
}
if(f[n - 1][m - 1] != inf)cout << f[n - 1][m - 1] << endl;
else cout << -1 << endl;
return 0;
}
Compilation message (stderr)
adventure.cpp: In function 'int main()':
adventure.cpp:123:33: warning: '_y' may be used uninitialized in this function [-Wmaybe-uninitialized]
123 | if(update(x + _x, y + _y)){
| ~~^~~~
adventure.cpp:123:25: warning: '_x' may be used uninitialized in this function [-Wmaybe-uninitialized]
123 | if(update(x + _x, y + _y)){
| ~~^~~~
# | 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... |