QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#186314#6739. TeleportUrgantTeam#AC ✓361ms80784kbC++232.1kb2023-09-23 16:49:112023-09-23 16:49:11

Judging History

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

  • [2023-09-23 16:49:11]
  • 评测
  • 测评结果:AC
  • 用时:361ms
  • 内存:80784kb
  • [2023-09-23 16:49:11]
  • 提交

answer

#include<bits/stdc++.h>

using namespace std;

int const maxn = 5005;
char a[maxn][maxn];
int dist[maxn][maxn], inf = 1e9 + 7;
vector < pair < int, int > > dp[2];
vector < pair < int, int > > go = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}};
vector < pair < int, int > > nxt[5];

bool cmp(pair < int, int > f, pair < int, int > d) {
    return f.first + f.second < d.first + d.second;
}

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;
        }
    }
    dist[1][1] = 0;
    dp[0].push_back({1, 1});
    int type = 0;
    while (!dp[type].empty()) {
        for (int i = 0; i < 5; i++) nxt[i] = {};
        for (auto key : dp[type]) {
            for (int j = 0; j < 4; j++) {
                int x = key.first + go[j].first, y = key.second + go[j].second;
                if (x >= 1 && y >= 1 && x <= n && y <= n && a[x][y] == '.' && dist[x][y] == inf) {
                    nxt[j].push_back({x, y});
                    dist[x][y] = dist[key.first][key.second] + 1;
                }
            }
            int x = key.first, y = key.second;
            for (int it = 1; it <= k; it++) {
                y++;
                swap(x, y);
                if (x > n) break;
                if (dist[x][y] <= dist[key.first][key.second]) break;
                if (a[x][y] == '.') {
                    nxt[4].push_back({x, y});
                    dist[x][y] = dist[key.first][key.second] + 1;
                }
            }
        }
        type ^= 1;
        dp[type] = {};
        for (int i = 0; i < 5; i++) {
            vector < pair < int, int > > cur;
            cur.resize(nxt[i].size() + dp[type].size());
            merge(nxt[i].begin(), nxt[i].end(), dp[type].begin(), dp[type].end(), cur.begin(), cmp);
            dp[type] = cur;
        }
    }
    if (dist[n][n] == inf) cout << -1;
    else cout << dist[n][n];
    return 0;
}


Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 1ms
memory: 5616kb

input:

3 2
.*.
.*.
...

output:

3

result:

ok 1 number(s): "3"

Test #2:

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

input:

3 3
.*.
.*.
...

output:

2

result:

ok 1 number(s): "2"

Test #3:

score: 0
Accepted
time: 40ms
memory: 29160kb

input:

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

output:

540

result:

ok 1 number(s): "540"

Test #4:

score: 0
Accepted
time: 37ms
memory: 32444kb

input:

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

output:

5

result:

ok 1 number(s): "5"

Test #5:

score: 0
Accepted
time: 39ms
memory: 30724kb

input:

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

output:

104

result:

ok 1 number(s): "104"

Test #6:

score: 0
Accepted
time: 36ms
memory: 29028kb

input:

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

output:

10

result:

ok 1 number(s): "10"

Test #7:

score: 0
Accepted
time: 36ms
memory: 28772kb

input:

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

output:

21

result:

ok 1 number(s): "21"

Test #8:

score: 0
Accepted
time: 361ms
memory: 80784kb

input:

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

output:

3354

result:

ok 1 number(s): "3354"

Test #9:

score: 0
Accepted
time: 332ms
memory: 76732kb

input:

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

output:

6

result:

ok 1 number(s): "6"

Test #10:

score: 0
Accepted
time: 351ms
memory: 78688kb

input:

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

output:

58

result:

ok 1 number(s): "58"