結果

問題 No.971 いたずらっ子
ユーザー shibh308shibh308
提出日時 2019-12-03 23:09:06
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 849 ms / 2,000 ms
コード長 1,064 bytes
コンパイル時間 2,645 ms
コンパイル使用メモリ 209,768 KB
最終ジャッジ日時 2025-01-08 07:20:20
ジャッジサーバーID
(参考情報)
judge4 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 4
other AC * 21
権限があれば一括ダウンロードができます

ソースコード

diff #

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


signed main() {

    int h, w;
    cin >> h >> w;
    vector<string> a(h);
    for(auto& x : a)
        cin >> x;

    vector<vector<int>> dist(h, vector<int>(w, 1e9));
    dist[0][0] = 0;
    priority_queue<tuple<int,int,int>, vector<tuple<int,int,int>>, greater<tuple<int,int,int>>> que;
    que.emplace(0, 0, 0);
    vector<int> dx{0, 1};
    vector<int> dy{1, 0};

    while(!que.empty()){
        int d, x, y;
        tie(d, x, y) = que.top();
        que.pop();
        if(dist[x][y] != d)
            continue;
        for(int i = 0; i < 2; ++i){
            int nx = x + dx[i];
            int ny = y + dy[i];
            if(nx < 0 || ny < 0 || nx >= h || ny >= w)
                continue;
            int nd = d + 1;
            if(a[nx][ny] == 'k'){
                nd = d + 1 + (nx + ny);
            }
            if(nd < dist[nx][ny]){
                dist[nx][ny] = nd;
                que.emplace(nd, nx, ny);
            }
        }
    }
    cout << dist.back().back() << endl;
}
0