結果
問題 | No.1323 うしらずSwap |
ユーザー | Luzhiled |
提出日時 | 2020-12-10 18:19:49 |
言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
結果 |
WA
|
実行時間 | - |
コード長 | 2,740 bytes |
コンパイル時間 | 2,348 ms |
コンパイル使用メモリ | 205,428 KB |
最終ジャッジ日時 | 2025-01-16 21:28:20 |
ジャッジサーバーID (参考情報) |
judge1 / judge2 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 3 ms
6,816 KB |
testcase_01 | AC | 10 ms
6,816 KB |
testcase_02 | AC | 4 ms
6,820 KB |
testcase_03 | AC | 2 ms
6,816 KB |
testcase_04 | AC | 2 ms
6,816 KB |
testcase_05 | AC | 2 ms
6,816 KB |
testcase_06 | AC | 2 ms
6,816 KB |
testcase_07 | AC | 2 ms
6,816 KB |
testcase_08 | AC | 3 ms
6,820 KB |
testcase_09 | AC | 2 ms
6,820 KB |
testcase_10 | AC | 2 ms
6,820 KB |
testcase_11 | AC | 2 ms
6,820 KB |
testcase_12 | AC | 2 ms
6,820 KB |
testcase_13 | AC | 2 ms
6,816 KB |
testcase_14 | WA | - |
testcase_15 | WA | - |
testcase_16 | AC | 573 ms
213,476 KB |
testcase_17 | AC | 1,528 ms
213,172 KB |
testcase_18 | AC | 581 ms
213,576 KB |
testcase_19 | AC | 1,597 ms
213,212 KB |
testcase_20 | AC | 712 ms
213,248 KB |
testcase_21 | AC | 730 ms
213,300 KB |
testcase_22 | AC | 1,393 ms
213,552 KB |
testcase_23 | AC | 504 ms
213,160 KB |
testcase_24 | AC | 1,419 ms
213,328 KB |
testcase_25 | AC | 488 ms
213,120 KB |
testcase_26 | AC | 449 ms
213,580 KB |
testcase_27 | AC | 450 ms
213,672 KB |
testcase_28 | AC | 932 ms
213,296 KB |
testcase_29 | AC | 444 ms
213,564 KB |
testcase_30 | AC | 1,000 ms
213,504 KB |
testcase_31 | AC | 439 ms
213,032 KB |
testcase_32 | AC | 1,085 ms
213,444 KB |
testcase_33 | AC | 1,819 ms
252,352 KB |
testcase_34 | AC | 1,973 ms
267,448 KB |
testcase_35 | AC | 2,080 ms
274,872 KB |
testcase_36 | AC | 2,027 ms
279,828 KB |
testcase_37 | AC | 2,120 ms
282,012 KB |
testcase_38 | AC | 2,381 ms
292,428 KB |
testcase_39 | AC | 2,329 ms
293,960 KB |
testcase_40 | AC | 2,479 ms
295,072 KB |
testcase_41 | AC | 2,497 ms
296,100 KB |
testcase_42 | AC | 2,203 ms
295,776 KB |
testcase_43 | AC | 420 ms
164,152 KB |
testcase_44 | AC | 649 ms
171,776 KB |
testcase_45 | AC | 649 ms
186,520 KB |
testcase_46 | AC | 630 ms
171,720 KB |
testcase_47 | AC | 500 ms
172,056 KB |
testcase_48 | AC | 415 ms
163,880 KB |
testcase_49 | AC | 647 ms
171,732 KB |
testcase_50 | AC | 382 ms
158,104 KB |
testcase_51 | AC | 173 ms
131,900 KB |
testcase_52 | AC | 259 ms
145,412 KB |
testcase_53 | AC | 640 ms
171,936 KB |
testcase_54 | AC | 258 ms
145,660 KB |
testcase_55 | WA | - |
testcase_56 | AC | 206 ms
138,916 KB |
testcase_57 | AC | 837 ms
186,140 KB |
testcase_58 | AC | 3 ms
6,816 KB |
testcase_59 | AC | 4 ms
6,820 KB |
testcase_60 | AC | 3 ms
6,820 KB |
testcase_61 | AC | 2 ms
6,820 KB |
ソースコード
#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; auto bfs = [&](int s, int t) { vector< vector< int > > res(V); res[s].emplace_back(0); struct T { int v, pre, cost; bool f; T(int v_, int pre_, int cost_, bool f_) : v(v_), pre(pre_), cost(cost_), f(f_) {} }; queue< T > que; que.emplace(s, -1, 0, false); while (not que.empty()) { auto [v, pre, cost, f] = que.front(); que.pop(); for (auto &u : G[v]) { if (u == pre) continue; if ((int)res[u].size() == 2) continue; if ((int)res[u].size() == 1 and f) continue; res[u].emplace_back(cost + 1); que.emplace(u, v, cost + 1, f or (u == t)); } } return res; }; cerr << "st bfs" << endl; auto da = bfs(a, b); auto db = bfs(b, a); cerr << "ed bfs" << endl; int ans = inf; if ((int)da[b].size() == 0) { ans = -1; } if ((int)db[a].size() == 2) { ans = min(ans, da[b][0] + db[a][1]); } if ((int)da[b].size() == 2) { ans = min(ans, db[a][0] + da[b][1]); } auto find_fromv = [&](int y, int x, const vector< vector< int > > &dist) { int f = to_v(y, x); for (int i = 0; i < 4; i++) { int u = to_v(y + dy[i], x + dx[i]); if ((int)dist[u].size() == 0) continue; if (dist[u][0] > dist[f][0]) continue; f = u; } return f; }; cerr << "uku" << endl; for (int y = 1; y < h - 1; y++) { for (int x = 1; x < w - 1; x++) { int v = to_v(y, x); if ((int)da[v].size() == 0) continue; if ((int)db[v].size() == 0) continue; if ((int)G[v].size() < 3) continue; int af = find_fromv(y, x, da); int bf = find_fromv(y, x, db); int cost = 2 * (da[v][0] + db[v][0]); if (af == bf or af == v or bf == v) { cost += 4; } else { cost += 2; } ans = min(ans, cost); } } if (ans == inf) { ans = -1; } cout << ans << endl; }