結果

問題 No.1323 うしらずSwap
ユーザー chocoruskchocorusk
提出日時 2020-12-20 01:49:46
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 4,963 bytes
コンパイル時間 1,702 ms
コンパイル使用メモリ 148,516 KB
実行使用メモリ 129,056 KB
最終ジャッジ日時 2023-10-21 10:13:20
合計ジャッジ時間 17,187 ms
ジャッジサーバーID
(参考情報)
judge12 / judge9
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 3 ms
9,840 KB
testcase_01 AC 5 ms
15,276 KB
testcase_02 AC 5 ms
15,276 KB
testcase_03 AC 5 ms
15,276 KB
testcase_04 AC 5 ms
15,276 KB
testcase_05 AC 5 ms
15,276 KB
testcase_06 AC 5 ms
15,276 KB
testcase_07 AC 5 ms
15,276 KB
testcase_08 WA -
testcase_09 WA -
testcase_10 AC 4 ms
9,840 KB
testcase_11 WA -
testcase_12 AC 5 ms
15,276 KB
testcase_13 AC 5 ms
15,276 KB
testcase_14 WA -
testcase_15 AC 6 ms
15,276 KB
testcase_16 AC 589 ms
128,500 KB
testcase_17 AC 1,018 ms
128,508 KB
testcase_18 AC 552 ms
128,748 KB
testcase_19 AC 1,096 ms
128,336 KB
testcase_20 AC 586 ms
128,588 KB
testcase_21 AC 562 ms
128,988 KB
testcase_22 AC 1,035 ms
129,056 KB
testcase_23 AC 602 ms
128,924 KB
testcase_24 AC 1,062 ms
129,016 KB
testcase_25 AC 584 ms
128,924 KB
testcase_26 AC 380 ms
91,044 KB
testcase_27 AC 260 ms
77,244 KB
testcase_28 AC 142 ms
46,184 KB
testcase_29 WA -
testcase_30 WA -
testcase_31 WA -
testcase_32 WA -
testcase_33 AC 195 ms
24,744 KB
testcase_34 AC 173 ms
24,816 KB
testcase_35 AC 166 ms
26,472 KB
testcase_36 AC 155 ms
24,756 KB
testcase_37 AC 153 ms
24,756 KB
testcase_38 AC 138 ms
24,740 KB
testcase_39 AC 148 ms
26,460 KB
testcase_40 AC 145 ms
24,800 KB
testcase_41 AC 148 ms
24,732 KB
testcase_42 AC 134 ms
25,028 KB
testcase_43 AC 91 ms
46,112 KB
testcase_44 AC 113 ms
46,116 KB
testcase_45 AC 99 ms
46,160 KB
testcase_46 AC 137 ms
46,196 KB
testcase_47 AC 123 ms
46,200 KB
testcase_48 AC 195 ms
46,400 KB
testcase_49 AC 262 ms
46,412 KB
testcase_50 AC 154 ms
46,300 KB
testcase_51 AC 135 ms
46,396 KB
testcase_52 AC 157 ms
46,364 KB
testcase_53 AC 229 ms
46,312 KB
testcase_54 AC 126 ms
46,408 KB
testcase_55 AC 273 ms
46,404 KB
testcase_56 AC 147 ms
46,364 KB
testcase_57 AC 243 ms
46,300 KB
testcase_58 AC 6 ms
15,408 KB
testcase_59 AC 13 ms
43,232 KB
testcase_60 AC 7 ms
15,408 KB
testcase_61 WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <cstdio>
#include <cstring>
#include <iostream>
#include <string>
#include <cmath>
#include <bitset>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <deque>
#include <algorithm>
#include <complex>
#include <unordered_map>
#include <unordered_set>
#include <random>
#include <cassert>
#include <fstream>
#include <utility>
#include <functional>
#include <time.h>
#include <stack>
#include <array>
#include <list>
#define popcount __builtin_popcount
using namespace std;
typedef long long ll;
typedef pair<int, int> P;

