This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#pragma GCC optimize("-O3")
#include<bits/stdc++.h>
#include<ext/pb_ds/assoc_container.hpp>
#include<ext/pb_ds/tree_policy.hpp>
#define ll long long
#define ld long double
#define vl vector<ll>
#define vi vector<int>
#define all(v) v.begin(), v.end()
#define rall(v) v.rbegin(), v.rend()
#define f first
#define s second
#define pii pair<int, int>
#define pll pair<ll, ll>
#define pb push_back
#define p_b pop_back
using namespace std;
using namespace __gnu_pbds;
typedef tree<ll, null_type, less_equal<ll>, rb_tree_tag, tree_order_statistics_node_update> ordered_set;
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
const ll sz = 505;
ll dist[sz][sz], vis[sz][sz], n, m, i, j;
char grid[sz][sz];
void f()
{
for(i = 1; i < sz; i++)
{
for(j = 1; j < sz; j++)
dist[i][j] = 1e15;
}
dist[i][j] = 0;
priority_queue<pair<ll, pll>>pq;
pq.push({0, {1, 1}});
while(!pq.empty())
{
pll node = pq.top().s;
ll cst = pq.top().f;
pq.pop();
if(vis[node.f][node.s])
continue;
dist[node.f][node.s] = -cst;
vis[node.f][node.s] = 1;
//cout << node.f << ' ' << node.s << "\n";
if(grid[node.f][node.s] == 'E')
{
if(node.s - 1 >= 1)
pq.push({-(dist[node.f][node.s]+2), {node.f, node.s-1}});
if(node.s + 1 <= m)
pq.push({-dist[node.f][node.s], {node.f, node.s+1}});
if(node.f - 1 >= 1)
pq.push({-(dist[node.f][node.s]+3), {node.f-1, node.s}});
if(node.f + 1 <= n)
pq.push({-(dist[node.f][node.s]+1), {node.f+1, node.s}});
}
else if(grid[node.f][node.s] == 'S')
{
if(node.s - 1 >= 1)
pq.push({-(dist[node.f][node.s]+1), {node.f, node.s-1}});
if(node.s + 1 <= m)
pq.push({-(dist[node.f][node.s]+3), {node.f, node.s+1}});
if(node.f - 1 >= 1)
pq.push({-(dist[node.f][node.s]+2), {node.f-1, node.s}});
if(node.f + 1 <= n)
pq.push({-dist[node.f][node.s], {node.f+1, node.s}});
}
else if(grid[node.f][node.s] == 'W')
{
if(node.s - 1 >= 1)
pq.push({-dist[node.f][node.s], {node.f, node.s-1}});
if(node.s + 1 <= m)
pq.push({-(dist[node.f][node.s]+2), {node.f, node.s+1}});
if(node.f - 1 >= 1)
pq.push({-(dist[node.f][node.s]+1), {node.f-1, node.s}});
if(node.f + 1 <= n)
pq.push({-(dist[node.f][node.s]+3), {node.f+1, node.s}});
}
else if(grid[node.f][node.s] == 'N')
{
if(node.s - 1 >= 1)
pq.push({-(dist[node.f][node.s]+3), {node.f, node.s-1}});
if(node.s + 1 <= m)
pq.push({-(dist[node.f][node.s]+1), {node.f, node.s+1}});
if(node.f - 1 >= 1)
pq.push({-dist[node.f][node.s], {node.f-1, node.s}});
if(node.f + 1 <= n)
pq.push({-(dist[node.f][node.s]+2), {node.f+1, node.s}});
}
}
}
void solve()
{
cin >> n >> m;
for(i = 1; i <= n; i++)
{
string s;
cin >> s;
for(j = 1; j <= m; j++)
grid[i][j] = s[j-1];
}
f();
if(dist[n][m] == 1e15)
cout << "-1\n";
else
cout << dist[n][m] << "\n";
}
int main()
{
//ios_base::sync_with_stdio(0);
//cin.tie(0);
//cout.tie(0);
ll tests = 1;
//cin >> tests;
while(tests--)
{
solve();
}
}
Compilation message (stderr)
adventure.cpp: In function 'void first()':
adventure.cpp:31:11: warning: array subscript 505 is above array bounds of 'long long int [505][505]' [-Warray-bounds]
31 | dist[i][j] = 0;
| ~~~~~~^
adventure.cpp:22:4: note: while referencing 'dist'
22 | ll dist[sz][sz], vis[sz][sz], n, m, i, j;
| ^~~~
# | 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... |