結果

問題 No.3063 幅優先探索
ユーザー veqccveqcc
提出日時 2020-04-01 22:23:17
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 29 ms / 2,000 ms
コード長 1,455 bytes
コンパイル時間 1,057 ms
コンパイル使用メモリ 115,544 KB
実行使用メモリ 8,644 KB
最終ジャッジ日時 2023-09-09 18:57:29
合計ジャッジ時間 1,830 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 2 ms
4,380 KB
testcase_04 AC 2 ms
4,376 KB
testcase_05 AC 1 ms
4,380 KB
testcase_06 AC 29 ms
8,404 KB
testcase_07 AC 29 ms
8,644 KB
testcase_08 AC 1 ms
4,380 KB
testcase_09 AC 2 ms
4,376 KB
testcase_10 AC 2 ms
4,376 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 (ny < 0 || nx < 0) continue;
            if (ny >= r || nx >= c) continue;
            // 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