結果
問題 | No.1323 うしらずSwap |
ユーザー | 👑 emthrm |
提出日時 | 2020-12-20 02:16:40 |
言語 | C++17 (gcc 12.3.0 + boost 1.83.0) |
結果 |
WA
|
実行時間 | - |
コード長 | 3,206 bytes |
コンパイル時間 | 2,586 ms |
コンパイル使用メモリ | 220,796 KB |
実行使用メモリ | 47,104 KB |
最終ジャッジ日時 | 2024-09-21 11:22:01 |
合計ジャッジ時間 | 9,576 ms |
ジャッジサーバーID (参考情報) |
judge5 / judge1 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 2 ms
6,812 KB |
testcase_01 | AC | 3 ms
6,940 KB |
testcase_02 | AC | 2 ms
6,940 KB |
testcase_03 | AC | 3 ms
6,944 KB |
testcase_04 | AC | 2 ms
6,940 KB |
testcase_05 | AC | 2 ms
6,940 KB |
testcase_06 | AC | 2 ms
6,940 KB |
testcase_07 | AC | 2 ms
6,940 KB |
testcase_08 | AC | 2 ms
6,940 KB |
testcase_09 | AC | 2 ms
6,944 KB |
testcase_10 | AC | 2 ms
6,940 KB |
testcase_11 | AC | 2 ms
6,944 KB |
testcase_12 | WA | - |
testcase_13 | AC | 2 ms
6,940 KB |
testcase_14 | AC | 2 ms
6,940 KB |
testcase_15 | AC | 2 ms
6,944 KB |
testcase_16 | AC | 50 ms
33,024 KB |
testcase_17 | AC | 101 ms
32,896 KB |
testcase_18 | AC | 46 ms
33,024 KB |
testcase_19 | AC | 94 ms
32,896 KB |
testcase_20 | AC | 49 ms
33,024 KB |
testcase_21 | WA | - |
testcase_22 | WA | - |
testcase_23 | WA | - |
testcase_24 | WA | - |
testcase_25 | WA | - |
testcase_26 | AC | 91 ms
46,976 KB |
testcase_27 | AC | 96 ms
46,976 KB |
testcase_28 | AC | 73 ms
33,152 KB |
testcase_29 | AC | 93 ms
46,976 KB |
testcase_30 | AC | 181 ms
46,976 KB |
testcase_31 | AC | 94 ms
46,848 KB |
testcase_32 | AC | 188 ms
46,848 KB |
testcase_33 | AC | 128 ms
32,640 KB |
testcase_34 | AC | 137 ms
32,896 KB |
testcase_35 | AC | 137 ms
32,896 KB |
testcase_36 | AC | 135 ms
33,024 KB |
testcase_37 | AC | 135 ms
32,896 KB |
testcase_38 | AC | 142 ms
33,024 KB |
testcase_39 | AC | 144 ms
32,896 KB |
testcase_40 | AC | 141 ms
32,896 KB |
testcase_41 | AC | 146 ms
33,024 KB |
testcase_42 | AC | 136 ms
32,896 KB |
testcase_43 | AC | 48 ms
33,024 KB |
testcase_44 | AC | 76 ms
32,896 KB |
testcase_45 | AC | 65 ms
33,024 KB |
testcase_46 | AC | 77 ms
32,896 KB |
testcase_47 | AC | 54 ms
33,024 KB |
testcase_48 | AC | 77 ms
46,976 KB |
testcase_49 | AC | 130 ms
46,848 KB |
testcase_50 | AC | 74 ms
46,720 KB |
testcase_51 | AC | 51 ms
46,848 KB |
testcase_52 | AC | 62 ms
46,848 KB |
testcase_53 | AC | 122 ms
46,848 KB |
testcase_54 | AC | 61 ms
46,848 KB |
testcase_55 | AC | 133 ms
46,848 KB |
testcase_56 | AC | 54 ms
46,976 KB |
testcase_57 | AC | 140 ms
47,104 KB |
testcase_58 | AC | 2 ms
6,944 KB |
testcase_59 | WA | - |
testcase_60 | WA | - |
testcase_61 | AC | 2 ms
6,940 KB |
ソースコード
#define _USE_MATH_DEFINES #include <bits/stdc++.h> using namespace std; #define FOR(i,m,n) for(int i=(m);i<(n);++i) #define REP(i,n) FOR(i,0,n) #define ALL(v) (v).begin(),(v).end() using ll = long long; constexpr int INF = 0x3f3f3f3f; constexpr long long LINF = 0x3f3f3f3f3f3f3f3fLL; constexpr double EPS = 1e-8; constexpr int MOD = 1000000007; // constexpr int MOD = 998244353; constexpr int dy[] = {1, 0, -1, 0}, dx[] = {0, -1, 0, 1}; constexpr int dy8[] = {1, 1, 0, -1, -1, -1, 0, 1}, dx8[] = {0, -1, -1, -1, 0, 1, 1, 1}; template <typename T, typename U> inline bool chmax(T &a, U b) { return a < b ? (a = b, true) : false; } template <typename T, typename U> inline bool chmin(T &a, U b) { return a > b ? (a = b, true) : false; } struct IOSetup { IOSetup() { std::cin.tie(nullptr); std::ios_base::sync_with_stdio(false); std::cout << fixed << setprecision(20); } } iosetup; int main() { int h, w, ra, ca, rb, cb; cin >> h >> w >> ra >> ca >> rb >> cb; --ra; --ca; --rb; --cb; vector<string> s(h); REP(i, h) cin >> s[i]; vector dist(h, vector(w, -1)), dp(h, vector(w, 0)); dist[ra][ca] = 0; dp[ra][ca] = 1; vector prev(h, vector(w, make_pair(-1, -1))); queue<pair<int, int>> que({{ra, ca}}); while (!que.empty()) { auto [i, j] = que.front(); que.pop(); REP(k, 4) { int y = i + dy[k], x = j + dx[k]; if (s[y][x] == '.') { if (dist[y][x] == -1) { dist[y][x] = dist[i][j] + 1; dp[y][x] = dp[i][j]; prev[y][x] = {i, j}; que.emplace(y, x); } else if (dist[y][x] == dist[i][j] + 1) { dp[y][x] = min(dp[y][x] + dp[i][j], 2); } } } } if (dist[rb][cb] == -1) { cout << "-1\n"; return 0; } if (dp[rb][cb] == 2) { cout << dist[rb][cb] * 2 << '\n'; return 0; } s[rb][cb] = 'b'; int cr = rb, cc = cb; while (cr != ra || cc != ca) { tie(cr, cc) = prev[cr][cc]; s[cr][cc] = '$'; } s[ra][ca] = 'a'; REP(i, h) REP(j, w) { if (s[i][j] == '$') { REP(k, 4) { int y = i + dy[k], x = j + dx[k]; if (s[y][x] == '.') { cout << dist[rb][cb] * 2 + 2 << '\n'; return 0; } } } } int ans = INF; vector dista(h, vector(w, -1)); dista[ra][ca] = 0; que.emplace(ra, ca); while (!que.empty()) { auto [i, j] = que.front(); que.pop(); int cnt = 0; REP(k, 4) { int y = i + dy[k], x = j + dx[k]; if (s[y][x] != '#') { ++cnt; if (dista[y][x] == -1) { dista[y][x] = dista[i][j] + 1; que.emplace(y, x); } } } if (cnt >= 3) chmin(ans, dist[rb][cb] * 2 + (dista[i][j] + 1) * 4); } vector distb(h, vector(w, -1)); distb[rb][cb] = 0; que.emplace(rb, cb); while (!que.empty()) { auto [i, j] = que.front(); que.pop(); int cnt = 0; REP(k, 4) { int y = i + dy[k], x = j + dx[k]; if (s[y][x] != '#') { ++cnt; if (distb[y][x] == -1) { distb[y][x] = distb[i][j] + 1; que.emplace(y, x); } } } if (cnt >= 3) chmin(ans, dist[rb][cb] * 2 + (distb[i][j] + 1) * 4); } cout << (ans == INF ? -1 : ans) << '\n'; return 0; }