Submission #1124420

#TimeUsernameProblemLanguageResultExecution timeMemory
1124420Neco_arcMaze (JOI23_ho_t3)C++20
100 / 100
511 ms164704 KiB
#include <bits/stdc++.h>

#define ll long long
#define name "Maze"
#define fi(i, a, b)  for(int i = a; i <= b; ++i)
#define fid(i, a, b) for(int i = a; i >= b; --i)
#define maxn (int) (6e6 + 7)

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

using namespace std;

mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
ll GetRandom(ll l, ll r) {
    return uniform_int_distribution<ll> (l, r)(rng);
}

int n, m, c;
int stx, sty, fnx, fny;
vector<vector<int>> dist, a;

queue<array<int, 4>> q_good, q_bad;

inline bool inside(int x, int y) {
    return 1 <= x && x <= n && 1 <= y && y <= m;
}

int BFS() {

    dist[stx][sty] = a[stx][sty];

    if(!a[stx][sty]) q_good.push({stx, sty, 0, 0});
    else q_bad.push({stx, sty, c - 1, c - 1});

    while(q_good.size() || q_bad.size()) { /// bad go first to simulate the dijkstra

        while(q_bad.size()) {
            auto [x, y, sx, sy] = q_bad.front(); q_bad.pop();

            if(sx == 0 || sy == 0) q_good.push({x, y, 0, 0});

            fi(t, 0, 3) {
                int u = x + dx[t], v = y + dy[t];
                int rx = sx - abs(dx[t]), ry = sy - abs(dy[t]);

                if(rx >= 0 && ry >= 0 && inside(u, v) && dist[u][v] == -1) {
                    dist[u][v] = dist[x][y];
                    q_bad.push({u, v, rx, ry});
                }
            }
        }

        while(q_good.size()) {
            auto [x, y, sx, sy] = q_good.front(); q_good.pop();

            fi(t, 0, 3) {
                int u = x + dx[t], v = y + dy[t];
                if(!inside(u, v) || dist[u][v] != -1) continue;

                dist[u][v] = dist[x][y] + a[u][v];

                if(a[u][v]) q_bad.push({u, v, c - 1, c - 1});
                else q_good.push({u, v, 0, 0});
            }
        }
    }

    return dist[fnx][fny];
}

void solve() {

    cin >> n >> m >> c;
    cin >> stx >> sty >> fnx >> fny;

    a.resize(n + 2, vector<int>(m + 2));
    dist.resize(n + 2, vector<int>(m + 2, -1));

    fi(i, 1, n) fi(j, 1, m) {
        char x; cin >> x;
        a[i][j] = x == '#';
    }

    cout << BFS();

}

int main() {

    ios_base::sync_with_stdio(false);
    cin.tie(0), cout.tie(0);

    if(fopen(name".inp", "r")) {
        freopen(name".inp", "r", stdin);
        freopen(name".out", "w", stdout);
    }

    solve();

}

Compilation message (stderr)

Main.cpp: In function 'int main()':
Main.cpp:95:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   95 |         freopen(name".inp", "r", stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
Main.cpp:96:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   96 |         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...