結果

問題 No.3063 幅優先探索
ユーザー mmn15277198mmn15277198
提出日時 2021-04-18 16:28:43
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 1,625 bytes
コンパイル時間 1,700 ms
コンパイル使用メモリ 175,888 KB
実行使用メモリ 9,488 KB
最終ジャッジ日時 2023-09-17 07:48:30
合計ジャッジ時間 2,352 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
int main() {
    int h , w;
    cin >> h >> w;
    int y1 , x1;
    cin >> y1 >> x1;
    y1--;
    x1--;
    int y2 , x2;
    cin >> y2 >> x2;
    y2--;
    x2--;
    vector<string> s(h);
    for (int i = 0 ; i < h; i++) cin >> s[i];
    queue<int> qx;
    queue<int> qy;
    qx.push(x1);
    qy.push(y1);
    s[y1][x1] = 'A';
    vector<vector<int>> a(h , vector<int>(w , 0));
    while (!qx.empty()) {
        int x = qx.front();
        int y = qy.front();
        qx.pop();
        qy.pop();
        if (x == x2 && y == y2) {
            //cout << a[y][x] << endl;
            cout << abs(x1 - x2) + abs(y1 - y2) << endl;
            return 0;
            //break;
        }
        if (x + 1 < w && s[y][x + 1] == '.') {
            qx.push(x + 1);
            qy.push(y);
            s[y][x + 1] = 'A';
            a[y][x + 1] = a[y][x] + 1;
        }
        if (x - 1 >= 0 && s[y][x - 1] == '.') {
            qx.push(x - 1);
            qy.push(y);
            s[y][x - 1] = 'A';
            a[y][x - 1] = a[y][x] + 1;
        }
        if (y + 1 < h && s[y + 1][x] == '.') {
            qx.push(x);
            qy.push(y + 1);
            s[y + 1][x] = 'A';
            a[y + 1][x] = a[y][x] + 1;
        }
        if (y - 1 >= 0 && s[y - 1][x] == '.') {
            qx.push(x);
            qy.push(y - 1);
            s[y - 1][x] = 'A';
            a[y - 1][x] = a[y][x] + 1;
        }
    }
    /*
    for (int i = 0; i < h; i++) {
        for (int j = 0; j < w; j++) {
            cout << a[i][j] << " ";
        }
        cout << endl;
    }
    */
    return 0;
}
0