Submission #1218194

#TimeUsernameProblemLanguageResultExecution timeMemory
1218194InvMODMaze (JOI23_ho_t3)C++17
Compilation error
0 ms0 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);}

const int N = 2e5 + 5;
const int MOD = 1e9 + 7;
const ll INF = numeric_limits<ll>::max() / 8;


struct Node{
    int x, y, dist, relax;

    Node(int x = 0, int y = 0, int dist = 0, int relax = 0): x(x), y(y), dist(dist), relax(relax) {}

    bool operator < (const Node& q) const{
        return pi(dist, -relax) > pi(q.dist, -q.relax);
    }
};

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

    pi start; int ox, oy;
    cin >> ox >> oy; start = pi(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);
        }
    }

    // 0: vertical, 1: horizontal
    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)));

    priority_queue<Node> pq; pq.emplace(start.fi, start.se, 1, 0); dist[start.fi][start.se] = 1;

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

    while(sz(pq)){
        Node eq = pq.top(); pq.pop();

        int x = eq.x, y = eq.y;

//        cout << x <<" " << y <<" " << dist[x][y] <<" " << eq.relax << el;
        if(!can[0][x][y] || !can[1][x][y]){
            // white node or can't and need to increase
            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]){ // black
                        if((min(can[0][x][y] - abs(dx[i]), can[1][x][y] - abs(dy[i])) >= 0)){
                            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]);
                            pq.emplace(nx, ny, dist[nx][ny], min(can[0][nx][ny], can[1][nx][ny]));
                            // at the edge of expand
                        }
                        else{
                            dist[nx][ny] = dist[x][y] + 1;

                            can[0][nx][ny] = can[1][nx][ny] = jump - 1;
                            pq.emplace(nx, ny, dist[nx][ny], jump - 1);
                        }
                    }
                    else{ // white
                        dist[nx][ny] = dist[x][y];
                        pq.emplace(nx, ny, dist[nx][ny], 0);
                    }
                }
            }
        }
        else{
            // changed to white and now we will expand changing
            FOR(i, 0, 3){
                int nx = x + dx[i];
                int ny = y + dy[i];
                
                if((min(can[0][x][y] - abs(dx[i]), can[1][x][y] - abs(dy[i])) < 0) continue;

                if(init(nx, ny) && !dist[nx][ny]){
                    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]);
                    pq.emplace(nx, ny, dist[nx][ny], min(can[0][nx][ny], can[1][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 'void Main()':
Main.cpp:121:83: error: expected ';' before 'continue'
  121 |                 if((min(can[0][x][y] - abs(dx[i]), can[1][x][y] - abs(dy[i])) < 0) continue;
      |                                                                                   ^~~~~~~~~
      |                                                                                   ;
Main.cpp:123:17: error: expected primary-expression before 'if'
  123 |                 if(init(nx, ny) && !dist[nx][ny]){
      |                 ^~
Main.cpp:121:93: error: expected ')' before 'if'
  121 |                 if((min(can[0][x][y] - abs(dx[i]), can[1][x][y] - abs(dy[i])) < 0) continue;
      |                   ~                                                                         ^
      |                                                                                             )
  122 | 
  123 |                 if(init(nx, ny) && !dist[nx][ny]){
      |                 ~~                                                                           
Main.cpp:130:13: error: expected primary-expression before '}' token
  130 |             }
      |             ^
Main.cpp: In function 'int32_t main()':
Main.cpp:144:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  144 |         freopen(name".INP", "r", stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
Main.cpp:145:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  145 |         freopen(name".OUT", "w", stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~