Submission #1218231

#TimeUsernameProblemLanguageResultExecution timeMemory
1218231InvMODMaze (JOI23_ho_t3)C++17
100 / 100
396 ms82520 KiB
#include<bits/stdc++.h>

using namespace std;

#define fi first
#define se second
#define pb push_back
#define eb emplace_back

#define vi vector<int>
#define pi pair<int,int>
#define sz(v) (int)(v).size()
#define all(v) (v).begin(), (v).end()
#define compact(v) (v).erase(unique(all(v)), (v).end())

template<class T> using upq = priority_queue<T, vector<T>, greater<T>>;
template<class T> int lwrbound(const vector<T>& a, const T& b, const int s = 0){return int(lower_bound(s + all(a), b) - a.begin());}
template<class T> int uprbound(const vector<T>& a, const T& b, const int s = 0){return int(upper_bound(s + all(a), b) - a.begin());}

#define FOR(i, a, b) for(int i = (a); i <= (b); i++)
#define ROF(i, a, b) for(int i = (a); i >= (b); i--)
#define sumof(x) accumulate(all(x), 0ll)
#define dbg(x) "[" << #x " = " << (x) << "]"
#define el "\n"

using ll = long long;
using ld = long double;

template<class T> bool ckmx(T& a, const T b){return (a < b ? a = b, true : false);}
template<class T> bool ckmn(T& a, const T b){return (a > b ? a = b, true : false);}




void Main()
{
    int R,C, jsize; cin >> R >> C >> jsize;

    int sR, sC, eR, eC;
    cin >> sR >> sC >> eR >> eC;

    vector<vector<char>> a(R + 1, vector<char>(C + 1));
    FOR(i, 1, R) FOR(j, 1, C) cin >> a[i][j];

    const int dx[4] = {0, 1, -1, 0};
    const int dy[4] = {1, 0, 0, -1};
    auto init = [&](int x, int y) -> bool{
        return (0 < x && x <= R) && (0 < y && y <= C);
    };

    // white -> expand with 0 distance
    // black -> expand until we can't and will increase the distance
    vector<vi> dist(R + 1, vi(C + 1, -1));

    queue<pi> expand; queue<pair<pi, pi>> operation;
    expand.emplace(sR, sC); dist[sR][sC] = 0;

    while(sz(expand) + sz(operation)){
        // find all nodes that are available
        while(sz(expand)){
            pi e = expand.front(); expand.pop();

            int x = e.fi, y = e.se;
            FOR(i, 0, 3){
                int u = x + dx[i], v = y + dy[i];
                if(!init(u, v) || dist[u][v] != -1) continue;
                if(a[u][v] == '.'){
                    dist[u][v] = dist[x][y];
                    expand.push(pi(u, v));
                }
                else{
                    dist[u][v] = dist[x][y] + 1;
                    operation.push(make_pair(pi(u, v), pi(jsize - 1, jsize - 1)));
                }
            }
        }

        while(sz(operation)){
            pair<pi, pi> e = operation.front(); operation.pop();

            int x = e.fi.fi, y = e.fi.se, ver = e.se.fi, hor = e.se.se;

            if(ver == 0 || hor == 0)
                expand.push(pi(x, y));

            FOR(i, 0, 3){
                int u = x + dx[i], v = y + dy[i];
                if(!init(u, v) || dist[u][v] != -1) continue;
                int nVer = ver - (dy[i] != 0), nHor = hor - (dx[i] != 0);
                if(nVer < 0 || nHor < 0) continue;
                dist[u][v] = dist[x][y];
                operation.push(make_pair(pi(u, v), pi(nVer, nHor)));
            }
        }
    }

    cout << dist[eR][eC] << el;
}

int32_t main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0); cout.tie(0);

    #define name "InvMOD"
    if(fopen(name".INP", "r")){
        freopen(name".INP", "r", stdin);
        freopen(name".OUT", "w", stdout);
    }

    int t = 1; while(t--) Main();
    return 0;
}

Compilation message (stderr)

Main.cpp: In function 'int32_t main()':
Main.cpp:107:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  107 |         freopen(name".INP", "r", stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
Main.cpp:108:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  108 |         freopen(name".OUT", "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...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...