結果

問題 No.1323 うしらずSwap
ユーザー chocoruskchocorusk
提出日時 2020-12-20 00:32:17
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 2,856 bytes
コンパイル時間 3,277 ms
コンパイル使用メモリ 195,704 KB
実行使用メモリ 29,700 KB
最終ジャッジ日時 2023-10-21 09:53:48
合計ジャッジ時間 9,404 ms
ジャッジサーバーID
(参考情報)
judge13 / judge9
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,784 KB
testcase_01 AC 2 ms
5,784 KB
testcase_02 AC 2 ms
5,788 KB
testcase_03 AC 2 ms
5,784 KB
testcase_04 WA -
testcase_05 AC 1 ms
5,856 KB
testcase_06 AC 2 ms
5,856 KB
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
testcase_10 AC 1 ms
5,788 KB
testcase_11 WA -
testcase_12 WA -
testcase_13 AC 2 ms
5,784 KB
testcase_14 WA -
testcase_15 WA -
testcase_16 AC 70 ms
21,336 KB
testcase_17 AC 80 ms
21,336 KB
testcase_18 AC 71 ms
21,292 KB
testcase_19 AC 82 ms
21,356 KB
testcase_20 AC 69 ms
21,288 KB
testcase_21 WA -
testcase_22 WA -
testcase_23 WA -
testcase_24 WA -
testcase_25 WA -
testcase_26 AC 79 ms
23,000 KB
testcase_27 AC 84 ms
29,700 KB
testcase_28 AC 78 ms
21,268 KB
testcase_29 WA -
testcase_30 WA -
testcase_31 WA -
testcase_32 WA -
testcase_33 AC 176 ms
21,244 KB
testcase_34 AC 153 ms
21,324 KB
testcase_35 AC 143 ms
22,996 KB
testcase_36 AC 136 ms
21,300 KB
testcase_37 AC 135 ms
21,296 KB
testcase_38 AC 127 ms
21,240 KB
testcase_39 AC 131 ms
22,960 KB
testcase_40 AC 129 ms
21,332 KB
testcase_41 AC 125 ms
21,276 KB
testcase_42 AC 116 ms
21,560 KB
testcase_43 AC 55 ms
21,256 KB
testcase_44 AC 63 ms
21,256 KB
testcase_45 AC 66 ms
21,312 KB
testcase_46 AC 64 ms
21,256 KB
testcase_47 AC 58 ms
21,256 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 WA -
testcase_60 WA -
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>
#include <atcoder/all>
#define popcount __builtin_popcount
using namespace std;
using namespace atcoder;
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=1e9;
    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(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;
    }
    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]){
                cout<<2*d[rb][cb]+1<<endl;
                return 0;
            }
        }
    }
    for(auto p:v){
        int x=p.first, y=p.second;
        if(x==ra && y==ca) continue;
        if(x==rb && y==rb) continue;
        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(cnt>2){
            cout<<2*d[rb][cb]+2<<endl;
            return 0;
        }
    }
    cout<<-1<<endl;
    return 0;
}
0