QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#128772#6739. Teleportckiseki#AC ✓679ms192396kbC++203.0kb2023-07-21 15:42:102023-07-21 15:57:23

Judging History

你现在查看的是最新测评结果

  • [2023-08-10 23:21:45]
  • System Update: QOJ starts to keep a history of the judgings of all the submissions.
  • [2023-07-21 15:57:23]
  • 评测
  • 测评结果:AC
  • 用时:679ms
  • 内存:192396kb
  • [2023-07-21 15:42:10]
  • 提交

answer

#include <bits/stdc++.h>
using namespace std;

#define all(x) begin(x), end(x)
#ifdef CKISEKI
#define safe cerr << __PRETTY_FUNCTION__ << " line " << __LINE__ << " safe\n"
#define debug(a...) debug_(#a, a)
#define orange(a...) orange_(#a, a)
template <typename ...T>
void debug_(const char *s, T ...a) {
    cerr << "\e[1;32m(" << s << ") = (";
    int cnt = sizeof...(T);
    (..., (cerr << a << (--cnt ? ", " : ")\e[0m\n")));
}
template <typename I>
void orange_(const char *s, I L, I R) {
    cerr << "\e[1;32m[ " << s << " ] = [";
    while (L != R) cerr << " " << *L++;
    cerr << " ]\e[0m\n";
}
#else
#define safe ((void)0)
#define debug(...) safe
#define orange(...) safe
#endif

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

namespace dsu {
const int maxn = 5005;
int pa[maxn * maxn], mx[maxn * maxn], rk[maxn * maxn];
void init(int n) {
    for (int i = 0; i < n; i++)
        pa[i] = i, mx[i] = i, rk[i] = 0;
}

int anc(int x) {
    return x==pa[x] ? x : pa[x]=anc(pa[x]);
}
int getMax(int x) {
    return mx[anc(x)];
}
void join(int x, int y) {
    x = anc(x);
    y = anc(y);
    if (x == y) return;
    if (rk[x] < rk[y]) swap(x, y);
    pa[y] = x;
    if (rk[x] == rk[y]) ++rk[x];
    mx[x] = max(mx[x], mx[y]);
}

}


