QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#186273#6739. TeleportUrgantTeam#TL 147ms89728kbC++232.3kb2023-09-23 15:58:512023-09-23 15:58:52

Judging History

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

  • [2023-09-23 15:58:52]
  • 评测
  • 测评结果:TL
  • 用时:147ms
  • 内存:89728kb
  • [2023-09-23 15:58:51]
  • 提交

answer

#include<bits/stdc++.h>
#define mp make_pair
#define x first
#define y second

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}};
priority_queue < 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]) {
        dist[i][j] = d;
        Q.push({mp(-dist[i][j].x, -dist[i][j].y), {i, j}});
    }
}

main() {
    //freopen("input.txt", "r", stdin);
    //freopen("output.txt", "w", stdout);
    
    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.push({{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()) {
        pair <pair <int, int>, pair <int, int> > tt = Q.top();
        auto cur = mp(mp(-tt.x.x, -tt.x.y), tt.y);
        Q.pop();
        if (dist[cur.y.x][cur.y.y] != cur.x) continue;

        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: 1ms
memory: 9940kb

input:

3 2
.*.
.*.
...

output:

3

result:

ok 1 number(s): "3"

Test #2:

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

input:

3 3
.*.
.*.
...

output:

2

result:

ok 1 number(s): "2"

Test #3:

score: 0
Accepted
time: 131ms
memory: 85912kb

input:

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

output:

540

result:

ok 1 number(s): "540"

Test #4:

score: 0
Accepted
time: 133ms
memory: 89672kb

input:

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

output:

5

result:

ok 1 number(s): "5"

Test #5:

score: 0
Accepted
time: 145ms
memory: 88380kb

input:

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

output:

104

result:

ok 1 number(s): "104"

Test #6:

score: 0
Accepted
time: 140ms
memory: 88000kb

input:

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

output:

10

result:

ok 1 number(s): "10"

Test #7:

score: 0
Accepted
time: 147ms
memory: 89728kb

input:

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

output:

21

result:

ok 1 number(s): "21"

Test #8:

score: -100
Time Limit Exceeded

input:

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

output:


result: