結果

問題 No.3063 幅優先探索
ユーザー horoyoisawahoroyoisawa
提出日時 2020-04-01 22:41:25
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 1,876 bytes
コンパイル時間 1,636 ms
コンパイル使用メモリ 176,080 KB
実行使用メモリ 15,232 KB
最終ジャッジ日時 2023-09-09 19:24:19
合計ジャッジ時間 2,535 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

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

int main() {
    int h, w;
    cin >> h >> w;
    vector<vector<int>> g(h+2, vector<int>(w+2, 1));
    vector<vector<int>> ans(h+2, vector<int>(w+2, 1001001001));
    vector<vector<int>> seen(h+2, vector<int>(w+2, 0));
    pair<int, int> start, goal;
    cin >> start.first >> start.second;
    cin >> goal.first >> goal.second;
    for(int i=1;i<=h;i++) for(int j=1;j<=w;j++) {
        char c;
        cin >> c;
        if(c == '.') g[i][j] = 0;
    }
    if(g[start.first][start.second]){
      cout << 0 << endl;
      return 0;
    }
    if(g[goal.first][goal.second]) {
      cout << 0 << endl;
      return 0;
    }
    queue<pair<int, int>> que;
    que.emplace(start);
    ans[start.first][start.second] = 0;
    while(!que.empty()) {
        pair<int, int> p = que.front();que.pop();
        if(seen[p.first][p.second]) continue;
        seen[p.first][p.second] = 1;
        int now = ans[p.first][p.second];
        if(!g[p.first][p.second+1] && ans[p.first][p.second+1] > now + 1) {
            ans[p.first][p.second+1] = now + 1;
            que.emplace(make_pair(p.first, p.second+1));
        }
        if(!g[p.first][p.second-1] && ans[p.first][p.second-1] > now + 1) {
            ans[p.first][p.second-1] = now + 1;
            que.emplace(make_pair(p.first, p.second-1));
        }
        if(!g[p.first-1][p.second] && ans[p.first-1][p.second] > now + 1) {
            ans[p.first-1][p.second] = now + 1;
            que.emplace(make_pair(p.first-1, p.second));
        }
        if(!g[p.first+1][p.second] && ans[p.first+1][p.second] > now + 1) {
            ans[p.first+1][p.second] = now + 1;
            que.emplace(make_pair(p.first+1, p.second));
        }
    }
    if(ans[goal.first][goal.second] == 1001001001) cout << 0 << endl;
    else cout << ans[goal.first][goal.second] << endl;
    return 0;
}
0