int main()
{
    int h, w;
    int ra, ca, rb, cb;
    cin>>h>>w>>ra>>ca>>rb>>cb;
    ra--;ca--;rb--;cb--;
    string s[1400];
    for(int i=0; i<h; i++){
        cin>>s[i];
    }
    queue<P> que;
    que.push({ra, ca});
    int d[1400][1400];
    const int INF=1e8;
    for(int i=0; i<h; i++){
        for(int j=0; j<w; j++){
            d[i][j]=INF;
        }
    }
    d[ra][ca]=0;
    int dx[4]={1, -1, 0, 0}, dy[4]={0, 0, 1, -1};
    while(!que.empty()){
        P p=que.front(); que.pop();
        int x=p.first, y=p.second;
        for(int k=0; k<4; k++){
            int x1=x+dx[k], y1=y+dy[k];
            if(s[x1][y1]!='#' && d[x1][y1]>d[x][y]+1){
                que.push({x1, y1});
                d[x1][y1]=d[x][y]+1;
            }
        }
    }
    int dr[1400][1400];
    for(int i=0; i<h; i++){
        for(int j=0; j<w; j++){
            dr[i][j]=INF;
        }
    }
    dr[rb][cb]=0;
    que.push({rb, cb});
    while(!que.empty()){
        P p=que.front(); que.pop();
        int x=p.first, y=p.second;
        for(int k=0; k<4; k++){
            int x1=x+dx[k], y1=y+dy[k];
            if(s[x1][y1]!='#' && dr[x1][y1]>dr[x][y]+1){
                que.push({x1, y1});
                dr[x1][y1]=dr[x][y]+1;
            }
        }
    }
    vector<P> v;
    for(int i=0; i<h; i++){
        for(int j=0; j<w; j++){
            if(s[i][j]!='#' && d[i][j]+dr[i][j]==d[rb][cb]){
                v.push_back({i, j});
            }
        }
    }
    if(v.size()>d[rb][cb]+1){
        cout<<2*d[rb][cb]<<endl;
        return 0;
    }
    int ans=INF;
    for(auto p:v){
        int x=p.first, y=p.second;
        for(int k=0; k<4; k++){
            int x1=x+dx[k], y1=y+dy[k];
            if(s[x1][y1]!='#' && dr[x1][y1]==d[rb][cb]-d[x][y]){
                ans=min(ans, 2*d[rb][cb]+1);
            }
        }
    }
    bool dame[1400][1400]={};
    for(auto p:v){
        int x=p.first, y=p.second;
        dame[x][y]=1;
        int cnt=0;
        for(int k=0; k<4; k++){
            int x1=x+dx[k], y1=y+dy[k];
            if(s[x1][y1]!='#'){
                cnt++;
            }
        }
        if(!(x==ra && y==ca) && !(x==rb && y==rb) && cnt>2){
            ans=min(ans, 2*d[rb][cb]+2);
        }
    }
    int e[1400][1400];
    for(int i=0; i<h; i++){
        for(int j=0; j<w; j++){
            e[i][j]=INF;
        }
    }
    e[ra][ca]=0;
    que.push({ra, ca});
    while(!que.empty()){
        P p=que.front(); que.pop();
        int x=p.first, y=p.second;
        for(int k=0; k<4; k++){
            int x1=x+dx[k], y1=y+dy[k];
            if(s[x1][y1]!='#' && (!dame[x1][y1] || !dame[x][y]) && e[x1][y1]>e[x][y]+1){
                que.push({x1, y1});
                e[x1][y1]=e[x][y]+1;
            }
        }
    }
    int e1[1400][1400];
    for(int i=0; i<h; i++){
        for(int j=0; j<w; j++){
            e1[i][j]=INF;
        }
    }
    e1[ra][ca]=0;
    que.push({ra, ca});
    while(!que.empty()){
        P p=que.front(); que.pop();
        int x=p.first, y=p.second;
        for(int k=0; k<4; k++){
            int x1=x+dx[k], y1=y+dy[k];
            if(s[x1][y1]!='#' && !dame[x1][y1] && e1[x1][y1]>e1[x][y]+1){
                que.push({x1, y1});
                e1[x1][y1]=e1[x][y]+1;
            }
        }
    }
    map<int, int> mp;
    for(int i=0; i<h; i++){
        for(int j=0; j<w; j++){
            mp[e1[i][j]]++;
        }
    }
    int mn=INF;
    for(auto p:mp){
        if(p.second>1){
            mn=min(mn, p.first);
            break;
        }
    }
    int er[1400][1400];
    for(int i=0; i<h; i++){
        for(int j=0; j<w; j++){
            er[i][j]=INF;
        }
    }
    er[rb][cb]=0;
    que.push({rb, cb});
    while(!que.empty()){
        P p=que.front(); que.pop();
        int x=p.first, y=p.second;
        for(int k=0; k<4; k++){
            int x1=x+dx[k], y1=y+dy[k];
            if(s[x1][y1]!='#' && !dame[x1][y1] && er[x1][y1]>er[x][y]+1){
                que.push({x1, y1});
                er[x1][y1]=er[x][y]+1;
            }
        }
    }
    map<int, int> mp2;
    for(int i=0; i<h; i++){
        for(int j=0; j<w; j++){
            mp2[e[i][j]]++;
        }
    }
    for(auto p:mp2){
        if(p.second>1){
            mn=min(mn, p.first);
            break;
        }
    }
    ans=min({ans, 2*d[rb][cb]+4*mn, d[rb][cb]+e[rb][cb]});
    if(ans<INF) cout<<ans<<endl;
    else cout<<-1<<endl;
    return 0;
}
0