結果

問題 No.3063 幅優先探索
ユーザー s0j1sans0j1san
提出日時 2020-04-01 21:51:40
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
RE  
実行時間 -
コード長 1,470 bytes
コンパイル時間 853 ms
コンパイル使用メモリ 87,292 KB
実行使用メモリ 7,488 KB
最終ジャッジ日時 2023-09-09 17:57:41
合計ジャッジ時間 2,468 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include <iostream>
#include <set>
#include <queue>
#include <vector>
using namespace std;


int main() {

    int h, w;
    pair<int, int> startPos;
    pair<int, int> endPos;
    cin >> h >> w;

    cin >> startPos.first >> startPos.second;
    cin >> endPos.first >> endPos.second;

    startPos.first--;
    startPos.second--;

    endPos.first--;
    endPos.second--;

    vector<vector<bool>> maze(h, vector<bool>(w));
    vector<vector<int>> distance(h, vector<int>(w, -1));

    distance[startPos.first][startPos.second] = 0;

    for(int i = 0; i < h; ++i) {

        for(int j = 0; j < w; ++j) {
            char ch;

            cin >> ch;

            if(ch == '#')
                maze[i][j] = false;
            else
                maze[i][j] = true;

        }
    }



    queue<pair<int, int>> q;
    q.push(startPos);

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

    while(!q.empty()) {

        auto pos = q.front(); q.pop();

        int count = distance[pos.first][pos.second];

        if(pos == endPos)
            break;


        for(int i = 0; i < 4; ++i) {
            int x = pos.second + dx[i];
            int y = pos.first + dy[i];

            if(maze[y][x] && distance[y][x] == -1) {
                q.push(make_pair(y, x));
                distance[y][x] = count + 1;

            }
        }

    }

    
    cout << distance[endPos.first][endPos.second] << endl;

    return 0;


}
0