QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#186243#6739. TeleportUrgantTeam#TL 201ms88164kbC++232.0kb2023-09-23 15:16:402023-09-23 15:16:40

Judging History

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

  • [2023-09-23 15:16:40]
  • 评测
  • 测评结果:TL
  • 用时:201ms
  • 内存:88164kb
  • [2023-09-23 15:16:40]
  • 提交

answer

#include<bits/stdc++.h>

using namespace std;

int const maxn = 5005;
char a[maxn][maxn];
pair < int, int > dist[maxn][maxn];
int inf = 1e9 + 7;
vector < pair < int, int > > go = {{1, 0}, {0, 1}, {-1, 0}, {0, -1}};
set < pair < pair < int, int >, pair < int, int > > > Q;
pair < int, int > up[maxn][maxn];

inline void upd(int i, int j, pair < int, int > d) {
    if (d < dist[i][j]) {
        Q.erase({dist[i][j], {i, j}});
        dist[i][j] = d;
        Q.insert({dist[i][j], {i, j}});
    }
}

main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    int n, k;
    cin >> n >> k;
    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= n; j++) {
            cin >> a[i][j];
            dist[i][j] = {inf, inf};
        }
    }
    dist[1][1] = {0, k};
    Q.insert({{0, k}, {1, 1}});
    for (int sum = 2 * n; sum >= 2; sum--) {
        for (int i = 1; i <= n; i++) {
            int j = sum - i;
            if (j < 1 || j > n) continue;
            up[i][j] = {-1, -1};
            int x = j + 1, y = i;
            if (x <= n) {
                if (a[x][y] == '.') up[i][j] = {x, y};
                else up[i][j] = up[x][y];
            }
        }
    }
    while (!Q.empty()) {
        auto cur = *Q.begin();
        Q.erase(Q.begin());
        int x = cur.second.first, y = cur.second.second;
        int t = cur.first.first, s = cur.first.second;
        for (auto key : go) {
            int i = x + key.first, j = y + key.second;
            if (i >= 1 && j >= 1 && i <= n && j <= n && a[i][j] == '.') {
                upd(i, j, {t + 1, k});
            }
        }
        pair < int, int > nxt = up[x][y];
        if (nxt.first != -1) {
            int add_s = nxt.first + nxt.second - x - y;
            if (add_s + s <= k) upd(nxt.first, nxt.second, {t, s + add_s});
            else if (add_s <= k) upd(nxt.first, nxt.second, {t + 1, add_s});
        }
    }
    if (dist[n][n].first == inf) cout << -1 << '\n';
    else cout << dist[n][n].first << '\n';
    return 0;
}


Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 0ms
memory: 7928kb

input:

3 2
.*.
.*.
...

output:

3

result:

ok 1 number(s): "3"

Test #2:

score: 0
Accepted
time: 0ms
memory: 7700kb

input:

3 3
.*.
.*.
...

output:

2

result:

ok 1 number(s): "2"

Test #3:

score: 0
Accepted
time: 166ms
memory: 88084kb

input:

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

output:

540

result:

ok 1 number(s): "540"

Test #4:

score: 0
Accepted
time: 191ms
memory: 88080kb

input:

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

output:

5

result:

ok 1 number(s): "5"

Test #5:

score: 0
Accepted
time: 201ms
memory: 87836kb

input:

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

output:

104

result:

ok 1 number(s): "104"

Test #6:

score: 0
Accepted
time: 179ms
memory: 88164kb

input:

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

output:

10

result:

ok 1 number(s): "10"

Test #7:

score: 0
Accepted
time: 193ms
memory: 88108kb

input:

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

output:

21

result:

ok 1 number(s): "21"

Test #8:

score: -100
Time Limit Exceeded

input:

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

output:


result: