Submission #1042314

#TimeUsernameProblemLanguageResultExecution timeMemory
1042314jnjwnwnwAwesome Arrowland Adventure (eJOI19_adventure)C++17
100 / 100
230 ms45036 KiB
#include <iostream>
#include <algorithm>
#include <queue>
#include <set>

using namespace std;

#define ll long long
#define fff(i,a,b) for(ll i = a; i < b; i++)


ll n, m;
vector<ll> adj[501][501];
ll dir[501][501];
ll table[257];
priority_queue<pair<ll, pair<ll, ll>>> q;
set<pair<ll, ll>> seen;
pair<ll, ll> adjPos[]{{-1, 0}, {0, 1}, {1, 0}, {0, -1}};

void setIO(string s) {
    freopen((s + ".in").c_str(), "r", stdin);
    freopen((s + ".out").c_str(), "w", stdout);
}

int main(){
    // setIO("testing");
    // opposite of input, for my own clarity
    cin >> n >> m;
    // NESW, so if i have W and want to know how many i need to do to get N it should be 
    table['N'] = 0;
    table['E'] = 1;
    table['S'] = 2;
    table['W'] = 3;
    table['X'] = -1;
    fff(i, 0, n){
        fff(j, 0, m){
            char c; cin >> c;
            dir[i][j] = table[c];
        }
    }

    q.push({0, {0, 0}});

    while (q.size()){
        auto [d, pos] = q.top(); q.pop();
        auto [y, x] = pos;
        if (seen.find({y, x}) != seen.end()) continue;
        // process this spot. 
        seen.insert({y, x});
        if (y == n-1 && x == m-1){
            // at the end, output dist
            cout << -d << endl;
            return 0;
        }
        // now, move to nearby, if can
        if (dir[y][x] == -1) continue;
        fff(i, 0, 4){
            auto [dy, dx] = adjPos[i];
            ll ny = y+dy, nx = x+dx;
            if (ny < 0 || ny >= n || nx < 0 || nx >= m) continue;
            auto cost = -((i - dir[y][x]+4)%4);
            q.push({cost+d, {ny, nx}});
        }
    }
    cout << -1 << endl;
}

Compilation message (stderr)

adventure.cpp: In function 'int main()':
adventure.cpp:38:31: warning: array subscript has type 'char' [-Wchar-subscripts]
   38 |             dir[i][j] = table[c];
      |                               ^
adventure.cpp: In function 'void setIO(std::string)':
adventure.cpp:21:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   21 |     freopen((s + ".in").c_str(), "r", stdin);
      |     ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
adventure.cpp:22:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   22 |     freopen((s + ".out").c_str(), "w", stdout);
      |     ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...