Submission #1218212

#TimeUsernameProblemLanguageResultExecution timeMemory
1218212InvMODMaze (JOI23_ho_t3)C++17
62 / 100
123 ms65020 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, jump; cin >> R >> C >> jump;

    pi start;
    cin >> start.fi >> start.se;

    int ox, oy;
    cin >> ox >> oy;

    vector<vi> a(R + 1, vi(C + 1));
    FOR(i, 1, R) FOR(j, 1, C){
        char c; cin >> c;

        a[i][j] = (c == '#' ? 1 : 0);
    }

    const int dx[4] = {0, 1, 0, -1};
    const int dy[4] = {1, 0, -1, 0};

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

    vector<vi> dist(R + 1, vi(C + 1, 0));
    vector<vector<vi>> can(2, vector<vi>(R + 1, vi(C + 1, 0)));

    // white -> expand with 0 distance
    // black -> expand until we can't and will increase the distance

    queue<pi> expand; expand.push(start); dist[start.fi][start.se] = 1;
    while(sz(expand)){
        // find all nodes that are available

        queue<pi> operation;
        while(sz(expand)){
            pi e = expand.front(); expand.pop();

            int x = e.fi, y = e.se;
            FOR(i, 0, 3){
                int nx = x + dx[i];
                int ny = y + dy[i];

                if(init(nx, ny) && !dist[nx][ny]){
                    if(a[nx][ny]){
                        dist[nx][ny] = dist[x][y] + 1;
                        can[0][nx][ny] = can[1][nx][ny] = jump - 1;

                        operation.push(pi(nx, ny));
                    }
                    else{
                        dist[nx][ny] = dist[x][y];
                        expand.push(pi(nx, ny));
                    }
                }
            }
        }

        while(sz(operation)){
            pi e = operation.front(); operation.pop();

            int x = e.fi, y = e.se;
            if(!can[0][x][y] || !can[1][x][y]) expand.emplace(x, y);

            FOR(i, 0, 3){
                int nx = x + dx[i];
                int ny = y + dy[i];

                if(init(nx, ny) && !dist[nx][ny]){
                    if(can[0][x][y] - abs(dx[i]) < 0 || can[1][x][y] - abs(dy[i]) < 0)
                        continue;

                    dist[nx][ny] = dist[x][y];

                    can[0][nx][ny] = can[0][x][y] - abs(dx[i]);
                    can[1][nx][ny] = can[1][x][y] - abs(dy[i]);

                    operation.emplace(nx, ny);
                }
            }
        }
    }

    cout << dist[ox][oy] - 1 << 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:128:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  128 |         freopen(name".INP", "r", stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
Main.cpp:129:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  129 |         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...