int main() {
    cin.tie(nullptr)->sync_with_stdio(false);
    int n, k;
    cin >> n >> k;

    vector<string> g(n);
    for (int i = 0; i < n; i++) {
        cin >> g[i];
    }

    dsu::init(n * n);

    const auto ok = [n](int x, int y) { return x >= 0 && x < n && y >= 0 && y < n; };
    const auto ID = [n](int x, int y) {
        return x * n + y;
    };


    vector<vector<int>> dis(n, vector<int>(n, -1));
    vector<vector<int>> dead(n, vector<int>(n, 0));
    queue<int> que;
    dis[0][0] = 0;
    que.emplace(ID(0, 0));
    // kill(0, 0);

    const auto aug = [&](int X, int Y, int lim, int D) {
        for (int c = 0; c <= lim; c++) {
            if (!ok(X + c, Y + c)) break;
            if (c > 0) {
                dsu::join(ID(X + c - 1, Y + c - 1), ID(X + c, Y + c));
            }
            if (dead[X + c][Y + c]) {
                int m = dsu::getMax(ID(X + c, Y + c));
                int mX = m / n, mY = m % n;
                assert (mX - mY == X - Y);
                c = mX - X;
            } else {
                dead[X + c][Y + c] = true;
                if (g[X + c][Y + c] != '*' && dis[X + c][Y + c] == -1) {
                    dis[X + c][Y + c] = D + 1;
                    que.emplace(ID(X + c, Y + c));
                }
            }
        }
    };


    while (!que.empty()) {
        int i = que.front(); que.pop();
        int x = i / n, y = i % n;

        for (int t = 0; t < 4; t++) {
            int nx = x + dx[t];
            int ny = y + dy[t];
            if (ok(nx, ny) && g[nx][ny] != '*' && dis[nx][ny] == -1) {
                dis[nx][ny] = dis[x][y] + 1;
                que.emplace(ID(nx, ny));
                // kill(nx, ny);
            }
        }

        aug(x, y, k / 2, dis[x][y]);
        aug(y + 1, x, (k - 1) / 2, dis[x][y]);
    }


    cout << dis[n-1][n-1] << '\n';

    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 2ms
memory: 7624kb

input:

3 2
.*.
.*.
...

output:

3

result:

ok 1 number(s): "3"

Test #2:

score: 0
Accepted
time: 1ms
memory: 7640kb

input:

3 3
.*.
.*.
...

output:

2

result:

ok 1 number(s): "2"

Test #3:

score: 0
Accepted
time: 58ms
memory: 27784kb

input:

961 4
...*.*..*.....*.*..*..*..*.*.*.*.....*.....*....*..*...*....*.........*....*....*...*......*..*..*...*..*...*.....*...*...*.*.*.........**..**.......*.......*...*...*.*.*........*....*..*..*...*.....*.*......**.**..**...*..*.**.....*....*.*.*..*..*..*.*..*.*..*......*..*..*.*......*...*.*...*....

output:

540

result:

ok 1 number(s): "540"

Test #4:

score: 0
Accepted
time: 60ms
memory: 27364kb

input:

975 434
.*......*...*....*......*..*...*...**....*....*.......*...*.....*..*..*.*.*..*.*..*..*.*....*.*.*..*...*.*.....*......*.*...*......*..*..*......**..**.......*...*.*..*..*.*.**....*.*.*.....*....**.*..*..**.*..*.....*.*......*..*...*..*...*....**...*....*..*.*.*...........*..*.**.*.*..*.*..**...

output:

5

result:

ok 1 number(s): "5"

Test #5:

score: 0
Accepted
time: 59ms
memory: 27692kb

input:

966 19
..*.*.*........*.*..*.**..*....*..*..*....*.**..*.*.*..*.*.*..**....*.*.*....*.....*...........*.*..*.....*..*...*....*.*...*.*...*....*...*...*.*.*....*.*....**.*.......*....*.*.*...*..*..*..*.*...*...*...*..*..*..........*.*....*.*......*...*..**....*.*....**.....*..*..*..*.*....*...*..*.*....

output:

104

result:

ok 1 number(s): "104"

Test #6:

score: 0
Accepted
time: 56ms
memory: 28788kb

input:

973 199
..**.*...*.*...*.*.*.*.**..*.*.*..*......*.....*.*.*..*...**.....*.*..*.*..*...*....*..*...*....*.*...*.*......*..*.*.*.*......**......*.*.*.*.*.*...*...*...*....*......*.*.*.*..*..*..*...*..*.*....*....*.*...*..*..*.*..**.**..*.**....*.*...*..**..**...*......*.....*.....*..*.*.......*.*.*.....

output:

10

result:

ok 1 number(s): "10"

Test #7:

score: 0
Accepted
time: 59ms
memory: 28344kb

input:

984 95
.....*.*...*....*..*....*.*....**.**......*....*.*.*.......*.....*.*..*....*..*....*.*.....*..*.......*.*......*.*....*.....*......*...*....*....*......*....*.*...*........*......*.*....*.*...*....*..**....*.*.*.**....*..*.*..*..*.*......*..*......*.*......*...*......*.......*...*....*...**.....

output:

21

result:

ok 1 number(s): "21"

Test #8:

score: 0
Accepted
time: 526ms
memory: 192396kb

input:

2996 2
..*..*.....*..*.....*....**..*...........*..*........*..*..*.*..*..*.*.*.**.*.....**.*.......*.......*..*....*.....*..*.*.*....**.....*..**.....*.....*.......*.*.*.*....*...*..*.**......**..*.*...**..*..*......*..*.*...*......*.*.*.*...**.**..*.*........*.*..*...**......*.*..*.*..*....*.*.*.*...

output:

3354

result:

ok 1 number(s): "3354"

Test #9:

score: 0
Accepted
time: 624ms
memory: 181264kb

input:

2905 1023
.........*..*.**.....*...*.*....*.*.**.*..*...*..*....*...*.**.*.*.**..*......*...*.**..*...*..*.........*.........*.*.....**.*.*........*.....*.*....*..*.*.....*..*..*..*.*..*....**...*.*..*....*......*.........*.*.*.*......**.**.**..*.*.*..*..**..*..*...*...*.*.......*..*..*....*.*.........

output:

6

result:

ok 1 number(s): "6"

Test #10:

score: 0
Accepted
time: 679ms
memory: 190836kb

input:

2978 104
.*.........*...**.....*...*........*..*...*.*.*.....*........*.*...*.....*...*....*...*..*...*..*..*....*...*..*.*....*....*...*.......*.*.....*.*.......*.*..*...*.*.......*.*.*..........*.*..*.*..**.*....*.*.*..*...*.*.*.....*....*...*.*.*.*.........*.*.....**.*.*..*.*....*........*...*.**...

output:

58

result:

ok 1 number(s): "58"