QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#127760#6739. TeleportOrmlis#TL 156ms36048kbC++202.9kb2023-07-20 02:24:172023-07-20 02:24:18

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-20 02:24:18]
  • 评测
  • 测评结果:TL
  • 用时:156ms
  • 内存:36048kb
  • [2023-07-20 02:24:17]
  • 提交

answer

#include "bits/stdc++.h"

#define rep(i, n) for (int i = 0; i < (n); ++i)
#define rep1(i, n) for (int i = 1; i < (n); ++i)
#define rep1n(i, n) for (int i = 1; i <= (n); ++i)
#define repr(i, n) for (int i = (n) - 1; i >= 0; --i)
#define pb push_back
#define eb emplace_back
#define all(a) (a).begin(), (a).end()
#define rall(a) (a).rbegin(), (a).rend()
#define each(x, a) for (auto &x : a)
#define ar array
#define vec vector
#define range(i, n) rep(i, n)

using namespace std;

using ll = long long;
using ull = unsigned long long;
using ld = long double;
using str = string;
using pi = pair<int, int>;
using pl = pair<ll, ll>;

using vi = vector<int>;
using vl = vector<ll>;
using vpi = vector<pair<int, int>>;
using vvi = vector<vi>;

int Bit(int mask, int b) { return (mask >> b) & 1; }

template<class T>
bool ckmin(T &a, const T &b) {
    if (b < a) {
        a = b;
        return true;
    }
    return false;
}

template<class T>
bool ckmax(T &a, const T &b) {
    if (b > a) {
        a = b;
        return true;
    }
    return false;
}

const int INFi = 2e9;
const ll INF = 1e18;
const int LG = 18;


void solve() {
    int n, k;
    cin >> n >> k;
    vector<string> a(n);
    rep(i, n) cin >> a[i];
    vvi d(n, vi(n, -1));
    // x - y
    int mid = n;
    vector<set<int>> to(2 * n + 2);
    rep(i, n) {
        rep(j, n) {
            if (a[i][j] == '.') {
                to[i - j + mid].insert(j);
            }
        }
    }
    queue<pi> q;
    auto Add = [&](int x, int y, int dist) {
        if (x < 0 || x >= n || y < 0 || y >= n || a[x][y] == '*' || d[x][y] != -1) return false;
        d[x][y] = dist;
        to[x - y + mid].erase(y);
        q.push({x, y});
        return true;
    };
    Add(0, 0, 0);
    vi dx = {0, 0, 1, -1};
    vi dy = {1, -1, 0, 0};
    while (!q.empty() && d[n - 1][n - 1] == -1) {
        auto [x, y] = q.front();
        q.pop();
        int dist2 = d[x][y] + 1;
        rep(w, 4) {
            int x2 = x + dx[w];
            int y2 = y + dy[w];
            Add(x2, y2, dist2);
        }
        int len = k / 2;
        while (true) {
            auto it = to[x-y+mid].lower_bound(y);
            if (it == to[x-y+mid].end() || *it > y + len) break;
            int y2 = *it;
            Add(x + (y2 - y), y2, dist2);
        }
        len = (k - 1) / 2;
        swap(x, y);
        x++;
        if (x >= n) continue;
        while (true) {
            auto it = to[x-y+mid].lower_bound(y);
            if (it == to[x-y+mid].end() || *it > y + len) break;
            int y2 = *it;
            Add(x + (y2 - y), y2, dist2);
        }
    }
    cout << d[n - 1][n - 1] << '\n';
}

signed main() {
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout << setprecision(12) << fixed;
    int t = 1;
    rep(i, t) {
        solve();
    }
    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

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

input:

3 2
.*.
.*.
...

output:

3

result:

ok 1 number(s): "3"

Test #2:

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

input:

3 3
.*.
.*.
...

output:

2

result:

ok 1 number(s): "2"

Test #3:

score: 0
Accepted
time: 156ms
memory: 34664kb

input:

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

output:

540

result:

ok 1 number(s): "540"

Test #4:

score: 0
Accepted
time: 112ms
memory: 35532kb

input:

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

output:

5

result:

ok 1 number(s): "5"

Test #5:

score: 0
Accepted
time: 130ms
memory: 34948kb

input:

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

output:

104

result:

ok 1 number(s): "104"

Test #6:

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

input:

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

output:

10

result:

ok 1 number(s): "10"

Test #7:

score: 0
Accepted
time: 130ms
memory: 36048kb

input:

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

output:

21

result:

ok 1 number(s): "21"

Test #8:

score: -100
Time Limit Exceeded

input:

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

output:


result: