結果

問題 No.2096 Rage With Our Friends
ユーザー AngrySadEightAngrySadEight
提出日時 2022-09-13 20:41:02
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
RE  
(最新)
AC  
(最初)
実行時間 -
コード長 3,526 bytes
コンパイル時間 2,401 ms
コンパイル使用メモリ 91,532 KB
実行使用メモリ 11,172 KB
最終ジャッジ日時 2023-08-21 21:13:34
合計ジャッジ時間 8,577 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 RE -
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 RE -
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 -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <vector>
#include <string>
#include <deque>
#include <cassert>
using namespace std;

struct nodes{
    int x;
    int y;
};

int main(){
    //1. 入力を読む
    int H,W;
    cin >> H >> W;
    int sx, sy, gx, gy;
    cin >> sx >> sy >> gx >> gy;
    sx--;
    sy--;
    gx--;
    gy--;
    vector<string> S(H);
    for (int i = 0; i < H; i++){
        cin >> S[i];
    }
    assert(S[sx][sy] == '#');
    assert(S[gx][gy] == '#');
    
    //2. 全ての座標に対して,その真下にある座標で一番上にあるものがどこかのx座標を求める
    vector<int> calc_highest_x(W, H - 1);
    vector<vector<int>> highest_x(H, vector<int>(W, H - 1));
    for (int i = H - 2; i >= 0; i--){
        for (int j = 0; j < W; j++){
            if (S[i][j] == '#') calc_highest_x[j] = i;
            highest_x[i][j] = calc_highest_x[j];
        }
    }
    
    int INF = 1 << 24;

    //3. 01-BFSを行う
    vector<vector<bool>> isvisited(H, vector<bool>(W, false));
    vector<vector<int>> dist(H, vector<int>(W, INF));
    deque<nodes> dq;
    dq.push_front(nodes{sx, sy});
    dist[sx][sy] = 0;
    
    while(dq.size()){
        nodes p = dq.front();
        dq.pop_front();
        if (isvisited[p.x][p.y]){
            continue;
        }
        isvisited[p.x][p.y] = true;
        
        //1回の移動で行ける範囲を探索
        if (p.y - 1 >= 0){
            int next_x = max(0, p.x - 1);
            next_x = highest_x[next_x][p.y - 1];
            dq.push_front(nodes{next_x, p.y - 1});
            dist[next_x][p.y - 1] = min(dist[next_x][p.y - 1], dist[p.x][p.y]);
        }
        if (p.y + 1 < W){
            int next_x = max(0, p.x - 1);
            next_x = highest_x[next_x][p.y + 1];
            dq.push_front(nodes{next_x, p.y + 1});
            dist[next_x][p.y + 1] = min(dist[next_x][p.y + 1], dist[p.x][p.y]);
        }

        //2回の移動で行ける範囲を探索
        if (p.y - 2 >= 0){
            int next_x = max(0, p.x - 1);
            next_x = highest_x[next_x][p.y - 2];
            dq.push_back(nodes{next_x, p.y - 2});
            dist[next_x][p.y - 2] = min(dist[next_x][p.y - 2], dist[p.x][p.y] + 1);
        }
        if (p.y - 2 >= 0){
            int diffs_x = highest_x[p.x][p.y - 1] - p.x;
            int next_x = highest_x[p.x][p.y - 1] - 1 - diffs_x / 2;
            next_x = highest_x[max(0, next_x)][p.y - 2];
            dq.push_front(nodes{next_x, p.y - 2});
            dist[next_x][p.y - 2] = min(dist[next_x][p.y - 2], dist[p.x][p.y]);
        }
        if (p.y + 2 < W){
            int next_x = max(0, p.x - 1);
            next_x = highest_x[next_x][p.y + 2];
            dq.push_back(nodes{next_x, p.y + 2});
            dist[next_x][p.y + 2] = min(dist[next_x][p.y + 2], dist[p.x][p.y] + 1);
        }
        if (p.y + 2 < W){
            int diffs_x = highest_x[p.x][p.y + 1] - p.x;
            int next_x = highest_x[p.x][p.y + 1] - 1 - diffs_x / 2;
            next_x = highest_x[max(0, next_x)][p.y + 2];
            dq.push_front(nodes{next_x, p.y + 2});
            dist[next_x][p.y + 2] = min(dist[next_x][p.y + 2], dist[p.x][p.y]);
        }
        if (p.y >= 0 && p.y < W){
            int next_x = max(0, p.x - 1);
            next_x = highest_x[next_x][p.y];
            dq.push_back(nodes{next_x, p.y});
            dist[next_x][p.y] = min(dist[next_x][p.y], dist[p.x][p.y] + 1);
        }
    }
    if (dist[gx][gy] == INF) cout << -1 << endl;
    else cout << dist[gx][gy] << endl;
}
0