結果

問題 No.3063 幅優先探索
ユーザー y61mpnly61mpnl
提出日時 2020-04-01 23:24:52
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 1,155 bytes
コンパイル時間 666 ms
コンパイル使用メモリ 68,888 KB
実行使用メモリ 7,872 KB
最終ジャッジ日時 2023-09-09 20:13:43
合計ジャッジ時間 1,853 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include <cstdio>
#include <queue>
#include <string>
#include <vector>
#include <utility>

using namespace std;

static const int INF=1<<29;
static const int di[]={-1, 0, 1, 0};
static const int dj[]={0, -1, 0, 1};

int main() {
    int R, C, sy, sx, gy, gx;
    scanf("%d %d %d %d %d %d", &R, &C, &sy, &sx, &gy, &gx);
    --sy; --sx; --gy; --gx;

    vector<string> c(R);
    for (int i=0; i<R; ++i) {
        char buf[64];
        scanf("%s", buf);
        c[i] = buf;
    }

    vector<vector<int>> dp(R, vector<int>(C, INF)); dp[sy][sx]=0;

    queue<pair<int, int>> q; q.emplace(sy, sx);
    while (!q.empty()) {
        int i=q.front().first, j=q.front().second; q.pop();
        if (c[i][j] != '.') continue;

        for (int k=0; k<4; ++k) {
            int I=i+di[k], J=j+dj[k];
            if (I < 0 || R <= I || J < 0 || C <= J) continue;
            if (dp[I][J] < dp[i][j]) continue;
            if (c[I][J] != '.') continue;

            if (I == gy && J == gx)
                return !printf("%d\n", dp[i][j]+1);

            dp[I][J] = dp[i][j] + 1;
            c[i][j] = '+';
            q.emplace(I, J);
        }
    }

    return 0;
}
0