QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#202468#6739. Teleportreal_sigma_team#AC ✓304ms73940kbC++233.2kb2023-10-06 04:33:202023-10-06 04:33:20

Judging History

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

  • [2023-10-06 04:33:20]
  • 评测
  • 测评结果:AC
  • 用时:304ms
  • 内存:73940kb
  • [2023-10-06 04:33:20]
  • 提交

answer

#include <bits/stdc++.h>
#include <random>

#ifndef LOCAL
//#pragma GCC optimize("O3")
    //#pragma GCC optimize("Ofast")
    //#pragma GCC optimize("unroll-loops")
    //#pragma GCC target("avx,avx2,bmi,bmi2,popcnt,lzcnt")
#endif
using namespace std;
using ll = long long;
using dd = double;
using ld = long double;
using uii = unsigned int;
#define x first
#define y second
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()


void solve();

mt19937_64 mt(373);

int32_t main() {
#ifdef LOCAL
    freopen("input.in", "r", stdin);
    freopen("output.out", "w", stdout);
#else
    ios::sync_with_stdio(false);
    cin.tie(nullptr); cout.tie(nullptr);
    //freopen("amusing.in", "r", stdin);
    //freopen("amusing.out", "w", stdout);
#endif
    cout << fixed << setprecision(30);
    int tests = 1;
    //cin >> tests;
    while (tests--) {
        solve();
    }
}

void solve() {
    int n, k;
    cin >> n >> k;
    vector<vector<int>> p(n, vector<int>(n));
    vector<vector<int>> used(n, vector<int>(n, 0));
    for (int i = 0; i < n; i++) {
        string s;
        cin >> s;
        for (int j = 0; j < n; j++) {
            if (s[j] == '.') {
                p[i][j] = -2;
            } else {
                p[i][j] = -1;
                used[i][j] = 1;
            }
        }
    }
    if (p[0][0] == -1) {
        cout << "-1\n";
        return;
    }
    p[0][0] = 0;
    vector<pair<int, int>> d(2 * n + 1, {1000000, 1000000});
    for (int i = 0; i < n; i++) {
        d[i + n - 1] = {i, 0};
    }
    for (int i = 0; i < n; i++) {
        d[-i + n - 1] = {0, i};
    }
    queue<pair<int, int>> q;
    q.emplace(0, 0);
    used[0][0] = 1;
    while (!q.empty()) {
        int i = q.front().x, j = q.front().y;
        q.pop();
        if (i > 0 && !used[i - 1][j]) {
            used[i - 1][j] = 1;
            p[i - 1][j] = p[i][j] + 1;
            q.emplace(i - 1, j);
        }
        if (j > 0 && !used[i][j - 1]) {
            used[i][j - 1] = 1;
            p[i][j - 1] = p[i][j] + 1;
            q.emplace(i, j - 1);
        }
        if (i < n - 1 && !used[i + 1][j]) {
            used[i + 1][j] = 1;
            p[i + 1][j] = p[i][j] + 1;
            q.emplace(i + 1, j);
        }
        if (j < n - 1 && !used[i][j + 1]) {
            used[i][j + 1] = 1;
            p[i][j + 1] = p[i][j] + 1;
            q.emplace(i, j + 1);
        }
        int d1 = i - j + n - 1, d2 = j + 1 - i + n - 1;
        while (d[d1].x - i <= k / 2 && d[d1].x < n && d[d1].y < n) {
            if (!used[d[d1].x][d[d1].y] && d[d1].x > i) {
                used[d[d1].x][d[d1].y] = 1;
                p[d[d1].x][d[d1].y] = 1 + p[i][j];
                q.push(d[d1]);
            }
            d[d1].x++; d[d1].y++;
        }
        while (d[d2].x - j - 1 < (k + 1) / 2 && d[d2].x < n && d[d2].y < n) {
            if (!used[d[d2].x][d[d2].y] && d[d2].x >= j + 1) {
                used[d[d2].x][d[d2].y] = 1;
                p[d[d2].x][d[d2].y] = 1 + p[i][j];
                q.push(d[d2]);
            }
            d[d2].x++; d[d2].y++;
        }
    }
    if (p[n - 1][n - 1] < 0) {
        cout << "-1\n";
        return;
    }
    cout << p[n - 1][n - 1] << '\n';
}

详细

Test #1:

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

input:

3 2
.*.
.*.
...

output:

3

result:

ok 1 number(s): "3"

Test #2:

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

input:

3 3
.*.
.*.
...

output:

2

result:

ok 1 number(s): "2"

Test #3:

score: 0
Accepted
time: 24ms
memory: 10788kb

input:

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

output:

540

result:

ok 1 number(s): "540"

Test #4:

score: 0
Accepted
time: 25ms
memory: 11052kb

input:

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

output:

5

result:

ok 1 number(s): "5"

Test #5:

score: 0
Accepted
time: 24ms
memory: 10880kb

input:

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

output:

104

result:

ok 1 number(s): "104"

Test #6:

score: 0
Accepted
time: 25ms
memory: 10964kb

input:

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

output:

10

result:

ok 1 number(s): "10"

Test #7:

score: 0
Accepted
time: 26ms
memory: 11164kb

input:

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

output:

21

result:

ok 1 number(s): "21"

Test #8:

score: 0
Accepted
time: 264ms
memory: 73940kb

input:

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

output:

3354

result:

ok 1 number(s): "3354"

Test #9:

score: 0
Accepted
time: 304ms
memory: 69740kb

input:

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

output:

6

result:

ok 1 number(s): "6"

Test #10:

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

input:

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

output:

58

result:

ok 1 number(s): "58"