QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#145293#6739. Teleportberarchegas#AC ✓792ms348072kbC++203.1kb2023-08-22 03:47:522023-08-22 03:47:53

Judging History

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

  • [2023-08-22 03:47:53]
  • 评测
  • 测评结果:AC
  • 用时:792ms
  • 内存:348072kb
  • [2023-08-22 03:47:52]
  • 提交

answer

#include <bits/stdc++.h>
 
using namespace std;
using ll = long long;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
 
mt19937 rng((int) chrono::steady_clock::now().time_since_epoch().count());
    
const int MOD = 1e9 + 7;
const int MAXN = 5e3 + 5;
const ll INF = 2e18;

bool tb[MAXN][MAXN];

pii pai[MAXN][MAXN];
pii mx[MAXN][MAXN];
int qt[MAXN][MAXN];
int dist[MAXN][MAXN];

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

int n, k;

pii find(pii cur) {
    if(cur == pai[cur.first][cur.second]) return cur;
    return pai[cur.first][cur.second] = find(pai[cur.first][cur.second]);
}

void une(pii a, pii b) {
    a = find(a); b = find(b);
    if(a == b) return;
    if(qt[a.first][a.second] > qt[b.first][b.second]) swap(a, b);
    qt[b.first][b.second] += qt[a.first][a.second];
    pai[a.first][a.second] = b;
    if(mx[a.first][a.second].first + mx[a.first][a.second].second > mx[b.first][b.second].first + mx[b.first][b.second].second) mx[b.first][b.second] = mx[a.first][a.second];
    //mx[b.first][b.second] = max(mx[b.first][b.second], mx[a.first][a.second]);
}

pii ant(pii cur) {
    if(cur.first == 1) return cur;
    return {cur.second, cur.first - 1};
}

pii prox(pii cur) {
    return {cur.second + 1, cur.first};
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    cin >> n >> k;
    for(int i = 1; i <= n; i++) {
        string s;
        cin >> s;
        for(int j = 0; j < n; j++) {
            if(s[j] == '.') {
                tb[i][j + 1] = 0;
            }
            else {
                tb[i][j + 1] = 1;
            }
            pai[i][j + 1] = {i, j + 1};
            mx[i][j + 1] = {i, j + 1};
            qt[i][j + 1] = 1;
        }
    }
    queue <pii> fila;
    fila.push({1, 1});
    tb[1][1] = 1;
    dist[n][n] = -1;
    while(!fila.empty()) {
        pii cur = fila.front(); fila.pop();
        //cout << cur.first << ' ' << cur.second << ' ' << dist[cur.first][cur.second] << endl;
        une(cur, ant(cur));
        for(int i = 0; i < 4; i++) {
            int vx = dx[i] + cur.first, vy = dy[i] + cur.second;
            if(vx < 1 || vx > n || vy < 1 || vy > n) continue;
            if(tb[vx][vy] == 1) continue;
            tb[vx][vy] = 1;
            dist[vx][vy] = dist[cur.first][cur.second] + 1;
            fila.push({vx, vy});
            une({vx, vy}, ant({vx, vy}));
        }
        pii rz = find(cur);
        pii at = prox(mx[rz.first][rz.second]);
        while(at.first <= n && at.second <= n && at.first + at.second - cur.first - cur.second <= k) {
            if(tb[at.first][at.second] == 0) {
                tb[at.first][at.second] = 1;
                dist[at.first][at.second] = dist[cur.first][cur.second] + 1;
                fila.push(at);
            }
            une(at, ant(at));
            rz = find(at);
            at = prox(mx[rz.first][rz.second]);
            //cout << "at: " << at.first << ' ' << at.second << ' ' << rz.first << ' ' << rz.second << endl;
        }
    }
    cout << dist[n][n] << '\n';
    return 0;
}

詳細信息

Test #1:

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

input:

3 2
.*.
.*.
...

output:

3

result:

ok 1 number(s): "3"

Test #2:

score: 0
Accepted
time: 3ms
memory: 13828kb

input:

3 3
.*.
.*.
...

output:

2

result:

ok 1 number(s): "2"

Test #3:

score: 0
Accepted
time: 65ms
memory: 133400kb

input:

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

output:

540

result:

ok 1 number(s): "540"

Test #4:

score: 0
Accepted
time: 67ms
memory: 132580kb

input:

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

output:

5

result:

ok 1 number(s): "5"

Test #5:

score: 0
Accepted
time: 76ms
memory: 128772kb

input:

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

output:

104

result:

ok 1 number(s): "104"

Test #6:

score: 0
Accepted
time: 67ms
memory: 131032kb

input:

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

output:

10

result:

ok 1 number(s): "10"

Test #7:

score: 0
Accepted
time: 62ms
memory: 133396kb

input:

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

output:

21

result:

ok 1 number(s): "21"

Test #8:

score: 0
Accepted
time: 645ms
memory: 348072kb

input:

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

output:

3354

result:

ok 1 number(s): "3354"

Test #9:

score: 0
Accepted
time: 769ms
memory: 339440kb

input:

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

output:

6

result:

ok 1 number(s): "6"

Test #10:

score: 0
Accepted
time: 792ms
memory: 345480kb

input:

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

output:

58

result:

ok 1 number(s): "58"