Submission #1023093

#TimeUsernameProblemLanguageResultExecution timeMemory
1023093Arpi2007Awesome Arrowland Adventure (eJOI19_adventure)C++14
50 / 100
57 ms102400 KiB
#include <iostream>
#include <queue>
#include <fstream>
#include <cmath>
#include <algorithm>
#include <string>
#include <vector>
#include <set>
#include <map>
#include <utility>
#include <stack>
#include <math.h>
#include <climits>
#include <iomanip>
#include <unordered_map>
using namespace std;
 
#define ll long long
#define ld long double
#define ull unsigned long long
#define ub upper_bound
#define lb lower_bound 
#define ff first
#define ss second
#define mpr make_pair
#define vi vector<int>
#define vll vector<ll>
#define pii pair<int,int>
#define vpi vector<pii>
#define pb push_back
#define pob pop_back
#define mii map<int,int>
#define vpl vector<pair<ll, ll>>
#define pll pair<ll,ll>
#define all(v) v.begin(),v.end()
#define sz(x) x.size()
#define clr(x) x.clear()
#define yes cout << "YES\n"
#define no cout << "NO\n"
//ifstream cin("bcount.in");
//ofstream cout("bcount.out");
 
void fastio() {
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
}
 
void setprecision(int x) {
 
    cout.setf(ios::fixed | ios::showpoint);
    cout.precision(x);
}
 
/*void setIO(string name = "") {
    if ((int)name.size() > 0) {
        freopen((name + ".in").c_str(), "r", stdin);
        freopen((name + ".out").c_str(), "w", stdout);
    }
}*/
 
ll gcd(ll a, ll b) {
    if (a > b) {
        swap(a, b);
    }
    if (a == 0) {
        return b;
    }
    if (a == b) {
        return a;
    }
    return gcd(b - a, a);
}
 
ll num(ll n) {
    ll ans = 0;
    while (n != 0) {
        ans++;
        n /= 10;
    }
    return ans;
}
 
ll lcm(ll a, ll b) {
    return (a * b) / gcd(a, b);
}
 
ll MOD = 1e9 + 7;
ll MOD2 = 998244353;
 
ll factorial(ll n) {
    ll ans = 1;
    for (int i = 1; i <= n; i++) {
        ans *= i;
        ans %= MOD2;
    }
    return ans;
}
ll fact2(ll n, ll k) {
    ll ans = 1;
    for (int i = 1; i <= n; i++) {
        if (i == k) {
            ans *= (k - 1);
        }
        else {
            ans *= i;
        }
        ans %= MOD2;
    }
    return ans;
}
 
bool isPrime(int n)
{
    if (n <= 1) {
        return false;
    }
    if (n == 2) {
        return true;
    }
    for (int i = 2; i * i <= n; i++) {
        if (n % i == 0)
            return false;
    }
    return true;
}

ll bpow_mod(ll a, ll b, ll mod)
{
  ll ans = 1;
  while (b) {
    if ((b & 1) == 1) {
      ans *= a;
     ans %= mod;
    }
    b >>= 1;
    a *= a;
    a %= mod;
  }
  return ans;
}

 
bool sortbysec(const pair<int, int>& a,
    const pair<int, int>& b)
{
    return (a.second < b.second);
}
 
bool ask(vector<int> vertices) {
    cout << "? " << vertices.size();
    for(int x: vertices) {
        cout << " " << x;
    }
    cout << endl << flush;
    string answer;
    cin >> answer;
    return (answer == "YES");
}

void precalc(){
    return;
}

const int N = 4e6 + 10;
char c[502][502];
vpi g[N];
int m, n;
int dist[N];
int par[N];

bool check(int i, int j){
    if(i <= m && i >= 1 && j <= n && j >= 1){
        return true;
    }
    return false;
}

int f(int i, int j){
    return (i - 1) * n + j;
}

void solve() {
     cin >> m >> n;
    for(int i = 1; i <= m; ++i){
        for(int j = 1; j <= n; ++j){
            cin >> c[i][j];
        }
    }
    for(int i = 1; i <= m; ++i){
        for(int j = 1; j <= n; ++j){
            dist[f(i, j)] = 1e9;
            if(c[i][j] == 'E'){
                if(check(i, j + 1)){
                    g[f(i, j)].pb({f(i, j + 1), 0});
                }
                if(check(i + 1, j)){
                    g[f(i, j)].pb({f(i + 1, j), 1});
                }
                if(check(i, j - 1)){
                    g[f(i, j)].pb({f(i, j - 1), 2});
                }
                if(check(i - 1, j)){
                    g[f(i, j)].pb({f(i - 1, j), 3});
                }
            }
            if(c[i][j] == 'S'){
                if(check(i, j + 1)){
                    g[f(i, j)].pb({f(i, j + 1), 3});
                }
                if(check(i + 1, j)){
                    g[f(i, j)].pb({f(i + 1, j), 0});
                }
                if(check(i, j - 1)){
                    g[f(i, j)].pb({f(i, j - 1), 1});
                }
                if(check(i - 1, j)){
                    g[f(i, j)].pb({f(i - 1, j), 2});
                }
            }
            if(c[i][j] == 'W'){
                if(check(i, j + 1)){
                    g[f(i, j)].pb({f(i, j + 1), 2});
                }
                if(check(i + 1, j)){
                    g[f(i, j)].pb({f(i + 1, j), 3});
                }
                if(check(i, j - 1)){
                    g[f(i, j)].pb({f(i, j - 1), 0});
                }
                if(check(i - 1, j)){
                    g[f(i, j)].pb({f(i - 1, j), 1});
                }
            }
            if(c[i][j] == 'N'){
                if(check(i, j + 1)){
                    g[f(i, j)].pb({f(i, j + 1), 1});
                }
                if(check(i + 1, j)){
                    g[f(i, j)].pb({f(i + 1, j), 2});
                }
                if(check(i, j - 1)){
                    g[f(i, j)].pb({f(i, j - 1), 3});
                }
                if(check(i - 1, j)){
                    g[f(i, j)].pb({f(i - 1, j), 0});
                }
            }
           // cout << sz(g[f(i, j)]) << ' ' << i << ' ' << j << endl;
        }
    }
   // cout << sz(g[5]) << endl;
    priority_queue<pii>pq;
    pq.push({0, 1});
    dist[1] = 0;
    while(!pq.empty()){
        int k = -pq.top().ff;
        int u = pq.top().ss;
        pq.pop();
        if(k > dist[u]){
            continue;
        }
        for(auto i : g[u]){
            if(dist[i.ff] > i.ss + dist[u]){
                dist[i.ff] = i.ss + dist[u];
                pq.push({-dist[i.ff], i.ff});
            }
        }
    }
    int v = n * m;
    if(dist[n * m] == 1e9){
        cout << -1 << endl;return;
    }
    cout << dist[n * m] << endl;
}


void cases() {
    int t;
    cin >> t;
    while (t--) {
        solve();
    }  
}
 
int main() {
    fastio();
    precalc();
    //cases();
    solve();
    return 0;
}

Compilation message (stderr)

adventure.cpp: In function 'void solve()':
adventure.cpp:270:9: warning: unused variable 'v' [-Wunused-variable]
  270 |     int v = n * m;
      |         ^
#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...