結果

問題 No.3121 Prime Dance
ユーザー Nzt3
提出日時 2025-04-16 01:12:01
言語 C++23
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 819 ms / 2,000 ms
コード長 3,116 bytes
コンパイル時間 7,951 ms
コンパイル使用メモリ 299,936 KB
実行使用メモリ 206,960 KB
最終ジャッジ日時 2025-04-16 01:13:35
合計ジャッジ時間 8,481 ms
ジャッジサーバーID
(参考情報)
judge5 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other AC * 21
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;

const int MAX_P = 4000;
int main() {
  int H, W;
  cin >> H >> W;
  using ai2 = array<int, 2>;
  ai2 S, G;
  cin >> S[0] >> S[1];
  cin >> G[0] >> G[1];
  S[0]--, S[1]--;
  G[0]--, G[1]--;
  vector<string> C(H);
  for (int i = 0; i < H; i++) cin >> C[i];

  if (S[0] < G[0]) {
    S[0] = H - 1 - S[0];
    G[0] = H - 1 - G[0];
    reverse(C.begin(), C.end());
  }
  if (S[1] < G[1]) {
    S[1] = W - 1 - S[1];
    G[1] = W - 1 - G[1];
    for (auto &i : C) {
      reverse(i.begin(), i.end());
    }
  }

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

  vector L(H, vector(W, false));
  auto move_ok = [&](int x, int y) {
    if (x < 0 || x >= H || y < 0 || y >= W) return false;
    return C[x][y] != '#';
  };

  for (int x = 0; x < H; x++) {
    for (int y = 0; y < W; y++) {
      for (int i = 0; i < 4; i++) {
        if (move_ok(x, y) && move_ok(x + dx[i], y + dy[i]) &&
            move_ok(x + dx[i + 1], y + dy[i + 1])) {
          L[x][y] = 1;
        }
      }
    }
  }
  vector dist(H, vector(W, vector(H * W * 2, ai2{(int)1e8, (int)1e8})));
  dist[S[0]][S[1]][0][0] = 0;
  using ai4 = array<int, 4>;
  deque<ai4> bfs;
  bfs.push_back(ai4{S[0], S[1], 0, 0});
  while (bfs.size()) {
    auto [x, y, a, l] = bfs.front();
    bfs.pop_front();
    int d = dist[x][y][a][l];
    if (L[x][y] && dist[x][y][a][1] > d) {
      dist[x][y][a][1] = d;
      bfs.push_front(ai4{x, y, a, 1});
    }
    if (a + 1 < H * W * 2) {
      // a
      int x2 = x + 1, y2 = y;
      if (move_ok(x2, y2) && dist[x2][y2][a + 1][l] > d) {
        dist[x2][y2][a + 1][l] = d;
        bfs.push_front(ai4{x2, y2, a + 1, l});
      }
    }
    {
      // b
      int x2 = x - 1, y2 = y;
      if (move_ok(x2, y2) && dist[x2][y2][a][l] > d) {
        dist[x2][y2][a][l] = d;
        bfs.push_front(ai4{x2, y2, a, l});
      }
    }
    {
      // c
      int x2 = x, y2 = y + 1;
      if (move_ok(x2, y2) && dist[x2][y2][a][l] > d + 1) {
        dist[x2][y2][a][l] = d + 1;
        bfs.push_back(ai4{x2, y2, a, l});
      }
    }
    {
      // d
      int x2 = x, y2 = y - 1;
      if (move_ok(x2, y2) && dist[x2][y2][a][l] > d) {
        dist[x2][y2][a][l] = d;
        bfs.push_front(ai4{x2, y2, a, l});
      }
    }
  }

  vector prime(MAX_P, true);
  prime[0] = prime[1] = 0;
  for (int i = 2; i < MAX_P; i++) {
    if (!prime[i]) continue;
    for (int j = i * i; j < MAX_P; j += i) {
      prime[j] = 0;
    }
  }

  vector pH(0, 0), pW(0, 0);
  for (int i = 0; i + (S[0] - G[0]) < MAX_P; i++) {
    if (prime[i] && prime[i + (S[0] - G[0])]) {
      pH.push_back(i);
    }
  }
  for (int i = 0; i + (S[1] - G[1]) < MAX_P; i++) {
    if (prime[i] && prime[i + (S[1] - G[1])]) {
      pW.push_back(i);
    }
  }
  pH.push_back(1e8);
  pW.push_back(1e8);
  int ans = 1e8;
  for (int a = 1; a < H * W * 2; a++) {
    int c = dist[G[0]][G[1]][a][1];
    int h = *lower_bound(pH.begin(), pH.end(), a);
    int w = *lower_bound(pW.begin(), pW.end(), c);
    ans = min(ans, h * 2 + (S[0] - G[0]) + w * 2 + (S[1] - G[1]));
  }
  cout << (ans == 1e8 ? -1 : ans) << '\n';
}
0