QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#221866#6739. Teleportycccc319AC ✓289ms48436kbC++171.8kb2023-10-21 14:55:102023-10-21 14:55:11

Judging History

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

  • [2023-10-21 14:55:11]
  • 评测
  • 测评结果:AC
  • 用时:289ms
  • 内存:48436kb
  • [2023-10-21 14:55:10]
  • 提交

answer

#include "bits/stdc++.h"

using namespace std;
using i64 = long long;

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    int n, k;
    cin >> n >> k;
    vector<string> a(n);
    for (int i = 0; i < n; i++) {
        cin >> a[i];
    }
    int sx = 0, sy = 0, tx = n - 1, ty = n - 1;
    queue<array<int, 3>> q;
    q.push({0, sx, sy});
    int dx[] = {+1, -1, +0, +0};
    int dy[] = {+0, +0, +1, -1};
    vector<vector<int>> vis(n, vector<int>(n));
    vis[sx][sy] = 1;
    while (!q.empty()) {
        auto [res, x, y] = q.front();
        if (x == tx && y == ty) {
            cout << res << '\n';
            return 0;
        } 
        q.pop();
        for (int i = 0; i < 4; i++) {
            int nx = x + dx[i];
            int ny = y + dy[i];
            if (nx < 0 || ny < 0 || nx >= n || ny >= n) {
                continue;
            }
            if (vis[nx][ny]) {
                continue;
            }
            if (a[nx][ny] == '*') {
                continue;
            }
            vis[nx][ny] = 1;
            q.push({res + 1, nx, ny});
        }

        int X = x, Y = y;
        for (int i = 0; i <= k; i++) {
            int nx = X;
            int ny = Y;
            if (nx < 0 || ny < 0 || nx >= n || ny >= n) {
                break;
            }
            if (vis[nx][ny]) {
                int tmp = X;
                X = Y + 1;
                Y = tmp;
                continue;
            }
            if (a[nx][ny] == '*') {
                int tmp = X;
                X = Y + 1;
                Y = tmp;
                continue;
            }
            vis[nx][ny] = 1;
            q.push({res + 1, nx, ny});
            int tmp = X;
            X = Y + 1;
            Y = tmp;
        }
    }
    cout << "-1\n";

    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

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

input:

3 2
.*.
.*.
...

output:

3

result:

ok 1 number(s): "3"

Test #2:

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

input:

3 3
.*.
.*.
...

output:

2

result:

ok 1 number(s): "2"

Test #3:

score: 0
Accepted
time: 22ms
memory: 8084kb

input:

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

output:

540

result:

ok 1 number(s): "540"

Test #4:

score: 0
Accepted
time: 6ms
memory: 8248kb

input:

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

output:

5

result:

ok 1 number(s): "5"

Test #5:

score: 0
Accepted
time: 12ms
memory: 8120kb

input:

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

output:

104

result:

ok 1 number(s): "104"

Test #6:

score: 0
Accepted
time: 6ms
memory: 8268kb

input:

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

output:

10

result:

ok 1 number(s): "10"

Test #7:

score: 0
Accepted
time: 4ms
memory: 8320kb

input:

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

output:

21

result:

ok 1 number(s): "21"

Test #8:

score: 0
Accepted
time: 289ms
memory: 48436kb

input:

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

output:

3354

result:

ok 1 number(s): "3354"

Test #9:

score: 0
Accepted
time: 38ms
memory: 45252kb

input:

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

output:

6

result:

ok 1 number(s): "6"

Test #10:

score: 0
Accepted
time: 57ms
memory: 47972kb

input:

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

output:

58

result:

ok 1 number(s): "58"