結果

問題 No.1323 うしらずSwap
ユーザー milanis48663220milanis48663220
提出日時 2020-12-20 15:00:47
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 5,108 bytes
コンパイル時間 1,064 ms
コンパイル使用メモリ 104,468 KB
実行使用メモリ 270,056 KB
最終ジャッジ日時 2023-10-21 10:41:48
合計ジャッジ時間 27,138 ms
ジャッジサーバーID
(参考情報)
judge13 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 35 ms
90,116 KB
testcase_01 WA -
testcase_02 WA -
testcase_03 WA -
testcase_04 AC 35 ms
90,116 KB
testcase_05 WA -
testcase_06 WA -
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
testcase_10 AC 35 ms
90,116 KB
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
testcase_20 WA -
testcase_21 WA -
testcase_22 WA -
testcase_23 WA -
testcase_24 WA -
testcase_25 WA -
testcase_26 WA -
testcase_27 WA -
testcase_28 WA -
testcase_29 WA -
testcase_30 WA -
testcase_31 WA -
testcase_32 WA -
testcase_33 AC 1,121 ms
216,660 KB
testcase_34 AC 1,190 ms
235,796 KB
testcase_35 AC 1,327 ms
245,332 KB
testcase_36 AC 1,190 ms
251,052 KB
testcase_37 AC 1,203 ms
254,144 KB
testcase_38 AC 1,358 ms
267,436 KB
testcase_39 AC 1,363 ms
268,764 KB
testcase_40 AC 1,380 ms
269,444 KB
testcase_41 AC 1,384 ms
270,056 KB
testcase_42 AC 1,135 ms
269,972 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 <iostream>
#include <algorithm>
#include <iomanip>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <cassert>

#define debug_value(x) cerr << "line" << __LINE__ << ":<" << __func__ << ">:" << #x << "=" << x << endl;
#define debug(x) cerr << "line" << __LINE__ << ":<" << __func__ << ">:" << x << endl;

template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }

using namespace std;
typedef long long ll;

const int INF = 1e+9;

typedef pair<int, int> P;

struct edge{
    int to;
    int cost;
};

void dijkstra(int s, int num_v, vector<edge> G[], int d[]){
    priority_queue<P, vector<P>, greater<P>> que;
    fill(d, d+num_v, INF);
    d[s] = 0;
    que.push(P(0, s));
    while(!que.empty()){
        P p = que.top();que.pop();
        int v = p.second;
        if(d[v] < p.first) continue;
        for(int i = 0; i < G[v].size(); i++){
            edge e = G[v][i];
            if(d[v] + e.cost < d[e.to]){
                d[e.to] = d[v] + e.cost;
                que.push(P(d[e.to], e.to));
            }
        }
    }
}

int h, w, ra, ca, rb, cb;
char s[1500][1500];

int dh[4] = {0, 0, -1, 1};
int dw[4] = {-1, 1, 0, 0};

bool is_in_field(int i, int j){
    return i >= 0 && i < h && j >= 0 && j < w;
}

int to_idx(int i, int j){
    return i*w+j;
}

int to_i(int idx){
    return idx/w;
}

int to_j(int idx){
    return idx%w;
}

void input(){
    cin >> h >> w >> ra >> ca >> rb >> cb;
    ra--; ca--; rb--; cb--;
    for(int i = 0; i < h; i++){
        for(int j = 0; j < w; j++){
            cin >> s[i][j];
        }
    }
}

vector<edge> G[1333*1333];

int dist[1333*1333];

vector<P> get_shortest_path(){
    int cur_h = rb, cur_w = cb;
    vector<P> ans;
    ans.push_back(P(cur_h, cur_w));
    while(cur_h != ra || cur_w != ca){
        for(int i = 0; i < 4; i++){
            int h_to = cur_h+dh[i];
            int w_to = cur_w+dw[i];
            if(is_in_field(h_to, w_to) && dist[to_idx(h_to, w_to)] == dist[to_idx(cur_h, cur_w)]-1){
                cur_h = h_to;
                cur_w = w_to;
                break;
            }
        }
        ans.push_back(P(cur_h, cur_w));        
    }
    return ans;
}

vector<edge> G_rem[1333*1333];
bool used_in_path[1333][1333];

void make_g_rem(){
    for(int i = 0; i < h; i++){
        for(int j = 0; j < w; j++){
            if(s[i][j] == '#') continue;
            for(int k = 0; k < 4; k++){
                int i_to = i+dh[k], j_to = j+dw[k];
                if(is_in_field(i_to, j_to) && s[i_to][j_to] == '.'){
                    if(used_in_path[i][j] && used_in_path[i_to][j_to]) continue;
                    G_rem[to_idx(i, j)].push_back((edge){to_idx(i_to, j_to), 1});   
                } 
            }
        }
    }
}

int dist_rem[1333*1333];
void solve(){
    for(int i = 0; i < h; i++){
        for(int j = 0; j < w; j++){
            if(s[i][j] == '#') continue;
            for(int k = 0; k < 4; k++){
                int i_to = i+dh[k], j_to = j+dw[k];
                if(is_in_field(i_to, j_to) && s[i_to][j_to] == '.'){
                    G[to_idx(i, j)].push_back((edge){to_idx(i_to, j_to), 1});   
                } 
            }
        }
    }
    dijkstra(to_idx(ra, ca), h*w, G, dist);

    if(dist[to_idx(rb, cb)] == INF){
        cout << -1 << endl;
        return;
    }
    vector<P> path = get_shortest_path();
    for(auto p : path){
        used_in_path[p.first][p.second] = true;
        // cout << p.first << ',' << p.second << endl;
    }
    make_g_rem();
    // dijkstra(to_idx(ra, ca), h*w, G_rem, dist_rem);
    // cout << dist[to_idx(rb, cb)]+dist_rem[to_idx(rb, cb)] << endl;
    // return;

    // dijkstraっぽい感じ
    int min_diff = INF;
    priority_queue<P, vector<P>, greater<P>> que;
    fill(dist_rem, dist_rem+h*w, INF);
    for(auto p : path){
        int idx = to_idx(p.first, p.second);
        dist_rem[idx] = dist[idx];
        if(p.first == rb && p.second == cb) continue;
        que.push(P(dist[idx], idx));
    }
    while(!que.empty()){
        P p = que.top();que.pop();
        int v = p.second;
        if(dist_rem[v] < p.first) continue;
        for(int i = 0; i < G_rem[v].size(); i++){
            edge e = G_rem[v][i];
            if(dist_rem[v] + e.cost < dist_rem[e.to] && e.to != to_idx(rb, cb)){
                dist_rem[e.to] = dist_rem[v] + e.cost;
                que.push(P(dist_rem[e.to], e.to));
            }
            if(used_in_path[to_i(e.to)][to_j(e.to)] && e.to != to_idx(ra, ca)){
                chmin(min_diff, dist_rem[v] + e.cost - dist_rem[e.to]);
                // cout << to_i(v) << ',' << to_j(v) << ' ' << to_i(e.to) << ',' << to_j(e.to) << endl;
            }
        }
    }
    assert(min_diff >= 0);
    // if(min_diff == INF){
    //     cout << -1 << endl;
    // }else{
        cout << 2*dist[to_idx(rb, cb)] << endl;
    // }
}

int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout << setprecision(10) << fixed;
    input();
    solve();
}
0