QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#132690#6739. Teleportbucketpotato#AC ✓630ms240268kbC++203.2kb2023-07-31 03:01:272023-07-31 03:01:28

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-31 03:01:28]
  • 评测
  • 测评结果:AC
  • 用时:630ms
  • 内存:240268kb
  • [2023-07-31 03:01:27]
  • 提交

answer

#include "bits/stdc++.h"

using namespace std;

#define ll long long
const int MAXN = 5050;

int n, k;
bitset<MAXN*2> ok[MAXN];
bitset<MAXN*2> done[MAXN];
int dist[MAXN][MAXN * 2];
int fini[MAXN];

vector<int> ch[MAXN], nh[MAXN][5];

int get(int r, int c) {

  int mv = min(r, c);

  r -= mv;
  c -= mv;

  if (r != 0) {
    r--;
    swap(r, c);
  }

  return c;

}

void solve() {

  vector<int> curr;
  curr.push_back(0);
  ch[0].push_back(0);
  dist[0][0] = 0;
  done[0][0] = 1;

  while (curr.size()) {

    for (int x : curr) {
      fini[x] = 0;
    }

    vector<int> nc;
    for (int x : curr) {

      if (fini[x]) continue;
      fini[x] = 1;

      for (int j : ch[x]) {

        // cout << "we have " << x << " " << j << " at dist " << dist[x][j] << endl;

        vector<int> cpu;

        for (int i = 1; i <= k; i++) {

          if (j + i >= MAXN * 2) break;
          if (dist[x][i + j] <= dist[x][j]) break;
          if (done[x][i + j]) break;

          if (ok[x][i + j]) {
            dist[x][i + j] = dist[x][j] + 1;
            cpu.push_back(i + j);
            nc.push_back(x);
            for (int y = i + j; y > j; y--) {
              if (done[x][y]) break;
              done[x][y] = 1;
            }
          }

        }

        reverse(cpu.begin(), cpu.end());

        for (int y : cpu) {
          nh[x][0].push_back(y);
        }

      }

    }

    for (int x : curr) {

      for (int j : ch[x]) {

        auto funn = [&](int r1, int c1, int r2, int c2, int ty) {
          if (0 <= r2 && r2 < n && 0 <= c2 && c2 < MAXN * 2) {
            if (ok[r2][c2] && dist[r2][c2] > dist[r1][c1] + 1) {
              // cout << r1 << " " << c1 << " -> " << r2 << " " << c2 << endl;
              dist[r2][c2] = dist[r1][c1] + 1;
              nc.push_back(r2);
              nh[r2][ty].push_back(c2);
            }
          }
        };

        funn(x, j, x - 1, j    , 1);
        funn(x, j, x - 1, j + 2, 2);
        funn(x, j, x + 1, j    , 3);
        funn(x, j, x + 1, j - 2, 4);

      }

      ch[x].clear();

    }

    curr = nc;
    for (int x : nc) {

      while (1) {

        int cmax = -1;
        int wi = -1;

        for (int i = 0; i < 5; i++) {
          if (nh[x][i].size() && nh[x][i].back() > cmax) {
            cmax = nh[x][i].back();
            wi = i;
          }
        }

        if (wi == -1) break;

        nh[x][wi].pop_back();
        if (!ch[x].size() || ch[x].back() != cmax) {
          ch[x].push_back(cmax);
        }

      }

    }

  }

}

int main() {
  cin.tie(0)->sync_with_stdio(0);
  cin.exceptions(cin.failbit);

  cin >> n >> k;

  for (int i = 0; i < n; i++) {
    string s; cin >> s;
    for (int j = 0; j < n; j++) {
      if (s[j] == '.') {
        int cr = get(i, j);
        ok[cr][i + j - cr] = 1;
      }
    }
  }

  for (int i = 0; i < MAXN; i++) {
    for (int j = 0; j < 2 * MAXN; j++) {
      dist[i][j] = 1e9;
    }
  }

  // do stuff
  solve();

  int nr = get(n - 1, n - 1);

  if (dist[nr][n * 2 - 2 - nr] > 1e8) {
    cout << "-1\n";
  }
  else {
    cout << dist[nr][n * 2 - 2 - nr] << "\n";
  }

}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 16ms
memory: 205504kb

input:

3 2
.*.
.*.
...

output:

3

result:

ok 1 number(s): "3"

Test #2:

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

input:

3 3
.*.
.*.
...

output:

2

result:

ok 1 number(s): "2"

Test #3:

score: 0
Accepted
time: 58ms
memory: 208952kb

input:

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

output:

540

result:

ok 1 number(s): "540"

Test #4:

score: 0
Accepted
time: 53ms
memory: 212544kb

input:

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

output:

5

result:

ok 1 number(s): "5"

Test #5:

score: 0
Accepted
time: 61ms
memory: 207112kb

input:

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

output:

104

result:

ok 1 number(s): "104"

Test #6:

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

input:

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

output:

10

result:

ok 1 number(s): "10"

Test #7:

score: 0
Accepted
time: 55ms
memory: 211188kb

input:

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

output:

21

result:

ok 1 number(s): "21"

Test #8:

score: 0
Accepted
time: 630ms
memory: 214076kb

input:

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

output:

3354

result:

ok 1 number(s): "3354"

Test #9:

score: 0
Accepted
time: 411ms
memory: 240268kb

input:

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

output:

6

result:

ok 1 number(s): "6"

Test #10:

score: 0
Accepted
time: 400ms
memory: 222720kb

input:

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

output:

58

result:

ok 1 number(s): "58"