#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 pii pair<int, int>
#define pll pair<ll, ll>
#define all(v) v.begin(), v.end()
#define rall(v) v.rbegin(), v.rend()
#define pb push_back
#define p_b pop_back
#define f first
#define s second
using namespace std;
using namespace __gnu_pbds;
typedef tree<int, null_type, less_equal<int>, rb_tree_tag, tree_order_statistics_node_update> ordered_set;
const ll sz = 505;
char grid[sz+5][sz+5];
ll dist[sz+5][sz+5], n, m;
bool ok(ll x, ll y)
{
if(x >= 1 && x <= n && y >= 1 && y <= m)
return true;
return false;
}
void bfs()
{
priority_queue<pair<pll, ll>>pq;
pq.push({{1, 1}, 0});
while(!pq.empty())
{
ll x = pq.top().f.f, y = pq.top().f.s, ch = pq.top().s;
pq.pop();
ch *= -1;
dist[x][y] = ch;
if(grid[x][y] == 'X')
continue;
if(grid[x][y] == 'E')
{
ll nx = x, ny = y + 1;
if(ok(nx, ny))
{
if(ch < dist[nx][ny])
pq.push({{nx, ny}, -ch});
}
nx = x, ny = y - 1;
if(ok(nx, ny))
{
if(ch + 2 < dist[nx][ny])
pq.push({{nx, ny}, -(ch + 2)});
}
nx = x + 1, ny = y;
if(ok(nx, ny))
{
if(ch + 1 < dist[nx][ny])
pq.push({{nx, ny}, -(ch + 1)});
}
nx = x - 1, ny = y;
if(ok(nx, ny))
{
if(ch + 3 < dist[nx][ny])
pq.push({{nx, ny}, -(ch + 3)});
}
}
else if(grid[x][y] == 'S')
{
ll nx = x, ny = y + 1;
if(ok(nx, ny))
{
if(ch + 3 < dist[nx][ny])
pq.push({{nx, ny}, -(ch + 3)});
}
nx = x, ny = y - 1;
if(ok(nx, ny))
{
if(ch + 1 < dist[nx][ny])
pq.push({{nx, ny}, -(ch + 1)});
}
nx = x + 1, ny = y;
if(ok(nx, ny))
{
if(ch < dist[nx][ny])
pq.push({{nx, ny}, -ch});
}
nx = x - 1, ny = y;
if(ok(nx, ny))
{
if(ch + 2 < dist[nx][ny])
pq.push({{nx, ny}, -(ch + 2)});
}
}
else if(grid[x][y] == 'W')
{
ll nx = x, ny = y + 1;
if(ok(nx, ny))
{
if(ch + 2 < dist[nx][ny])
pq.push({{nx, ny}, -(ch + 2)});
}
nx = x, ny = y - 1;
if(ok(nx, ny))
{
if(ch < dist[nx][ny])
pq.push({{nx, ny}, -ch});
}
nx = x + 1, ny = y;
if(ok(nx, ny))
{
if(ch + 3 < dist[nx][ny])
pq.push({{nx, ny}, -(ch + 3)});
}
nx = x - 1, ny = y;
if(ok(nx, ny))
{
if(ch + 1 < dist[nx][ny])
pq.push({{nx, ny}, -(ch + 1)});
}
}
else if(grid[x][y] == 'N')
{
ll nx = x, ny = y + 1;
if(ok(nx, ny))
{
if(ch + 1 < dist[nx][ny])
pq.push({{nx, ny}, -(ch + 1)});
}
nx = x, ny = y - 1;
if(ok(nx, ny))
{
if(ch + 3 < dist[nx][ny])
pq.push({{nx, ny}, -(ch + 3)});
}
nx = x + 1, ny = y;
if(ok(nx, ny))
{
if(ch + 2 < dist[nx][ny])
pq.push({{nx, ny}, -(ch + 2)});
}
nx = x - 1, ny = y;
if(ok(nx, ny))
{
if(ch < dist[nx][ny])
pq.push({{nx, ny}, -ch});
}
}
}
}
void solve()
{
ll i, j;
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];
}
for(i = 1; i <= n; i++)
{
for(j = 1; j <= m; j++)
dist[i][j] = LLONG_MAX;
}
bfs();
cout << (dist[n][m] == LLONG_MAX ? -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();
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2392 KB |
Output is correct |
2 |
Correct |
1 ms |
2396 KB |
Output is correct |
3 |
Correct |
1 ms |
2404 KB |
Output is correct |
4 |
Correct |
1 ms |
2396 KB |
Output is correct |
5 |
Correct |
1 ms |
2396 KB |
Output is correct |
6 |
Correct |
1 ms |
2392 KB |
Output is correct |
7 |
Correct |
1 ms |
2652 KB |
Output is correct |
8 |
Correct |
1 ms |
2516 KB |
Output is correct |
9 |
Correct |
1 ms |
2480 KB |
Output is correct |
10 |
Correct |
1 ms |
2396 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2392 KB |
Output is correct |
2 |
Correct |
1 ms |
2396 KB |
Output is correct |
3 |
Correct |
1 ms |
2404 KB |
Output is correct |
4 |
Correct |
1 ms |
2396 KB |
Output is correct |
5 |
Correct |
1 ms |
2396 KB |
Output is correct |
6 |
Correct |
1 ms |
2392 KB |
Output is correct |
7 |
Correct |
1 ms |
2652 KB |
Output is correct |
8 |
Correct |
1 ms |
2516 KB |
Output is correct |
9 |
Correct |
1 ms |
2480 KB |
Output is correct |
10 |
Correct |
1 ms |
2396 KB |
Output is correct |
11 |
Correct |
1 ms |
2396 KB |
Output is correct |
12 |
Correct |
1 ms |
2396 KB |
Output is correct |
13 |
Correct |
1 ms |
2516 KB |
Output is correct |
14 |
Correct |
1 ms |
2396 KB |
Output is correct |
15 |
Correct |
1 ms |
2652 KB |
Output is correct |
16 |
Correct |
1 ms |
2396 KB |
Output is correct |
17 |
Correct |
1 ms |
2396 KB |
Output is correct |
18 |
Correct |
1 ms |
2648 KB |
Output is correct |
19 |
Correct |
1 ms |
2396 KB |
Output is correct |
20 |
Correct |
1 ms |
2396 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Correct |
1 ms |
2392 KB |
Output is correct |
3 |
Correct |
1 ms |
2396 KB |
Output is correct |
4 |
Correct |
1 ms |
2396 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Correct |
1 ms |
2396 KB |
Output is correct |
3 |
Correct |
5 ms |
2396 KB |
Output is correct |
4 |
Correct |
3 ms |
2396 KB |
Output is correct |
5 |
Correct |
14 ms |
2560 KB |
Output is correct |
6 |
Correct |
12 ms |
2556 KB |
Output is correct |
7 |
Correct |
3 ms |
2392 KB |
Output is correct |
8 |
Correct |
1 ms |
2396 KB |
Output is correct |
9 |
Correct |
1 ms |
2396 KB |
Output is correct |
10 |
Correct |
1 ms |
2396 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2392 KB |
Output is correct |
2 |
Correct |
1 ms |
2396 KB |
Output is correct |
3 |
Correct |
1 ms |
2404 KB |
Output is correct |
4 |
Correct |
1 ms |
2396 KB |
Output is correct |
5 |
Correct |
1 ms |
2396 KB |
Output is correct |
6 |
Correct |
1 ms |
2392 KB |
Output is correct |
7 |
Correct |
1 ms |
2652 KB |
Output is correct |
8 |
Correct |
1 ms |
2516 KB |
Output is correct |
9 |
Correct |
1 ms |
2480 KB |
Output is correct |
10 |
Correct |
1 ms |
2396 KB |
Output is correct |
11 |
Correct |
1 ms |
2396 KB |
Output is correct |
12 |
Correct |
1 ms |
2396 KB |
Output is correct |
13 |
Correct |
1 ms |
2516 KB |
Output is correct |
14 |
Correct |
1 ms |
2396 KB |
Output is correct |
15 |
Correct |
1 ms |
2652 KB |
Output is correct |
16 |
Correct |
1 ms |
2396 KB |
Output is correct |
17 |
Correct |
1 ms |
2396 KB |
Output is correct |
18 |
Correct |
1 ms |
2648 KB |
Output is correct |
19 |
Correct |
1 ms |
2396 KB |
Output is correct |
20 |
Correct |
1 ms |
2396 KB |
Output is correct |
21 |
Correct |
1 ms |
2396 KB |
Output is correct |
22 |
Correct |
1 ms |
2392 KB |
Output is correct |
23 |
Correct |
1 ms |
2396 KB |
Output is correct |
24 |
Correct |
1 ms |
2396 KB |
Output is correct |
25 |
Correct |
1 ms |
2396 KB |
Output is correct |
26 |
Correct |
1 ms |
2396 KB |
Output is correct |
27 |
Correct |
5 ms |
2396 KB |
Output is correct |
28 |
Correct |
3 ms |
2396 KB |
Output is correct |
29 |
Correct |
14 ms |
2560 KB |
Output is correct |
30 |
Correct |
12 ms |
2556 KB |
Output is correct |
31 |
Correct |
3 ms |
2392 KB |
Output is correct |
32 |
Correct |
1 ms |
2396 KB |
Output is correct |
33 |
Correct |
1 ms |
2396 KB |
Output is correct |
34 |
Correct |
1 ms |
2396 KB |
Output is correct |
35 |
Runtime error |
986 ms |
102400 KB |
Execution killed with signal 9 |
36 |
Halted |
0 ms |
0 KB |
- |