結果
| 問題 |
No.3121 Prime Dance
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2025-05-02 18:59:37 |
| 言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
TLE
|
| 実行時間 | - |
| コード長 | 1,598 bytes |
| コンパイル時間 | 2,763 ms |
| コンパイル使用メモリ | 213,816 KB |
| 実行使用メモリ | 463,104 KB |
| 最終ジャッジ日時 | 2025-05-02 19:00:28 |
| 合計ジャッジ時間 | 47,117 ms |
|
ジャッジサーバーID (参考情報) |
judge2 / judge3 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 2 |
| other | AC * 2 WA * 4 TLE * 15 |
ソースコード
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i, s, t) for (ll i = s; i < (ll)(t); i++)
#define all(x) begin(x), end(x)
template <typename T> bool chmin(T& x, T y) {
return x > y ? (x = y, true) : false;
}
template <typename T> bool chmax(T& x, T y) {
return x < y ? (x = y, true) : false;
}
struct IOST {
IOST() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
cout << fixed << setprecision(20);
}
} IOST;
void solve() {
int h, w;
cin >> h >> w;
int stx, sty, edx, edy;
cin >> stx >> sty >> edx >> edy;
stx--, sty--;
edx--, edy--;
vector<string> c(h);
rep(i, 0, h) cin >> c[i];
int dfx = (edx - stx);
int dfy = (edy - sty);
int m = 400;
vector dp(m + 1, vector(m + 1, vector(h, vector<bool>(w, 0))));
dp[0][0][stx][sty] = 1;
rep(i, 0, m) rep(j, 0, m) {
rep(x, 0, h) rep(y, 0, w) {
if (dp[i][j][x][y]) {
if (x + 1 < h) dp[i][j][x + 1][y] = 1;
if (y + 1 < w) dp[i][j][x][y + 1] = 1;
if (x > 0) dp[i + 1][j][x - 1][y] = 1;
if (y > 0) dp[i][j + 1][x][y - 1] = 1;
}
}
}
set<int> p;
{
int dd = 1e4;
vector<int> mm(dd, 0);
mm[0] = mm[1] = 1;
rep(i, 2, dd) {
if (mm[i]) continue;
for (int j = i * 2; j < dd; j += i) {
mm[j] = 1;
}
p.insert(i);
}
}
ll ans = 1e18;
rep(b, 0, m) rep(d, 0, m) {
if (!p.count(b) || !p.count(d)) continue;
if (dp[b][d][edx][edy]) {
if (!p.count(dfx + b) || !p.count(dfy + d)) continue;
chmin(ans, dfx + b + b + dfy + d + d);
}
}
if (ans == 1e18) ans = -1;
cout << ans << "\n";
}
int main() {
int t = 1;
// cin >> t;
rep(i, 0, t) solve();
}