結果
| 問題 |
No.1323 うしらずSwap
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2020-12-07 23:07:40 |
| 言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
WA
|
| 実行時間 | - |
| コード長 | 2,216 bytes |
| コンパイル時間 | 2,335 ms |
| コンパイル使用メモリ | 178,648 KB |
| 実行使用メモリ | 123,264 KB |
| 最終ジャッジ日時 | 2024-09-21 10:36:48 |
| 合計ジャッジ時間 | 20,372 ms |
|
ジャッジサーバーID (参考情報) |
judge2 / judge4 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 51 WA * 8 |
ソースコード
#include <bits/stdc++.h>
using namespace std;
int main() {
int h, w;
cin >> h >> w;
auto to_v = [w](int y, int x) {
return y * w + x;
};
int Ya, Xa, Yb, Xb;
cin >> Ya >> Xa >> Yb >> Xb;
int a = to_v(Ya - 1, Xa - 1);
int b = to_v(Yb - 1, Xb - 1);
vector< string > ss(h);
for (auto &s : ss) cin >> s;
int V = h * w;
vector< vector< int > > G(V);
constexpr int dy[] = {-1, 0, 1, 0};
constexpr int dx[] = {0, 1, 0, -1};
for (int y = 1; y < h - 1; y++) {
for (int x = 1; x < w - 1; x++) {
if (ss[y][x] == '#') continue;
int v = to_v(y, x);
for (int i = 0; i < 4; i++) {
int ny = y + dy[i];
int nx = x + dx[i];
if (ss[ny][nx] == '#') continue;
int u = to_v(ny, nx);
G[v].emplace_back(u);
}
}
}
constexpr int inf = 1001001001;
vector< int > rs(V);
auto bfs = [&](int s) {
queue< int > que;
que.emplace(s);
vector< int > res(V, inf);
res[s] = 0;
rs.assign(V, 0);
rs[s] = 1;
while (not que.empty()) {
int v = que.front();
que.pop();
for (auto &u : G[v]) {
if (res[u] == res[v] + 1) {
rs[u] = 2;
}
if (res[u] > res[v] + 1) {
res[u] = res[v] + 1;
rs[u] = rs[v];
que.emplace(u);
}
}
}
return res;
};
auto da = bfs(a);
auto db = bfs(b);
int ans = inf;
if (rs[a] == 0) {
ans = -1;
}
if (rs[a] >= 2) {
ans = da[b] + db[a];
}
for (int y = 1; y < h - 1; y++) {
for (int x = 1; x < w - 1; x++) {
int v = to_v(y, x);
if (rs[v] == 0) continue;
if ((int)G[v].size() < 3) continue;
int af = v;
for (int i = 0; i < 4; i++) {
int u = to_v(y + dy[i], x + dx[i]);
if (da[u] > da[af]) continue;
af = u;
}
int bf = v;
for (int i = 0; i < 4; i++) {
int u = to_v(y + dy[i], x + dx[i]);
if (db[u] > db[bf]) continue;
bf = u;
}
if (af == bf) {
ans = min(ans, 2 * (da[v] + db[v]) + 4);
} else {
ans = min(ans, 2 * (da[v] + db[v]) + 2);
}
}
}
if (ans == inf) {
ans = -1;
}
cout << ans << endl;
}