結果

問題 No.3063 幅優先探索
ユーザー FSMFSM
提出日時 2020-04-01 21:32:09
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 1,561 bytes
コンパイル時間 723 ms
コンパイル使用メモリ 78,736 KB
実行使用メモリ 7,432 KB
最終ジャッジ日時 2023-09-09 16:15:53
合計ジャッジ時間 1,485 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include<iostream>
#include<vector>
#include<string>
#include<climits>
#include<queue>
#include<utility>
 
 
using graph = std::vector<std::vector<int>>;
const int INF = INT_MAX;
const int NOT_SEARCHED = 100000;
int s[2], g[2];
int h, w;
const int dx[] = {0, 1, 0, -1};
const int dy[] = {1, 0, -1, 0};
 
void bfs(graph &meiro){
    std::queue<std::pair<int, int>> que;
    que.emplace(s[0], s[1]);
    meiro[s[0]][s[1]] = 0;
 
    while(!que.empty()){
        std::pair<int, int> now = que.front();
        que.pop();
        int y = now.first;
        int x = now.second;
        
        for(int i = 0; i < 4; i++){
            int next_x = x + dx[i];
            int next_y = y + dy[i];
            if(next_x < 0 || next_x >= w) continue;
            if(next_y < 0 || next_y >= h) continue;
            if(meiro[next_y][next_x] != INF){
                if(meiro[next_y][next_x] > meiro[y][x] + 1){
                    meiro[next_y][next_x] = meiro[y][x] + 1;
                    que.emplace(next_y, next_x);
                }
            }
        }
    }
}
 
int main(){
    std::cin >> h >> w;
    std::cin >> s[0] >> s[1];
    std::cin >> g[0] >> g[1];
    // to 0 origin
    s[0]--; s[1]--; g[0]--; g[1]--;
    graph meiro(h, std::vector<int>(w, INF));
    for(int i = 0; i < h; i++){
        for(int j = 0; j < w; j++){
            char c;
            std::cin >> c;
            if(c == '.'){
                meiro[i][j] = NOT_SEARCHED;
            }
        }
    }
 
    bfs(meiro);
 
    std::cout << meiro[g[0]][g[1]] << std::endl;
 
    return 0;
}
0