結果

問題 No.2096 Rage With Our Friends
ユーザー AngrySadEightAngrySadEight
提出日時 2022-09-14 23:41:06
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 286 ms / 5,000 ms
コード長 3,616 bytes
コンパイル時間 1,198 ms
コンパイル使用メモリ 92,060 KB
実行使用メモリ 43,264 KB
最終ジャッジ日時 2024-05-09 03:21:08
合計ジャッジ時間 4,910 ms
ジャッジサーバーID
(参考情報)
judge4 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 2 ms
5,376 KB
testcase_02 AC 2 ms
5,376 KB
testcase_03 AC 2 ms
5,376 KB
testcase_04 AC 2 ms
5,376 KB
testcase_05 AC 2 ms
5,376 KB
testcase_06 AC 2 ms
5,376 KB
testcase_07 AC 2 ms
5,376 KB
testcase_08 AC 2 ms
5,376 KB
testcase_09 AC 2 ms
5,376 KB
testcase_10 AC 2 ms
5,376 KB
testcase_11 AC 113 ms
43,136 KB
testcase_12 AC 114 ms
43,264 KB
testcase_13 AC 202 ms
43,008 KB
testcase_14 AC 286 ms
43,008 KB
testcase_15 AC 113 ms
43,264 KB
testcase_16 AC 2 ms
5,376 KB
testcase_17 AC 201 ms
43,008 KB
testcase_18 AC 2 ms
5,376 KB
testcase_19 AC 2 ms
5,376 KB
testcase_20 AC 2 ms
5,376 KB
testcase_21 AC 260 ms
43,136 KB
testcase_22 AC 266 ms
43,136 KB
testcase_23 AC 15 ms
5,504 KB
testcase_24 AC 160 ms
26,368 KB
testcase_25 AC 105 ms
18,688 KB
testcase_26 AC 57 ms
11,392 KB
testcase_27 AC 54 ms
11,648 KB
testcase_28 AC 3 ms
5,376 KB
testcase_29 AC 28 ms
7,296 KB
権限があれば一括ダウンロードができます

ソースコード

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, 0);
    vector<vector<int>> highest_x(H, vector<int>(W, 0));
    for (int i = 0; i < H; 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 (p.x == gx && p.y == gy){
            cout << dist[gx][gy] << endl;
            return 0;
        }
        if (isvisited[p.x][p.y]){
            continue;
        }
        isvisited[p.x][p.y] = true;
        
        //1回の移動で行ける範囲を探索
        if (p.y - 1 >= 0){
            int next_x = min(H - 1, 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 = min(H - 1, 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 = min(H - 1, 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 = -1 * (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[min(H - 1, 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 = min(H - 1, 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 = -1 * (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[min(H - 1, 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 = min(H - 1, 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);
        }
    }
    cout << -1 << endl;
    return 0;
}
0