結果

問題 No.1323 うしらずSwap
ユーザー HaaHaa
提出日時 2020-12-20 23:23:32
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 2,081 bytes
コンパイル時間 2,144 ms
コンパイル使用メモリ 188,660 KB
実行使用メモリ 41,472 KB
最終ジャッジ日時 2023-10-21 11:12:42
合計ジャッジ時間 10,777 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 91 ms
41,436 KB
testcase_17 AC 133 ms
41,380 KB
testcase_18 AC 91 ms
41,472 KB
testcase_19 AC 128 ms
41,396 KB
testcase_20 AC 91 ms
41,416 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 145 ms
41,352 KB
testcase_29 WA -
testcase_30 WA -
testcase_31 WA -
testcase_32 WA -
testcase_33 AC 246 ms
41,028 KB
testcase_34 AC 271 ms
41,140 KB
testcase_35 AC 274 ms
41,248 KB
testcase_36 AC 270 ms
41,336 KB
testcase_37 AC 276 ms
41,224 KB
testcase_38 AC 303 ms
41,068 KB
testcase_39 AC 289 ms
41,172 KB
testcase_40 AC 300 ms
41,256 KB
testcase_41 AC 305 ms
41,372 KB
testcase_42 AC 269 ms
41,228 KB
testcase_43 AC 93 ms
41,384 KB
testcase_44 AC 123 ms
41,280 KB
testcase_45 AC 115 ms
41,468 KB
testcase_46 AC 122 ms
41,280 KB
testcase_47 AC 104 ms
41,364 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 2 ms
4,348 KB
testcase_61 WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef pair<ll,ll> P;
typedef vector<ll> VI;
typedef vector<VI> VVI;
#define REP(i,n) for(int i=0;i<n;i++)
#define ALL(v) v.begin(),v.end()
constexpr ll MOD=998244353;
constexpr ll INF=1e18;

struct xy {
    int x; int y; int d;
    bool operator<(const xy &another) const {return d > another.d;};
};
int dx[4] = { 1,0,-1,0 }, dy[4] = { 0,1,0,-1 };
int h, w;
vector<string> s(1333);

ll bfs(int sx, int sy, int gx, int gy){
    priority_queue<xy> q;
    q.push({sx,sy,0});
    VVI dis(h,VI(w,INF)); dis[sx][sy]=0;
    vector<vector<xy>> from(h,vector<xy>(w,xy{-1,-1}));
    vector<vector<bool>> two(h,vector<bool>(w,0));
    while (!q.empty()) {
        int x=q.top().x, y=q.top().y, d=q.top().d;
        q.pop();
        REP(k,4){
            int tox=x+dx[k], toy=y+dy[k];
            if(tox<0||tox>=h||toy<0||toy>=w)
                continue;
            if(s[tox][toy]=='#')
                continue;
            if(dis[tox][toy]>d+1){
                dis[tox][toy]=d+1;
                from[tox][toy]=xy{x,y};
                two[tox][toy]=two[x][y];
                q.push({tox,toy,d+1});
            }
            else if(dis[tox][toy]==d+1){
                two[tox][toy]=1;
            }
        }
    }
    if(dis[gx][gy]==INF)
        return -1;
    else if(two[gx][gy])
        return dis[gx][gy]*2;
    else{
        xy now={gx,gy};
        vector<vector<bool>> route(h,vector<bool>(w,0));
        while(now.x!=-1){
            route[now.x][now.y]=1;
            now=from[now.x][now.y];
        }
        REP(i,h)REP(j,w){
            if(route[i][j]){
                REP(k,4){
                    int tox=i+dx[k], toy=j+dy[k];
                    if(!route[tox][toy]&&s[tox][toy]=='.')
                        return dis[gx][gy]*2+2;
                }
            }
        }
        return -1;
    }
}

int main(){
    int sx, sy, gx, gy;
    cin >> h >> w >> sx >> sy >> gx >> gy;
    sx--, sy--, gx--, gy--;
    REP(i,h) cin >> s[i];
    cout << bfs(sx, sy, gx, gy) << endl;
    return 0;  
}
0