結果
問題 | No.1323 うしらずSwap |
ユーザー | trineutron |
提出日時 | 2020-12-21 00:04:23 |
言語 | C++17 (gcc 12.3.0 + boost 1.83.0) |
結果 |
WA
|
実行時間 | - |
コード長 | 3,831 bytes |
コンパイル時間 | 2,444 ms |
コンパイル使用メモリ | 222,572 KB |
実行使用メモリ | 220,816 KB |
最終ジャッジ日時 | 2023-10-21 11:20:37 |
合計ジャッジ時間 | 16,735 ms |
ジャッジサーバーID (参考情報) |
judge13 / judge12 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 2 ms
4,348 KB |
testcase_01 | AC | 2 ms
4,348 KB |
testcase_02 | AC | 1 ms
4,348 KB |
testcase_03 | WA | - |
testcase_04 | AC | 2 ms
4,348 KB |
testcase_05 | WA | - |
testcase_06 | AC | 2 ms
4,348 KB |
testcase_07 | AC | 2 ms
4,348 KB |
testcase_08 | WA | - |
testcase_09 | WA | - |
testcase_10 | AC | 2 ms
4,348 KB |
testcase_11 | WA | - |
testcase_12 | WA | - |
testcase_13 | AC | 1 ms
4,348 KB |
testcase_14 | AC | 2 ms
4,348 KB |
testcase_15 | WA | - |
testcase_16 | AC | 261 ms
207,088 KB |
testcase_17 | AC | 363 ms
206,824 KB |
testcase_18 | AC | 254 ms
207,088 KB |
testcase_19 | AC | 370 ms
206,824 KB |
testcase_20 | AC | 256 ms
206,824 KB |
testcase_21 | WA | - |
testcase_22 | WA | - |
testcase_23 | WA | - |
testcase_24 | WA | - |
testcase_25 | WA | - |
testcase_26 | AC | 262 ms
220,816 KB |
testcase_27 | AC | 260 ms
220,816 KB |
testcase_28 | AC | 228 ms
199,216 KB |
testcase_29 | AC | 247 ms
220,748 KB |
testcase_30 | AC | 376 ms
220,552 KB |
testcase_31 | AC | 248 ms
220,288 KB |
testcase_32 | WA | - |
testcase_33 | AC | 428 ms
199,620 KB |
testcase_34 | AC | 423 ms
200,200 KB |
testcase_35 | AC | 405 ms
201,904 KB |
testcase_36 | AC | 424 ms
202,128 KB |
testcase_37 | AC | 413 ms
201,144 KB |
testcase_38 | AC | 379 ms
201,096 KB |
testcase_39 | AC | 389 ms
201,860 KB |
testcase_40 | AC | 394 ms
201,424 KB |
testcase_41 | AC | 410 ms
202,484 KB |
testcase_42 | AC | 391 ms
201,364 KB |
testcase_43 | AC | 171 ms
195,124 KB |
testcase_44 | AC | 207 ms
194,936 KB |
testcase_45 | AC | 195 ms
196,856 KB |
testcase_46 | AC | 193 ms
195,064 KB |
testcase_47 | AC | 185 ms
195,128 KB |
testcase_48 | WA | - |
testcase_49 | WA | - |
testcase_50 | WA | - |
testcase_51 | WA | - |
testcase_52 | WA | - |
testcase_53 | WA | - |
testcase_54 | WA | - |
testcase_55 | WA | - |
testcase_56 | WA | - |
testcase_57 | WA | - |
testcase_58 | WA | - |
testcase_59 | WA | - |
testcase_60 | WA | - |
testcase_61 | AC | 2 ms
4,348 KB |
ソースコード
#include <bits/stdc++.h> using namespace std; using point = pair<int, int>; const vector<int> dx{1, 0, -1, 0}, dy{0, 1, 0, -1}; int main() { constexpr int inf = 1e9; int h, w, ra, ca, rb, cb; cin >> h >> w >> ra >> ca >> rb >> cb; ra--; ca--; rb--; cb--; vector<string> s(h); for (int i = 0; i < h; i++) { cin >> s.at(i); } vector d(h, vector(w, inf)), db(h, vector(w, inf)), dloop(h, vector(w, inf)); queue<point> q; d.at(ra).at(ca) = 0; q.emplace(ra, ca); while (not q.empty()) { auto [x, y] = q.front(); q.pop(); for (int i = 0; i < 4; i++) { int newx = x + dx.at(i), newy = y + dy.at(i); if (s.at(newx).at(newy) == '#') continue; if (d.at(newx).at(newy) != inf) continue; d.at(newx).at(newy) = d.at(x).at(y) + 1; q.emplace(newx, newy); } } const int dist = d.at(rb).at(cb); if (dist == inf) { cout << -1 << endl; return 0; } db.at(rb).at(cb) = 0; q.emplace(rb, cb); while (not q.empty()) { auto [x, y] = q.front(); q.pop(); for (int i = 0; i < 4; i++) { int newx = x + dx.at(i), newy = y + dy.at(i); if (s.at(newx).at(newy) == '#') continue; if (db.at(newx).at(newy) != inf) continue; db.at(newx).at(newy) = db.at(x).at(y) + 1; q.emplace(newx, newy); } } vector<vector<int>> v(2 * h * w), v_back(h * w), v_front(h * w); for (int i = 0; i < h; i++) { for (int j = 0; j < w; j++) { if (d.at(i).at(j) == inf or db.at(i).at(j) == inf) continue; int d_total = d.at(i).at(j) + db.at(i).at(j); if (abs(d.at(i).at(j) - db.at(i).at(j)) < dist) { v.at(d.at(i).at(j) - db.at(i).at(j) + h * w).push_back(d_total); } else if (db.at(i).at(j) >= dist) { v_back.at(d.at(i).at(j)).push_back(d_total); } else if (d.at(i).at(j) >= dist) { v_front.at(db.at(i).at(j)).push_back(d_total); } } } int ans = inf; for (int i = 0; i < 2 * h * w; i++) { sort(v.at(i).begin(), v.at(i).end()); if (v.at(i).size() >= 2) { ans = min(ans, v.at(i).at(0) + v.at(i).at(1)); } if (i >= h * w) continue; sort(v_back.at(i).begin(), v_back.at(i).end()); if (v_back.at(i).size() >= 2) { ans = min(ans, v_back.at(i).at(0) + v_back.at(i).at(1)); } sort(v_front.at(i).begin(), v_front.at(i).end()); if (v_front.at(i).size() >= 2) { ans = min(ans, v_front.at(i).at(0) + v_front.at(i).at(1)); } } int x_erase = rb, y_erase = cb, d_erase = dist; while (d_erase) { s.at(x_erase).at(y_erase) = '#'; for (int i = 0; i < 4; i++) { int newx = x_erase + dx.at(i), newy = y_erase + dy.at(i); if (s.at(newx).at(newy) == '#') continue; if (d.at(newx).at(newy) >= d_erase) continue; x_erase = newx; y_erase = newy; d_erase--; break; } } s.at(rb).at(cb) = '.'; dloop.at(ra).at(ca) = 0; q.emplace(ra, ca); while (not q.empty()) { auto [x, y] = q.front(); q.pop(); for (int i = 0; i < 4; i++) { int newx = x + dx.at(i), newy = y + dy.at(i); if (s.at(newx).at(newy) == '#') continue; if (dloop.at(newx).at(newy) != inf) continue; dloop.at(newx).at(newy) = dloop.at(x).at(y) + 1; q.emplace(newx, newy); } } ans = min(ans, dist + dloop.at(rb).at(cb)); if (ans == inf) { cout << -1 << endl; } else { cout << ans << endl; } }