結果
| 問題 |
No.1323 うしらずSwap
|
| コンテスト | |
| ユーザー |
t33f
|
| 提出日時 | 2020-12-20 11:53:49 |
| 言語 | C++17(clang) (17.0.6 + boost 1.87.0) |
| 結果 |
WA
|
| 実行時間 | - |
| コード長 | 3,070 bytes |
| コンパイル時間 | 981 ms |
| コンパイル使用メモリ | 131,628 KB |
| 実行使用メモリ | 33,908 KB |
| 最終ジャッジ日時 | 2024-09-21 11:39:37 |
| 合計ジャッジ時間 | 6,435 ms |
|
ジャッジサーバーID (参考情報) |
judge3 / judge4 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 2 WA * 1 |
| other | AC * 24 WA * 35 |
ソースコード
#include <cassert>
#include <cstring>
#include <tuple>
#include <algorithm>
#include <queue>
#include <vector>
#include <iostream>
using namespace std;
template<typename Callback>
inline void for_nbd4(int x, int y, int lx, int ux, int ly, int uy, Callback f) {
constexpr pair<int, int> dxdy[] = {{1, 0}, {0, 1}, {-1, 0}, {0, -1}};
for (auto [dx, dy] : dxdy) {
const int u = x + dx, v = y + dy;
if (lx <= u && u < ux && ly <= v && v < uy) f(u, v);
}
}
template<typename Callback>
inline void for_nbd4(int x, int y, int X, int Y, Callback f) {
for_nbd4(x, y, 0, X, 0, Y, f);
}
template<typename T, typename Callback>
inline T sum_nbd4(int x, int y, int lx, int ux, int ly, int uy, Callback f) {
constexpr pair<int, int> dxdy[] = {{1, 0}, {0, 1}, {-1, 0}, {0, -1}};
T result{};
for (auto [dx, dy] : dxdy) {
const int u = x + dx, v = y + dy;
if (lx <= u && u < ux && ly <= v && v < uy) result += f(u, v);
}
return result;
}
template<typename T, typename Callback>
inline T sum_nbd4(int x, int y, int X, int Y, Callback f) {
return sum_nbd4<T, Callback>(x, y, 0, X, 0, Y, f);
}
int main() {
int h, w, sx, sy, gx, gy; cin >> h >> w >> sx >> sy >> gx >> gy;
sx--; sy--; gx--; gy--;
vector<string> S(h);
for (int i = 0; i < h; i++) cin >> S[i];
int dist[h][w], count[h][w];
pair<int, int> from[h][w];
constexpr int INF = 1<<30;
queue<tuple<int, int> > q;
q.emplace(sx, sy);
for (int i = 0; i < h; i++)
for (int j = 0; j < w; j++)
if (i == sx && j == sy) {
dist[i][j] = 0;
count[i][j] = 1;
from[i][j] = {-1, -1};
} else {
dist[i][j] = INF;
count[i][j] = 0;
}
while (!q.empty()) {
auto [x, y] = q.front();
q.pop();
for_nbd4(x, y, h, w, [x=x, y=y, &S, &dist, &q, &count, &from](int u, int v) {
if (S[u][v] == '#') return;
if (dist[u][v] > dist[x][y] + 1) {
dist[u][v] = dist[x][y] + 1;
count[u][v] = count[x][y];
from[u][v] = {x, y};
q.emplace(u, v);
} else if (dist[u][v] == dist[x][y] + 1) {
assert(count[u][v] > 0);
assert(count[x][y] > 0);
count[u][v] = 2;
}
});
}
// for (int i = 0; i < h; i++)
// for (int j = 0; j < w; j++)
// cerr << i << ' ' << j << ' ' << dist[i][j] << ' ' << count[i][j] << endl;
if (count[gx][gy] == 0) cout << -1 << endl;
else if (count[gx][gy] > 1) cout << 2 * dist[gx][gy] << endl;
else {
for (int x = gx, y = gy;;) {
tie(x, y) = from[x][y];
if (x == sx && y == sy) break;
if (sum_nbd4<int>(x, y, h, w, [&](int u, int v) {return int(S[u][v] == '.');}) > 2)
{
cout << 2 * dist[gx][gy] + 2 << endl;
return 0;
}
}
}
}
t33f