#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 pll pair<ll, ll>
#define pii pair<int, int>
#define all(v) v.begin(), v.end()
#define rall(v) v.begin(), v.end()
#define pb push_back
#define p_b pop_back
#define f first
#define s second
using namespace std;
const ll sz = 600;
vector<pair<pll, ll>>g[sz][sz];
ll dist[sz][sz], vis[sz][sz];
void dijkstra(pll node)
{
for(ll i = 0; i < sz; i++)
{
for(ll j = 0; j < sz; j++)
dist[i][j] = 0x3F3F3F3F;
}
dist[node.f][node.s] = 0;
priority_queue<pair<ll, pll>>pq;
pq.push({0, {node.f, node.s}});
while(!pq.empty())
{
ll a = pq.top().s.f, b = pq.top().s.s;
pq.pop();
if(vis[a][b])
continue;
vis[a][b] = 1;
for(auto [u, w] : g[a][b])
{
if(dist[u.f][u.s] > dist[a][b] + w)
{
dist[u.f][u.s] = dist[a][b] + w;
pq.push({-dist[u.f][u.s], u});
}
}
}
}
void solve()
{
ll n, m, i, j;
cin >> n >> m;
char grid[n+5][m+5];
for(i = 1; i <= n; i++)
{
for(j = 1; j <= m; j++)
cin >> grid[i][j];
}
// E, S, W, N
for(i = 1; i <= n; i++)
{
for(j = 1; j <= m; j++)
{
if(grid[i][j] == 'W')
{
if(j - 1 >= 1)
g[i][j].pb({{i, j - 1}, 0});
if(j + 1 <= m)
g[i][j].pb({{i, j + 1}, 2});
if(i - 1 >= 1)
g[i][j].pb({{i - 1, j}, 1});
if(i + 1 <= n)
g[i][j].pb({{i + 1, j}, 3});
}
else if(grid[i][j] == 'N')
{
if(j - 1 >= 1)
g[i][j].pb({{i, j - 1}, 3});
if(j + 1 <= m)
g[i][j].pb({{i, j + 1}, 1});
if(i - 1 >= 1)
g[i][j].pb({{i - 1, j}, 0});
if(i + 1 <= n)
g[i][j].pb({{i + 1, j}, 2});
}
else if(grid[i][j] == 'E')
{
if(j - 1 >= 1)
g[i][j].pb({{i, j - 1}, 2});
if(j + 1 <= m)
g[i][j].pb({{i, j + 1}, 0});
if(i - 1 >= 1)
g[i][j].pb({{i - 1, j}, 3});
if(i + 1 <= n)
g[i][j].pb({{i + 1, j}, 1});
}
else if(grid[i][j] == 'S')
{
if(j - 1 >= 1)
g[i][j].pb({{i, j - 1}, 1});
if(j + 1 <= m)
g[i][j].pb({{i, j + 1}, 3});
if(i - 1 >= 1)
g[i][j].pb({{i - 1, j}, 2});
if(i + 1 <= n)
g[i][j].pb({{i + 1, j}, 0});
}
}
}
dijkstra({1, 1});
cout << (dist[n][m] == 0x3F3F3F3F ? -1 : 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();
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
12124 KB |
Output is correct |
2 |
Correct |
4 ms |
12124 KB |
Output is correct |
3 |
Correct |
7 ms |
12380 KB |
Output is correct |
4 |
Correct |
4 ms |
12120 KB |
Output is correct |
5 |
Correct |
4 ms |
12124 KB |
Output is correct |
6 |
Correct |
4 ms |
12336 KB |
Output is correct |
7 |
Correct |
4 ms |
12124 KB |
Output is correct |
8 |
Correct |
3 ms |
12124 KB |
Output is correct |
9 |
Correct |
4 ms |
12172 KB |
Output is correct |
10 |
Correct |
3 ms |
12124 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
12124 KB |
Output is correct |
2 |
Correct |
4 ms |
12124 KB |
Output is correct |
3 |
Correct |
7 ms |
12380 KB |
Output is correct |
4 |
Correct |
4 ms |
12120 KB |
Output is correct |
5 |
Correct |
4 ms |
12124 KB |
Output is correct |
6 |
Correct |
4 ms |
12336 KB |
Output is correct |
7 |
Correct |
4 ms |
12124 KB |
Output is correct |
8 |
Correct |
3 ms |
12124 KB |
Output is correct |
9 |
Correct |
4 ms |
12172 KB |
Output is correct |
10 |
Correct |
3 ms |
12124 KB |
Output is correct |
11 |
Correct |
4 ms |
12124 KB |
Output is correct |
12 |
Correct |
4 ms |
12120 KB |
Output is correct |
13 |
Correct |
3 ms |
12120 KB |
Output is correct |
14 |
Correct |
3 ms |
12124 KB |
Output is correct |
15 |
Correct |
4 ms |
12276 KB |
Output is correct |
16 |
Correct |
4 ms |
12124 KB |
Output is correct |
17 |
Correct |
4 ms |
12124 KB |
Output is correct |
18 |
Correct |
5 ms |
12124 KB |
Output is correct |
19 |
Correct |
4 ms |
12324 KB |
Output is correct |
20 |
Correct |
4 ms |
12316 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
12120 KB |
Output is correct |
2 |
Correct |
4 ms |
12376 KB |
Output is correct |
3 |
Correct |
4 ms |
12124 KB |
Output is correct |
4 |
Correct |
4 ms |
12124 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
12380 KB |
Output is correct |
2 |
Correct |
4 ms |
12124 KB |
Output is correct |
3 |
Correct |
4 ms |
12636 KB |
Output is correct |
4 |
Correct |
4 ms |
12132 KB |
Output is correct |
5 |
Correct |
4 ms |
12380 KB |
Output is correct |
6 |
Correct |
4 ms |
12380 KB |
Output is correct |
7 |
Correct |
4 ms |
12124 KB |
Output is correct |
8 |
Correct |
4 ms |
12316 KB |
Output is correct |
9 |
Correct |
4 ms |
12124 KB |
Output is correct |
10 |
Correct |
4 ms |
12124 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
12124 KB |
Output is correct |
2 |
Correct |
4 ms |
12124 KB |
Output is correct |
3 |
Correct |
7 ms |
12380 KB |
Output is correct |
4 |
Correct |
4 ms |
12120 KB |
Output is correct |
5 |
Correct |
4 ms |
12124 KB |
Output is correct |
6 |
Correct |
4 ms |
12336 KB |
Output is correct |
7 |
Correct |
4 ms |
12124 KB |
Output is correct |
8 |
Correct |
3 ms |
12124 KB |
Output is correct |
9 |
Correct |
4 ms |
12172 KB |
Output is correct |
10 |
Correct |
3 ms |
12124 KB |
Output is correct |
11 |
Correct |
4 ms |
12124 KB |
Output is correct |
12 |
Correct |
4 ms |
12120 KB |
Output is correct |
13 |
Correct |
3 ms |
12120 KB |
Output is correct |
14 |
Correct |
3 ms |
12124 KB |
Output is correct |
15 |
Correct |
4 ms |
12276 KB |
Output is correct |
16 |
Correct |
4 ms |
12124 KB |
Output is correct |
17 |
Correct |
4 ms |
12124 KB |
Output is correct |
18 |
Correct |
5 ms |
12124 KB |
Output is correct |
19 |
Correct |
4 ms |
12324 KB |
Output is correct |
20 |
Correct |
4 ms |
12316 KB |
Output is correct |
21 |
Correct |
4 ms |
12120 KB |
Output is correct |
22 |
Correct |
4 ms |
12376 KB |
Output is correct |
23 |
Correct |
4 ms |
12124 KB |
Output is correct |
24 |
Correct |
4 ms |
12124 KB |
Output is correct |
25 |
Correct |
4 ms |
12380 KB |
Output is correct |
26 |
Correct |
4 ms |
12124 KB |
Output is correct |
27 |
Correct |
4 ms |
12636 KB |
Output is correct |
28 |
Correct |
4 ms |
12132 KB |
Output is correct |
29 |
Correct |
4 ms |
12380 KB |
Output is correct |
30 |
Correct |
4 ms |
12380 KB |
Output is correct |
31 |
Correct |
4 ms |
12124 KB |
Output is correct |
32 |
Correct |
4 ms |
12316 KB |
Output is correct |
33 |
Correct |
4 ms |
12124 KB |
Output is correct |
34 |
Correct |
4 ms |
12124 KB |
Output is correct |
35 |
Correct |
9 ms |
14428 KB |
Output is correct |
36 |
Correct |
4 ms |
12460 KB |
Output is correct |
37 |
Correct |
11 ms |
15172 KB |
Output is correct |
38 |
Correct |
11 ms |
15964 KB |
Output is correct |
39 |
Correct |
81 ms |
37072 KB |
Output is correct |
40 |
Correct |
84 ms |
36828 KB |
Output is correct |
41 |
Correct |
31 ms |
34628 KB |
Output is correct |
42 |
Correct |
82 ms |
36948 KB |
Output is correct |
43 |
Correct |
101 ms |
48064 KB |
Output is correct |
44 |
Correct |
31 ms |
34640 KB |
Output is correct |