結果

問題 No.1323 うしらずSwap
ユーザー tokusakuraitokusakurai
提出日時 2020-12-20 23:07:14
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 2,885 bytes
コンパイル時間 2,259 ms
コンパイル使用メモリ 223,132 KB
実行使用メモリ 26,752 KB
最終ジャッジ日時 2023-10-21 11:10:50
合計ジャッジ時間 7,171 ms
ジャッジサーバーID
(参考情報)
judge13 / judge10
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,348 KB
testcase_01 AC 1 ms
4,348 KB
testcase_02 AC 2 ms
4,348 KB
testcase_03 WA -
testcase_04 AC 2 ms
4,348 KB
testcase_05 WA -
testcase_06 WA -
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 WA -
testcase_17 WA -
testcase_18 AC 39 ms
26,712 KB
testcase_19 AC 68 ms
26,652 KB
testcase_20 WA -
testcase_21 WA -
testcase_22 WA -
testcase_23 WA -
testcase_24 WA -
testcase_25 WA -
testcase_26 AC 47 ms
26,712 KB
testcase_27 AC 51 ms
26,696 KB
testcase_28 WA -
testcase_29 WA -
testcase_30 WA -
testcase_31 WA -
testcase_32 WA -
testcase_33 AC 103 ms
26,520 KB
testcase_34 AC 109 ms
26,604 KB
testcase_35 AC 107 ms
26,636 KB
testcase_36 AC 105 ms
26,720 KB
testcase_37 AC 103 ms
26,644 KB
testcase_38 AC 105 ms
26,528 KB
testcase_39 AC 106 ms
26,604 KB
testcase_40 AC 109 ms
26,640 KB
testcase_41 AC 106 ms
26,724 KB
testcase_42 AC 101 ms
26,640 KB
testcase_43 WA -
testcase_44 WA -
testcase_45 WA -
testcase_46 WA -
testcase_47 WA -
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 WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for(int i = 0; i < n; i++)
#define rep2(i, x, n) for(int i = x; i <= n; i++)
#define rep3(i, x, n) for(int i = x; i >= n; i--)
#define each(e, v) for(auto &e: v)
#define pb push_back
#define eb emplace_back
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define sz(x) (int)x.size()
using ll = long long;
using pii = pair<int, int>;
using pil = pair<int, ll>;
using pli = pair<ll, int>;
using pll = pair<ll, ll>;
const int MOD = 1000000007;
//const int MOD = 998244353;
const int inf = (1<<30)-1;
const ll INF = (1LL<<60)-1;
template<typename T> bool chmax(T &x, const T &y) {return (x < y)? (x = y, true) : false;};
template<typename T> bool chmin(T &x, const T &y) {return (x > y)? (x = y, true) : false;};

struct io_setup{
    io_setup(){
        ios_base::sync_with_stdio(false);
        cin.tie(NULL);
        cout << fixed << setprecision(15);
    }
} io_setup;

int main(){
    int H, W;
    cin >> H >> W;
    int sx, sy, tx, ty;
    cin >> sx >> sy >> tx >> ty;
    sx--, sy--, tx--, ty--;
    vector<string> S(H);
    rep(i, H) cin >> S[i];

    queue<pii> que;
    vector<vector<int>> d(H, vector<int>(W, inf));
    vector<vector<pii>> pre(H, vector<pii>(W));
    
    que.emplace(sx, sy);
    d[sx][sy] = 0;
    vector<int> dx = {1, 0, -1, 0}, dy = {0, 1, 0, -1};
    vector<vector<bool>> tw(H, vector<bool>(W, false));
    
    while(!que.empty()){
        auto [i, j] = que.front(); que.pop();
        rep(k, 4){
            int ni = i+dx[k], nj = j+dy[k];
            if(S[ni][nj] == '#') continue;
            if(chmin(d[ni][nj], d[i][j]+1)){
                que.emplace(ni, nj);
                pre[ni][nj] = pii(i, j);
            }
            else if(d[ni][nj] == d[i][j]+1){
                tw[ni][nj] = true;
            }
        }
    }
    
    if(d[tx][ty] == inf) {cout << -1 << '\n'; return 0;}
    
    bool flag = false;
    int px = tx, py = ty;
    while(true){
        if(tw[px][py]) {cout << 2*d[tx][ty] << '\n'; return 0;}
        auto[ax, ay] = pre[px][py];
        px = ax, py = ay;
        if(px == sx && py == sy) break;
        int deg = 0;
        rep(k, 4){
            if(S[px+dx[k]][py+dy[k]] == '.') deg++;
        }
        if(deg >= 3) flag = true;
        S[px][py] = '#';
    }
        
    if(flag) {cout << 2*d[tx][ty]+2 << '\n'; return 0;}

    int ans = d[tx][ty];
    rep(i, H){
        fill(all(d[i]), inf);
    }
    d[sx][sy] = 0;
    que.emplace(sx, sy);

    while(!que.empty()){
        auto [i, j] = que.front(); que.pop();
        rep(k, 4){
            int ni = i+dx[k], nj = j+dy[k];
            if(S[ni][nj] == '#') continue;
            if(chmin(d[ni][nj], d[i][j]+1)){
                que.emplace(ni, nj);
            }
        }
    }

    if(d[tx][ty] == inf) {cout << -1 << '\n'; return 0;}
    cout << ans+d[tx][ty] << '\n';
}
0