結果

問題 No.2913 二次元距離空間
ユーザー GOTKAKO
提出日時 2024-10-04 22:14:25
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 27 ms / 2,000 ms
コード長 1,261 bytes
コンパイル時間 2,359 ms
コンパイル使用メモリ 206,760 KB
最終ジャッジ日時 2025-02-24 15:22:23
ジャッジサーバーID
(参考情報)
judge4 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 28
権限があれば一括ダウンロードができます

ソースコード

diff #

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

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);

    int H,W; cin >> H >> W;
    vector<string> S(H);
    for(auto &s : S) cin >> s;
    vector<vector<pair<int,int>>> dist(H,vector<pair<int,int>>(W,{1001001001,-1}));
    dist.at(0).at(0) = {0,0};
    deque<pair<int,int>> Q; Q.push_back({0,0});
    vector<pair<int,int>> dxy = {{-1,0},{0,1},{1,0},{0,-1}};
    while(Q.size()){
        auto [x,y] = Q.front(); Q.pop_front();
        auto [cx,cy] = dist.at(x).at(y);
        for(auto [dx,dy] : dxy){
            if(dy != 0) cx++;
            else cy++;

            int nx = x+dx,ny = y+dy;
            if(nx < 0 || ny < 0 || nx >= H || ny >= W){}
            else if(S.at(nx).at(ny) != '#'){
                if(dist.at(nx).at(ny) > make_pair(cx,cy)){
                    dist.at(nx).at(ny) = {cx,cy};
                    if(dy) Q.push_back({nx,ny});
                    else Q.push_front({nx,ny});
                }
            }
            if(dy != 0) cx--;
            else cy--;
        }
    }
    if(dist.at(H-1).at(W-1).first != 1001001001){
        cout << "Yes\n";
        auto [X,Y] = dist.at(H-1).at(W-1);
        cout << X << " " << Y << endl;
    }  
    else cout << "No\n";
}
0