結果

問題 No.3063 幅優先探索
ユーザー veqccveqcc
提出日時 2020-04-01 22:20:58
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
RE  
実行時間 -
コード長 1,349 bytes
コンパイル時間 1,117 ms
コンパイル使用メモリ 114,072 KB
実行使用メモリ 8,460 KB
最終ジャッジ日時 2023-09-09 18:49:23
合計ジャッジ時間 2,953 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include <functional>
#include <algorithm>
#include <iostream>
#include <iomanip>
#include <cstring>
#include <string>
#include <vector>
#include <random>
#include <bitset>
#include <queue>
#include <cmath>
#include <stack>
#include <set>
#include <map>
typedef long long ll;
using namespace std;
const ll MOD = 1000000007LL;
typedef pair <int, int> P;

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

int main() {
    cin.sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);

    int r, c;
    cin >> r >> c;

    int sy, sx, gy, gx;
    cin >> sy >> sx >> gy >> gx;
    sy--; sx--; gy--; gx--;

    vector <string> table(r);
    for (int i = 0; i < r; i++) cin >> table[i];

    vector<vector<int>> dp(r, vector<int>(c, r * c));
    dp[sy][sx] = 0;
    queue <P> q;

    q.push(P(sy * c + sx, 0));

    while (q.size()) {
        P p = q.front();
        q.pop();

        int y = p.first / c;
        int x = p.first % c;
        int n = p.second;
        if (dp[y][x] < n) continue;

        for (int i = 0; i < 4; i++) {
            int ny = y + dy[i];
            int nx = x + dx[i];
            if (table[ny][nx] == '#') continue;

            if (dp[ny][nx] > n + 1) {
                dp[ny][nx] = n + 1;
                q.push(P(ny * c + nx, n + 1));
            }
        }
    }

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