結果

問題 No.1323 うしらずSwap
ユーザー trineutrontrineutron
提出日時 2020-12-20 03:33:52
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 2,454 bytes
コンパイル時間 2,644 ms
コンパイル使用メモリ 219,976 KB
実行使用メモリ 164,948 KB
最終ジャッジ日時 2023-10-21 10:19:12
合計ジャッジ時間 14,355 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,348 KB
testcase_01 AC 2 ms
4,348 KB
testcase_02 AC 2 ms
4,348 KB
testcase_03 AC 2 ms
4,348 KB
testcase_04 AC 2 ms
4,348 KB
testcase_05 WA -
testcase_06 AC 2 ms
4,348 KB
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
testcase_10 AC 2 ms
4,348 KB
testcase_11 WA -
testcase_12 WA -
testcase_13 AC 2 ms
4,348 KB
testcase_14 WA -
testcase_15 WA -
testcase_16 AC 192 ms
149,692 KB
testcase_17 AC 259 ms
148,060 KB
testcase_18 AC 188 ms
149,644 KB
testcase_19 AC 258 ms
148,116 KB
testcase_20 AC 187 ms
149,536 KB
testcase_21 WA -
testcase_22 WA -
testcase_23 WA -
testcase_24 WA -
testcase_25 WA -
testcase_26 WA -
testcase_27 WA -
testcase_28 AC 204 ms
149,784 KB
testcase_29 WA -
testcase_30 WA -
testcase_31 WA -
testcase_32 WA -
testcase_33 AC 349 ms
150,964 KB
testcase_34 AC 343 ms
151,504 KB
testcase_35 AC 323 ms
152,432 KB
testcase_36 AC 322 ms
153,924 KB
testcase_37 AC 328 ms
153,612 KB
testcase_38 AC 318 ms
153,116 KB
testcase_39 AC 305 ms
153,324 KB
testcase_40 AC 321 ms
154,408 KB
testcase_41 AC 325 ms
153,752 KB
testcase_42 AC 306 ms
153,000 KB
testcase_43 AC 152 ms
146,632 KB
testcase_44 AC 179 ms
146,576 KB
testcase_45 AC 177 ms
148,256 KB
testcase_46 AC 177 ms
146,764 KB
testcase_47 AC 162 ms
147,192 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 AC 3 ms
4,348 KB
testcase_60 AC 3 ms
4,348 KB
testcase_61 AC 2 ms
4,348 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#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));
    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);
        }
    }
    if (d.at(rb).at(cb) == 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);
    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;
            if (abs(d.at(i).at(j) - db.at(i).at(j)) < d.at(i).at(j)) {
                v.at(d.at(i).at(j) - db.at(i).at(j) + h * w).push_back(d.at(i).at(j) + db.at(i).at(j));
            } else {
                v_back.at(d.at(i).at(j)).push_back(d.at(i).at(j) + db.at(i).at(j));
            }
        }
    }
    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));
        }
    }
    if (ans == inf) {
        cout << -1 << endl;
    } else {
        cout << ans << endl;
    }
}
0