Submission #964706

# Submission time Handle Problem Language Result Execution time Memory
964706 2024-04-17T11:35:22 Z rahidilbayramli Awesome Arrowland Adventure (eJOI19_adventure) C++17
34 / 100
1 ms 4696 KB
#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], vis[sz+5][sz+5], n, m;
bool ok(ll x, ll y)
{
    if(x >= 1 && x <= n && y >= 1 && y <= m && vis[x][y] < 10)
        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;
        vis[x][y]++;
        if(grid[x][y] != 'X'){
            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});
                        dist[nx][ny] = ch;
                    }
                }
                nx = x, ny = y - 1;
                if(ok(nx, ny))
                {
                    if(ch + 2 < dist[nx][ny]){
                        pq.push({{nx, ny}, ch + 2});
                        dist[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});
                        dist[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});
                        dist[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});
                        dist[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});
                        dist[nx][ny] = ch + 1;
                    }
                }
                nx = x + 1, ny = y;
                if(ok(nx, ny))
                {
                    if(ch < dist[nx][ny]){
                        pq.push({{nx, ny}, ch});
                        dist[nx][ny] = ch;
                    }
                }
                nx = x - 1, ny = y;
                if(ok(nx, ny))
                {
                    if(ch + 2 < dist[nx][ny]){
                        pq.push({{nx, ny}, ch + 2});
                        dist[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});
                        dist[nx][ny] = ch + 2;
                    }
                }
                nx = x, ny = y - 1;
                if(ok(nx, ny))
                {
                    if(ch < dist[nx][ny]){
                        pq.push({{nx, ny}, ch});
                        dist[nx][ny] = ch;
                    }
                }
                nx = x + 1, ny = y;
                if(ok(nx, ny))
                {
                    if(ch + 3 < dist[nx][ny]){
                        pq.push({{nx, ny}, ch + 3});
                        dist[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});
                        dist[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});
                        dist[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});
                        dist[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});
                        dist[nx][ny] = ch + 2;
                    }
                }
                nx = x - 1, ny = y;
                if(ok(nx, ny))
                {
                    if(ch < dist[nx][ny]){
                        pq.push({{nx, ny}, ch});
                        dist[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();
    }
}
# Verdict Execution time Memory 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
# Verdict Execution time Memory 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
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 4696 KB Output is correct
17 Correct 1 ms 4440 KB Output is correct
18 Correct 1 ms 4444 KB Output is correct
19 Correct 1 ms 4444 KB Output is correct
20 Correct 1 ms 4444 KB Output is correct
# Verdict Execution time Memory 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
# Verdict Execution time Memory Grader output
1 Correct 1 ms 4444 KB Output is correct
2 Incorrect 1 ms 4444 KB Output isn't correct
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory 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
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 4696 KB Output is correct
17 Correct 1 ms 4440 KB Output is correct
18 Correct 1 ms 4444 KB Output is correct
19 Correct 1 ms 4444 KB Output is correct
20 Correct 1 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 4444 KB Output is correct
26 Incorrect 1 ms 4444 KB Output isn't correct
27 Halted 0 ms 0 KB -