結果

問題 No.1323 うしらずSwap
ユーザー trineutrontrineutron
提出日時 2020-12-21 00:13:41
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 488 ms / 3,000 ms
コード長 3,893 bytes
コンパイル時間 2,233 ms
コンパイル使用メモリ 222,156 KB
実行使用メモリ 220,900 KB
最終ジャッジ日時 2024-04-27 08:57:12
合計ジャッジ時間 16,637 ms
ジャッジサーバーID
(参考情報)
judge3 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
6,816 KB
testcase_01 AC 1 ms
6,940 KB
testcase_02 AC 1 ms
6,944 KB
testcase_03 AC 1 ms
6,944 KB
testcase_04 AC 2 ms
6,944 KB
testcase_05 AC 2 ms
6,940 KB
testcase_06 AC 1 ms
6,944 KB
testcase_07 AC 1 ms
6,940 KB
testcase_08 AC 2 ms
6,944 KB
testcase_09 AC 1 ms
6,940 KB
testcase_10 AC 2 ms
6,944 KB
testcase_11 AC 1 ms
6,944 KB
testcase_12 AC 1 ms
6,944 KB
testcase_13 AC 1 ms
6,940 KB
testcase_14 AC 1 ms
6,940 KB
testcase_15 AC 2 ms
6,944 KB
testcase_16 AC 269 ms
220,448 KB
testcase_17 AC 400 ms
220,128 KB
testcase_18 AC 268 ms
220,756 KB
testcase_19 AC 404 ms
219,996 KB
testcase_20 AC 263 ms
220,412 KB
testcase_21 AC 262 ms
220,500 KB
testcase_22 AC 396 ms
220,660 KB
testcase_23 AC 270 ms
220,212 KB
testcase_24 AC 401 ms
220,384 KB
testcase_25 AC 266 ms
220,280 KB
testcase_26 AC 262 ms
220,828 KB
testcase_27 AC 258 ms
220,900 KB
testcase_28 AC 219 ms
199,136 KB
testcase_29 AC 242 ms
220,884 KB
testcase_30 AC 372 ms
220,632 KB
testcase_31 AC 244 ms
220,236 KB
testcase_32 AC 360 ms
220,620 KB
testcase_33 AC 488 ms
199,556 KB
testcase_34 AC 469 ms
199,936 KB
testcase_35 AC 430 ms
201,928 KB
testcase_36 AC 402 ms
202,108 KB
testcase_37 AC 405 ms
201,804 KB
testcase_38 AC 368 ms
201,232 KB
testcase_39 AC 373 ms
201,864 KB
testcase_40 AC 387 ms
201,684 KB
testcase_41 AC 406 ms
202,744 KB
testcase_42 AC 375 ms
201,400 KB
testcase_43 AC 164 ms
195,156 KB
testcase_44 AC 193 ms
194,852 KB
testcase_45 AC 188 ms
196,852 KB
testcase_46 AC 186 ms
195,144 KB
testcase_47 AC 176 ms
195,188 KB
testcase_48 AC 176 ms
195,144 KB
testcase_49 AC 207 ms
195,188 KB
testcase_50 AC 167 ms
194,260 KB
testcase_51 AC 137 ms
192,916 KB
testcase_52 AC 155 ms
193,480 KB
testcase_53 AC 203 ms
195,704 KB
testcase_54 AC 157 ms
193,604 KB
testcase_55 AC 224 ms
195,292 KB
testcase_56 AC 146 ms
193,668 KB
testcase_57 AC 221 ms
196,428 KB
testcase_58 AC 2 ms
6,944 KB
testcase_59 AC 3 ms
6,940 KB
testcase_60 AC 3 ms
6,940 KB
testcase_61 AC 2 ms
6,944 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)), 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 (d.at(i).at(j) - db.at(i).at(j) > 0) {
                v_back.at(d.at(i).at(j)).push_back(d_total);
            } else {
                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;
            if (x == ra and y == ca and newx == rb and newy == cb) 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;
    }
}
0