QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#125262#6739. TeleportKKT89AC ✓319ms125184kbC++173.1kb2023-07-16 13:29:112023-07-16 13:29:15

Judging History

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

  • [2023-08-10 23:21:45]
  • System Update: QOJ starts to keep a history of the judgings of all the submissions.
  • [2023-07-16 13:29:15]
  • 评测
  • 测评结果:AC
  • 用时:319ms
  • 内存:125184kb
  • [2023-07-16 13:29:11]
  • 提交

answer

#pragma GCC optimize("Ofast")
#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef unsigned long long int ull;

mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
ll myRand(ll B) {
    return (ull)rng() % B;
}
inline double time() {
    return static_cast<long double>(chrono::duration_cast<chrono::nanoseconds>(chrono::steady_clock::now().time_since_epoch()).count()) * 1e-9;
}

const int N = 5005;
string s[N];
int nxt[N][N];
int dist[N][N];

int dx[] = {1, -1, 0, 0};
int dy[] = {0, 0, 1, -1};

int main(){
    cin.tie(nullptr);
    ios::sync_with_stdio(false);
    int n,k; cin >> n >> k;
    for (int i = 0; i < n; ++i) {
        cin >> s[i];
    }
    for (int i = 0; i < n; ++i) {
        for (int j = 0; j < n; ++j) {
            dist[i][j] = -1;
            if (i+1 < n and j+1 < n) {
                nxt[i][j] = 1;
            }
            else {
                nxt[i][j] = -1;
            }
        }
    }
    dist[0][0] = 0;
    queue<pair<int,int>> q;
    auto ph = [&](int x, int y, int cst) -> void {
        q.push({x, y});
        dist[x][y] = cst+1;
        if (x == n-1 and y == n-1) {
            cout << dist[x][y] << endl;
            exit(0);
        }
    };
    q.push({0, 0});
    while (q.size()) {
        auto p = q.front(); q.pop();
        int x = p.first, y = p.second;
        if (s[x][y] == '*') continue;
        if (nxt[x][y] != -1) {
            int mx = k/2;
            mx = min(mx, n-1-x);
            mx = min(mx, n-1-y);
            int gx = x+mx, gy = y+mx;
            bool ed = (nxt[gx][gy] == -1);
            for (int i = nxt[x][y]; i <= mx; ++i) {
                int nx = x+i, ny = y+i;
                if (dist[nx][ny] == -1) {
                    ph(nx, ny, dist[x][y]);
                }
                if (ed) nxt[nx][ny] = -1;
                else nxt[nx][ny] = nxt[gx][gy] + (gx-nx);
            }
            if (!ed) nxt[x][y] = nxt[gx][gy] + (gx-x);
            else nxt[x][y] = -1;
        }
        if (y+1 < n and nxt[y+1][x] != -1) {
            int xx = y+1, yy = x;

            int mx = (k-1)/2;
            mx = min(mx, n-1-xx);
            mx = min(mx, n-1-yy);
            int gx = xx+mx, gy = yy+mx;
            bool ed = (nxt[gx][gy] == -1);
            for (int i = nxt[xx][yy]; i <= mx; ++i) {
                int nx = xx+i, ny = yy+i;
                if (dist[nx][ny] == -1) {
                    ph(nx, ny, dist[x][y]);
                }
                if (ed) nxt[nx][ny] = -1;
                else nxt[nx][ny] = nxt[gx][gy] + (gx-nx);
            }
            if (!ed) nxt[xx][yy] = nxt[gx][gy] + (gx-xx);
            else nxt[xx][yy] = -1;
        }

        for (int i = 0; i < 4; ++i) {
            int nx = x + dx[i];
            int ny = y + dy[i];
            if (0 <= nx and nx < n and 0 <= ny and ny < n and dist[nx][ny] == -1) {
                ph(nx, ny, dist[x][y]);
            }
        }
        if (y+1 < n and dist[y+1][x] == -1) {
            ph(y+1, x, dist[x][y]);
        }
    }
    cout << dist[n-1][n-1] << endl;
}

詳細信息

Test #1:

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

input:

3 2
.*.
.*.
...

output:

3

result:

ok 1 number(s): "3"

Test #2:

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

input:

3 3
.*.
.*.
...

output:

2

result:

ok 1 number(s): "2"

Test #3:

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

input:

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

output:

540

result:

ok 1 number(s): "540"

Test #4:

score: 0
Accepted
time: 7ms
memory: 44172kb

input:

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

output:

5

result:

ok 1 number(s): "5"

Test #5:

score: 0
Accepted
time: 3ms
memory: 43864kb

input:

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

output:

104

result:

ok 1 number(s): "104"

Test #6:

score: 0
Accepted
time: 3ms
memory: 45964kb

input:

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

output:

10

result:

ok 1 number(s): "10"

Test #7:

score: 0
Accepted
time: 3ms
memory: 47776kb

input:

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

output:

21

result:

ok 1 number(s): "21"

Test #8:

score: 0
Accepted
time: 319ms
memory: 124984kb

input:

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

output:

3354

result:

ok 1 number(s): "3354"

Test #9:

score: 0
Accepted
time: 18ms
memory: 119576kb

input:

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

output:

6

result:

ok 1 number(s): "6"

Test #10:

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

input:

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

output:

58

result:

ok 1 number(s): "58"