#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
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;
| ^~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
4696 KB |
Output is correct |
2 |
Correct |
1 ms |
4440 KB |
Output is correct |
3 |
Correct |
1 ms |
4444 KB |
Output is correct |
4 |
Correct |
1 ms |
4444 KB |
Output is correct |
5 |
Correct |
1 ms |
4444 KB |
Output is correct |
6 |
Correct |
1 ms |
4444 KB |
Output is correct |
7 |
Correct |
2 ms |
4440 KB |
Output is correct |
8 |
Correct |
1 ms |
4440 KB |
Output is correct |
9 |
Correct |
1 ms |
4444 KB |
Output is correct |
10 |
Correct |
2 ms |
4444 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
4696 KB |
Output is correct |
2 |
Correct |
1 ms |
4440 KB |
Output is correct |
3 |
Correct |
1 ms |
4444 KB |
Output is correct |
4 |
Correct |
1 ms |
4444 KB |
Output is correct |
5 |
Correct |
1 ms |
4444 KB |
Output is correct |
6 |
Correct |
1 ms |
4444 KB |
Output is correct |
7 |
Correct |
2 ms |
4440 KB |
Output is correct |
8 |
Correct |
1 ms |
4440 KB |
Output is correct |
9 |
Correct |
1 ms |
4444 KB |
Output is correct |
10 |
Correct |
2 ms |
4444 KB |
Output is correct |
11 |
Correct |
1 ms |
4444 KB |
Output is correct |
12 |
Correct |
1 ms |
4444 KB |
Output is correct |
13 |
Correct |
1 ms |
4444 KB |
Output is correct |
14 |
Correct |
1 ms |
4444 KB |
Output is correct |
15 |
Correct |
1 ms |
4444 KB |
Output is correct |
16 |
Correct |
1 ms |
4440 KB |
Output is correct |
17 |
Correct |
1 ms |
4444 KB |
Output is correct |
18 |
Correct |
1 ms |
4440 KB |
Output is correct |
19 |
Correct |
1 ms |
4696 KB |
Output is correct |
20 |
Correct |
2 ms |
4444 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
4444 KB |
Output is correct |
2 |
Correct |
1 ms |
4444 KB |
Output is correct |
3 |
Correct |
1 ms |
4444 KB |
Output is correct |
4 |
Correct |
1 ms |
4444 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
4440 KB |
Output is correct |
2 |
Correct |
1 ms |
4444 KB |
Output is correct |
3 |
Correct |
1 ms |
4444 KB |
Output is correct |
4 |
Correct |
1 ms |
4444 KB |
Output is correct |
5 |
Correct |
1 ms |
4444 KB |
Output is correct |
6 |
Correct |
1 ms |
4444 KB |
Output is correct |
7 |
Correct |
1 ms |
4444 KB |
Output is correct |
8 |
Correct |
1 ms |
4444 KB |
Output is correct |
9 |
Correct |
1 ms |
4444 KB |
Output is correct |
10 |
Correct |
1 ms |
4444 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
4696 KB |
Output is correct |
2 |
Correct |
1 ms |
4440 KB |
Output is correct |
3 |
Correct |
1 ms |
4444 KB |
Output is correct |
4 |
Correct |
1 ms |
4444 KB |
Output is correct |
5 |
Correct |
1 ms |
4444 KB |
Output is correct |
6 |
Correct |
1 ms |
4444 KB |
Output is correct |
7 |
Correct |
2 ms |
4440 KB |
Output is correct |
8 |
Correct |
1 ms |
4440 KB |
Output is correct |
9 |
Correct |
1 ms |
4444 KB |
Output is correct |
10 |
Correct |
2 ms |
4444 KB |
Output is correct |
11 |
Correct |
1 ms |
4444 KB |
Output is correct |
12 |
Correct |
1 ms |
4444 KB |
Output is correct |
13 |
Correct |
1 ms |
4444 KB |
Output is correct |
14 |
Correct |
1 ms |
4444 KB |
Output is correct |
15 |
Correct |
1 ms |
4444 KB |
Output is correct |
16 |
Correct |
1 ms |
4440 KB |
Output is correct |
17 |
Correct |
1 ms |
4444 KB |
Output is correct |
18 |
Correct |
1 ms |
4440 KB |
Output is correct |
19 |
Correct |
1 ms |
4696 KB |
Output is correct |
20 |
Correct |
2 ms |
4444 KB |
Output is correct |
21 |
Correct |
1 ms |
4444 KB |
Output is correct |
22 |
Correct |
1 ms |
4444 KB |
Output is correct |
23 |
Correct |
1 ms |
4444 KB |
Output is correct |
24 |
Correct |
1 ms |
4444 KB |
Output is correct |
25 |
Correct |
1 ms |
4440 KB |
Output is correct |
26 |
Correct |
1 ms |
4444 KB |
Output is correct |
27 |
Correct |
1 ms |
4444 KB |
Output is correct |
28 |
Correct |
1 ms |
4444 KB |
Output is correct |
29 |
Correct |
1 ms |
4444 KB |
Output is correct |
30 |
Correct |
1 ms |
4444 KB |
Output is correct |
31 |
Correct |
1 ms |
4444 KB |
Output is correct |
32 |
Correct |
1 ms |
4444 KB |
Output is correct |
33 |
Correct |
1 ms |
4444 KB |
Output is correct |
34 |
Correct |
1 ms |
4444 KB |
Output is correct |
35 |
Correct |
10 ms |
4444 KB |
Output is correct |
36 |
Correct |
1 ms |
4444 KB |
Output is correct |
37 |
Correct |
12 ms |
4616 KB |
Output is correct |
38 |
Correct |
3 ms |
4440 KB |
Output is correct |
39 |
Correct |
100 ms |
5208 KB |
Output is correct |
40 |
Correct |
96 ms |
5056 KB |
Output is correct |
41 |
Correct |
6 ms |
4696 KB |
Output is correct |
42 |
Correct |
97 ms |
4956 KB |
Output is correct |
43 |
Correct |
125 ms |
30896 KB |
Output is correct |
44 |
Correct |
5 ms |
4700 KB |
Output is correct |