Submission #737075

#TimeUsernameProblemLanguageResultExecution timeMemory
737075nguyentunglamMaze (JOI23_ho_t3)C++17
100 / 100
1436 ms381724 KiB
#include<bits/stdc++.h>
#define fi first
#define se second
#define endl "\n"
#define ii pair<int, int>
using namespace std;
vector<vector<int> > a, f, mark, row, col;
int mx[4] = {0, 0, -1, 1};
int my[4] = {1, -1, 0, 0};
int findr(int i, int j) {
    return !mark[i][j] ? i : row[i][j] = findr(row[i][j], j);
}
int findc(int i, int j) {
    return !mark[i][j] ? j : col[i][j] = findc(i, col[i][j]);
}

int main() {
    #define task ""
    cin.tie(0) -> sync_with_stdio(0);
    if (fopen ("task.inp", "r")) {
        freopen ("task.inp", "r", stdin);
//        freopen ("task.out", "w", stdout);
    }
    if (fopen (task".inp", "r")) {
        freopen (task".inp", "r", stdin);
        freopen (task".out", "w", stdout);
    }
    int r, c, n; cin >> r >> c >> n;
    a = vector<vector<int> > (r + 2, vector<int> (c + 2));
    f = vector<vector<int> > (r + 2, vector<int> (c + 2));
    mark = vector<vector<int> > (r + 2, vector<int> (c + 2));
    row = vector<vector<int> > (r + 2, vector<int> (c + 2));
    col = vector<vector<int> > (r + 2, vector<int> (c + 2));


    int sr, sc, gr, gc; cin >> sr >> sc >> gr >> gc;
    for(int i = 1; i <= r; i++) for(int j = 1; j <= c; j++) {
        char c; cin >> c;
        if (c == '.') a[i][j] = 0;
        else a[i][j] = 1;
        f[i][j] = 1e9;
        row[i][j] = i + 1;
        col[i][j] = j + 1;
    }

    deque<tuple<int, int, int> > q;
    f[sr][sc] = 0;
    q.push_back({0, sr, sc});
    while (!q.empty()) {
        int cost, x, y; tie(cost, x, y) = q.front(); q.pop_front();
        if (cost != f[x][y]) continue;
        int d1 = abs(gr - x), d2 = abs(gc - y);
        if (d1 > d2) swap(d1, d2);
        if (d1 < n && d2 <= n && f[gr][gc] > f[x][y] + 1) {
            f[gr][gc] = f[x][y] + 1;
            q.push_back({f[gr][gc], gr, gc});
        }
        for(int k = 0; k < 4; k++) {
            int xx = x + mx[k], yy = y + my[k];
            if (xx < 1 || xx > r || yy < 1 || yy > c || a[xx][yy]) continue;
            if (f[xx][yy] > f[x][y]) {
                f[xx][yy] = f[x][y];
                q.push_front({f[xx][yy], xx, yy});
            }
        }

        int i, j;
        if (x > n) {
            i = x - n;
            j = max(1, y - n + 1);
            for(j = findc(i, j); j <= min(c, y + n - 1); j = findc(i, j)) {
                if (f[i][j] > f[x][y] + 1) {
                    f[i][j] = f[x][y] + 1;
                    q.push_back({f[i][j], i, j});
                }
                mark[i][j] = 1;
            }
        }
        if (x + n <= r) {
            i = x + n;
            j = max(1, y - n + 1);
            for(j = findc(i, j); j <= min(c, y + n - 1); j = findc(i, j)) {
                if (f[i][j] > f[x][y] + 1) {
                    f[i][j] = f[x][y] + 1;
                    q.push_back({f[i][j], i, j});
                }
                mark[i][j] = 1;
            }
        }

        if (y > n) {
            j = y - n;
            i = max(1, x - n + 1);
            for(i = findr(i, j); i <= min(r, x + n - 1); i = findr(i, j)) {
                if (f[i][j] > f[x][y] + 1) {
                    f[i][j] = f[x][y] + 1;
                    q.push_back({f[i][j], i, j});
                }
                mark[i][j] = 1;
            }
        }
        if (y + n <= c) {
            j = y + n;
            i = max(1, x - n + 1);
            for(i = findr(i, j); i <= min(r, x + n - 1); i = findr(i, j)) {
                if (f[i][j] > f[x][y] + 1) {
                    f[i][j] = f[x][y] + 1;
                    q.push_back({f[i][j], i, j});
                }
                mark[i][j] = 1;
            }
        }
    }
    cout << f[gr][gc];
}

Compilation message (stderr)

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