結果

問題 No.3063 幅優先探索
ユーザー guriceringuricerin
提出日時 2020-04-02 18:11:47
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 30 ms / 2,000 ms
コード長 2,116 bytes
コンパイル時間 2,425 ms
コンパイル使用メモリ 208,988 KB
実行使用メモリ 8,400 KB
最終ジャッジ日時 2023-09-11 00:44:08
合計ジャッジ時間 2,980 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ(β)

テストケース

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

ソースコード

diff #

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

#define rep(i, a, b) for (int i = (a); i < (int)(b); i++)
#define rrep(i, a, b) for (int i = (a); i >= (int)(b); i--)
#define all(x) (x).begin(), (x).end()
using i32 = int32_t;
using i64 = int64_t;
using f32 = float;
using f64 = double;
using P = pair<int, int>;

template <class T>
bool chmin(T& a, T b) {
    if (a > b) {
        a = b;
        return true;
    } else {
        return false;
    }
}
template <class T>
bool chmax(T& a, T b) {
    if (a < b) {
        a = b;
        return true;
    } else {
        return false;
    }
}

template <class T>
void dump_vec(const vector<T>& v) {
    auto len = v.size();
    rep(i, 0, len) {
        cout << v[i] << (i == (int)len - 1 ? "\n" : " ");
    }
}

struct FastIO {
    FastIO() {
        cin.tie(0);
        ios::sync_with_stdio(false);
        cout << fixed << setprecision(20);
    }
} FASTIO;

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

signed main() {
    int R, C;
    cin >> R >> C;
    int sx, sy, gx, gy;
    cin >> sy >> sx >> gy >> gx;
    sy--;
    sx--;
    gy--;
    gx--;
    vector<string> maze(R);
    rep(i, 0, R) {
        string s;
        cin >> s;
        maze[i] = s;
    }

    int inf = (int)(1e9);
    vector<vector<int>> dist(R, vector<int>(C, inf));
    dist[sy][sx] = 0;
    queue<P> q;
    q.push(P(sy, sx));
    while (!q.empty()) {
        auto p = q.front();
        q.pop();
        int y = p.first, x = p.second;
        if (y == gy && x == gx) break;
        rep(i, 0, 4) {
            int ny = y + dy[i], nx = x + dx[i];
            if (!(0 <= ny && ny < R && 0 <= nx && nx < C)) {
                continue;
            }
            // if (maze[ny][nx] == '#') continue;
            int cost = dist[y][x] + 1;
            if (chmin(dist[ny][nx], cost)) {
                q.push(P(ny, nx));
            }
        }
    }
    cout << dist[gy][gx] << "\n";
}
0