結果

問題 No.3063 幅優先探索
ユーザー betrue12betrue12
提出日時 2020-04-01 22:09:26
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 914 bytes
コンパイル時間 2,335 ms
コンパイル使用メモリ 207,856 KB
実行使用メモリ 9,432 KB
最終ジャッジ日時 2023-09-09 18:26:38
合計ジャッジ時間 2,844 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 WA -
testcase_04 WA -
testcase_05 WA -
testcase_06 WA -
testcase_07 AC 48 ms
9,432 KB
testcase_08 AC 2 ms
4,376 KB
testcase_09 AC 1 ms
4,380 KB
testcase_10 AC 1 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;

int main(){
    int H, W;
    cin >> H >> W;
    int sx, sy, gx, gy;
    cin >> sx >> sy >> gx >> gy;
    sx--; sy--; gx--; gy--;
    string S[1000];
    for(int i=0; i<H; i++) cin >> S[i];

    auto inside = [&](int i, int j){ return i>=0 && i<H && j>=0 && j<W && S[i][j] != '#'; };

    int dx[] = {0, 0, -1, 1};
    int dy[] = {-1, 1, 0, 0};

    queue<pair<int, int>> que;
    que.emplace(sx, sy);
    vector<vector<int>> dist(H, vector<int>(W, 1e9));
    dist[sx][sy] = 0;
    while(que.size()){
        auto p = que.front(); que.pop();
        int i = p.first, j = p.second;
        for(int k=0; k<4; k++){
            int x = i+dx[k], y = j+dy[k];
            if(inside(x, y) && dist[x][y] == 1e9){
                dist[x][y] = dist[i][j] + 1;
                que.emplace(x, y);
            }
        }
    }

    cout << dist[gx][gy] << endl;
    return 0;
}
0