結果
問題 | No.1323 うしらずSwap |
ユーザー | Luzhiled |
提出日時 | 2020-12-16 01:29:24 |
言語 | C++17 (gcc 12.3.0 + boost 1.83.0) |
結果 |
AC
|
実行時間 | 1,530 ms / 3,000 ms |
コード長 | 3,092 bytes |
コンパイル時間 | 2,861 ms |
コンパイル使用メモリ | 214,084 KB |
実行使用メモリ | 234,012 KB |
最終ジャッジ日時 | 2024-11-15 09:28:53 |
合計ジャッジ時間 | 30,891 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge3 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 2 ms
5,248 KB |
testcase_01 | AC | 2 ms
5,248 KB |
testcase_02 | AC | 2 ms
5,248 KB |
testcase_03 | AC | 2 ms
6,816 KB |
testcase_04 | AC | 2 ms
6,820 KB |
testcase_05 | AC | 2 ms
6,816 KB |
testcase_06 | AC | 3 ms
6,816 KB |
testcase_07 | AC | 2 ms
6,816 KB |
testcase_08 | AC | 3 ms
6,820 KB |
testcase_09 | AC | 3 ms
6,816 KB |
testcase_10 | AC | 3 ms
6,816 KB |
testcase_11 | AC | 3 ms
6,816 KB |
testcase_12 | AC | 2 ms
6,820 KB |
testcase_13 | AC | 2 ms
6,816 KB |
testcase_14 | AC | 2 ms
6,816 KB |
testcase_15 | AC | 3 ms
6,816 KB |
testcase_16 | AC | 332 ms
178,744 KB |
testcase_17 | AC | 760 ms
178,492 KB |
testcase_18 | AC | 324 ms
179,036 KB |
testcase_19 | AC | 828 ms
178,456 KB |
testcase_20 | AC | 331 ms
178,816 KB |
testcase_21 | AC | 332 ms
178,816 KB |
testcase_22 | AC | 778 ms
178,940 KB |
testcase_23 | AC | 328 ms
178,668 KB |
testcase_24 | AC | 808 ms
178,944 KB |
testcase_25 | AC | 325 ms
178,816 KB |
testcase_26 | AC | 296 ms
171,220 KB |
testcase_27 | AC | 263 ms
162,532 KB |
testcase_28 | AC | 505 ms
178,732 KB |
testcase_29 | AC | 315 ms
179,028 KB |
testcase_30 | AC | 517 ms
178,852 KB |
testcase_31 | AC | 316 ms
178,772 KB |
testcase_32 | AC | 615 ms
178,968 KB |
testcase_33 | AC | 1,091 ms
204,664 KB |
testcase_34 | AC | 1,176 ms
214,784 KB |
testcase_35 | AC | 1,261 ms
219,904 KB |
testcase_36 | AC | 1,190 ms
223,104 KB |
testcase_37 | AC | 1,212 ms
224,340 KB |
testcase_38 | AC | 1,448 ms
231,296 KB |
testcase_39 | AC | 1,436 ms
232,540 KB |
testcase_40 | AC | 1,530 ms
233,236 KB |
testcase_41 | AC | 1,519 ms
234,012 KB |
testcase_42 | AC | 1,297 ms
233,600 KB |
testcase_43 | AC | 222 ms
145,908 KB |
testcase_44 | AC | 387 ms
150,904 KB |
testcase_45 | AC | 352 ms
160,956 KB |
testcase_46 | AC | 390 ms
150,836 KB |
testcase_47 | AC | 254 ms
151,296 KB |
testcase_48 | AC | 244 ms
145,968 KB |
testcase_49 | AC | 490 ms
150,912 KB |
testcase_50 | AC | 225 ms
141,984 KB |
testcase_51 | AC | 119 ms
124,456 KB |
testcase_52 | AC | 165 ms
133,496 KB |
testcase_53 | AC | 502 ms
151,348 KB |
testcase_54 | AC | 162 ms
133,760 KB |
testcase_55 | AC | 488 ms
151,088 KB |
testcase_56 | AC | 135 ms
129,364 KB |
testcase_57 | AC | 634 ms
160,800 KB |
testcase_58 | AC | 3 ms
5,248 KB |
testcase_59 | AC | 3 ms
5,248 KB |
testcase_60 | AC | 3 ms
5,248 KB |
testcase_61 | AC | 2 ms
5,248 KB |
ソースコード
#include <bits/stdc++.h> using namespace std; constexpr int inf = 1001001001; constexpr int dy[] = {-1, 0, 1, 0}; constexpr int dx[] = {0, 1, 0, -1}; using Graph = vector< vector< int > >; int main() { int h, w; cin >> h >> w; auto to_v = [w](int y, int x) { return y * w + x; }; auto from_v = [w](int v, int &y, int &x) { y = v / w; x = v % w; }; int Ya, Xa, Yb, Xb; cin >> Ya >> Xa >> Yb >> Xb; Ya -= 1; Xa -= 1; Yb -= 1; Xb -= 1; int a = to_v(Ya, Xa); int b = to_v(Yb, Xb); vector< string > ss(h); for (auto &s : ss) cin >> s; ss[Ya][Xa] = 'A'; ss[Yb][Xb] = 'B'; int V = to_v(h, w); auto make_graph = [&](const vector< string > &ss) { Graph G(V); for (int y = 1; y + 1 < h; y++) { for (int x = 1; x + 1 < w; 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); } } } return G; }; vector< int > pre(V, -1); auto bfs = [&](int s, const Graph &G) { int n = G.size(); vector< pair< int, int > > d(n, {inf, 0}); queue< int > que; que.push(s); d[s] = make_pair(0, 1); while (not que.empty()) { int v = que.front(); que.pop(); for (auto &u : G[v]) { if (d[u].first < d[v].first + 1) continue; if (d[u].first == d[v].first + 1) { d[u].second = 2; continue; } d[u] = d[v]; d[u].first += 1; que.push(u); pre[u] = v; } } return d; }; auto G = make_graph(ss); auto bs = bfs(b, G); auto as = bfs(a, G); int ans = inf; if (as[b].second == 0) { ans = -1; } if (as[b].second >= 2) { ans = 2 * as[b].first; } if (as[b].second != 0) { int v = pre[b]; while (v != a) { int y, x; from_v(v, y, x); ss[y][x] = 'x'; v = pre[v]; } } for (int y = 1; y + 1 < h; y++) { for (int x = 1; x + 1 < w; x++) { if (ss[y][x] != 'x') continue; for (int i = 0; i < 4; i++) { int ny = y + dy[i]; int nx = x + dx[i]; if (ss[ny][nx] == '.') { ans = min(ans, 2 * as[b].first + 2); } } } } for (int v = 0; v < V; v++) { if (as[v].second == 0) continue; if ((int)G[v].size() < 3) continue; int cost = 2 * (as[v].first + bs[v].first) + 4; ans = min(ans, cost); } { for (int y = 1; y + 1 < h; y++) { for (int x = 1; x + 1 < w; x++) { if (ss[y][x] != 'x') continue; ss[y][x] = '#'; } } ss[Yb][Xb] = '#'; G = make_graph(ss); auto nas = bfs(a, G); for (int i = 0; i < 4; i++) { int y = Yb + dy[i]; int x = Xb + dx[i]; int v = to_v(y, x); if (v == a) continue; if (nas[v].second == 0) continue; ans = min(ans, as[b].first + nas[v].first + 1); } } if (ans == inf) ans = -1; cout << ans << endl; }