結果

問題 No.1323 うしらずSwap
ユーザー chocoruskchocorusk
提出日時 2020-12-20 01:03:05
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
RE  
実行時間 -
コード長 4,506 bytes
コンパイル時間 2,103 ms
コンパイル使用メモリ 142,224 KB
実行使用メモリ 29,660 KB
最終ジャッジ日時 2023-10-21 10:05:08
合計ジャッジ時間 16,387 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,752 KB
testcase_01 RE -
testcase_02 RE -
testcase_03 RE -
testcase_04 RE -
testcase_05 RE -
testcase_06 RE -
testcase_07 RE -
testcase_08 RE -
testcase_09 RE -
testcase_10 AC 2 ms
5,756 KB
testcase_11 RE -
testcase_12 RE -
testcase_13 RE -
testcase_14 RE -
testcase_15 RE -
testcase_16 RE -
testcase_17 RE -
testcase_18 RE -
testcase_19 RE -
testcase_20 RE -
testcase_21 RE -
testcase_22 RE -
testcase_23 RE -
testcase_24 RE -
testcase_25 RE -
testcase_26 RE -
testcase_27 RE -
testcase_28 RE -
testcase_29 RE -
testcase_30 RE -
testcase_31 RE -
testcase_32 RE -
testcase_33 AC 198 ms
21,204 KB
testcase_34 AC 176 ms
21,284 KB
testcase_35 AC 169 ms
22,956 KB
testcase_36 AC 157 ms
21,260 KB
testcase_37 AC 155 ms
21,256 KB
testcase_38 AC 141 ms
21,200 KB
testcase_39 AC 149 ms
22,920 KB
testcase_40 AC 146 ms
21,292 KB
testcase_41 AC 148 ms
21,236 KB
testcase_42 AC 137 ms
21,520 KB
testcase_43 RE -
testcase_44 RE -
testcase_45 RE -
testcase_46 RE -
testcase_47 RE -
testcase_48 RE -
testcase_49 RE -
testcase_50 RE -
testcase_51 RE -
testcase_52 RE -
testcase_53 RE -
testcase_54 RE -
testcase_55 RE -
testcase_56 RE -
testcase_57 RE -
testcase_58 RE -
testcase_59 RE -
testcase_60 RE -
testcase_61 RE -
権限があれば一括ダウンロードができます

ソースコード

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(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;
    }assert(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;
            }
        }
    }
    assert(0);
    bool dame[1400][1400]={};
    for(auto p:v){
        int x=p.first, y=p.second;
        if(!(x==ra && y==ca) && !(x==rb && y==rb))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){
            cout<<2*d[rb][cb]+2<<endl;
            return 0;
        }
    }
    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] && e[x1][y1]>e[x][y]+1){
                que.push({x1, y1});
                e[x1][y1]=e[x][y]+1;
            }
        }
    }
    map<int, int> mp;
    for(int i=0; i<h; i++){
        for(int j=0; j<w; j++){
            mp[e[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[er[i][j]]++;
        }
    }
    for(auto p:mp2){
        if(p.second>1){
            mn=min(mn, p.first);
            break;
        }
    }
    int ans=min(2*d[rb][cb]+2*mn, d[rb][cb]+e[rb][cb]);
    if(ans<INF) cout<<ans<<endl;
    else cout<<-1<<endl;
    return 0;
}
0