結果
問題 | No.1323 うしらずSwap |
ユーザー | Luzhiled |
提出日時 | 2020-12-16 01:41:23 |
言語 | C++17 (gcc 12.3.0 + boost 1.83.0) |
結果 |
AC
|
実行時間 | 1,501 ms / 3,000 ms |
コード長 | 3,078 bytes |
コンパイル時間 | 2,610 ms |
コンパイル使用メモリ | 214,380 KB |
実行使用メモリ | 234,020 KB |
最終ジャッジ日時 | 2024-11-15 09:30:22 |
合計ジャッジ時間 | 30,049 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
5,248 KB |
testcase_04 | AC | 2 ms
5,248 KB |
testcase_05 | AC | 2 ms
5,248 KB |
testcase_06 | AC | 2 ms
5,248 KB |
testcase_07 | AC | 2 ms
5,248 KB |
testcase_08 | AC | 2 ms
5,248 KB |
testcase_09 | AC | 2 ms
5,248 KB |
testcase_10 | AC | 2 ms
5,248 KB |
testcase_11 | AC | 2 ms
5,248 KB |
testcase_12 | AC | 2 ms
5,248 KB |
testcase_13 | AC | 2 ms
5,248 KB |
testcase_14 | AC | 2 ms
5,248 KB |
testcase_15 | AC | 2 ms
5,248 KB |
testcase_16 | AC | 326 ms
178,688 KB |
testcase_17 | AC | 737 ms
178,688 KB |
testcase_18 | AC | 321 ms
178,944 KB |
testcase_19 | AC | 803 ms
178,700 KB |
testcase_20 | AC | 321 ms
178,816 KB |
testcase_21 | AC | 325 ms
178,936 KB |
testcase_22 | AC | 755 ms
179,036 KB |
testcase_23 | AC | 322 ms
178,816 KB |
testcase_24 | AC | 778 ms
178,816 KB |
testcase_25 | AC | 321 ms
178,680 KB |
testcase_26 | AC | 291 ms
171,156 KB |
testcase_27 | AC | 265 ms
162,352 KB |
testcase_28 | AC | 499 ms
178,816 KB |
testcase_29 | AC | 309 ms
178,964 KB |
testcase_30 | AC | 504 ms
178,920 KB |
testcase_31 | AC | 310 ms
178,740 KB |
testcase_32 | AC | 597 ms
178,860 KB |
testcase_33 | AC | 1,059 ms
204,672 KB |
testcase_34 | AC | 1,128 ms
214,784 KB |
testcase_35 | AC | 1,240 ms
219,728 KB |
testcase_36 | AC | 1,162 ms
223,076 KB |
testcase_37 | AC | 1,180 ms
224,512 KB |
testcase_38 | AC | 1,411 ms
231,220 KB |
testcase_39 | AC | 1,429 ms
232,548 KB |
testcase_40 | AC | 1,482 ms
233,184 KB |
testcase_41 | AC | 1,501 ms
234,020 KB |
testcase_42 | AC | 1,288 ms
233,648 KB |
testcase_43 | AC | 218 ms
145,836 KB |
testcase_44 | AC | 385 ms
150,900 KB |
testcase_45 | AC | 345 ms
161,024 KB |
testcase_46 | AC | 385 ms
150,912 KB |
testcase_47 | AC | 251 ms
151,296 KB |
testcase_48 | AC | 240 ms
145,924 KB |
testcase_49 | AC | 480 ms
150,912 KB |
testcase_50 | AC | 219 ms
141,952 KB |
testcase_51 | AC | 117 ms
124,600 KB |
testcase_52 | AC | 161 ms
133,532 KB |
testcase_53 | AC | 502 ms
151,208 KB |
testcase_54 | AC | 159 ms
133,760 KB |
testcase_55 | AC | 477 ms
151,084 KB |
testcase_56 | AC | 134 ms
129,280 KB |
testcase_57 | AC | 626 ms
160,768 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 | 3 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]; } v = pre[b]; while (v != a) { int y, x; from_v(v, y, x); 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); } } v = pre[v]; } } 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); } if (as[b].second != 0) { int v = pre[b]; while (v != a) { int y, x; from_v(v, y, x); ss[y][x] = '#'; v = pre[v]; } ss[Yb][Xb] = '#'; G = make_graph(ss); int cost = as[b].first + 1; as = 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 (as[v].second == 0) continue; ans = min(ans, cost + as[v].first); } } if (ans == inf) ans = -1; cout << ans << endl; }