#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();
dist[x][y] = ch;
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++)
{
for(j = 1; j <= m; j++)
cin >> grid[i][j];
}
for(i = 1; i <= n; i++)
{
for(j = 1; j <= m; j++)
dist[i][j] = LLONG_MAX;
}
bfs();
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();
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
2392 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
2392 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2508 KB |
Output is correct |
2 |
Correct |
1 ms |
2396 KB |
Output is correct |
3 |
Incorrect |
1 ms |
2396 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
2392 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
2392 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |