結果

問題 No.2855 Move on Grid
ユーザー t98slider
提出日時 2024-08-25 14:04:47
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
TLE  
実行時間 -
コード長 1,666 bytes
コンパイル時間 2,501 ms
コンパイル使用メモリ 216,004 KB
最終ジャッジ日時 2025-02-24 01:04:03
ジャッジサーバーID
(参考情報)
judge1 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 21 TLE * 3 -- * 16
権限があれば一括ダウンロードができます

ソースコード

diff #

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

template<class T> istream& operator >> (istream& is, vector<T>& vec) {
    for(T& x : vec) is >> x;
    return is;
}

template<class T> ostream& operator << (ostream& os, const vector<T>& vec) {
    if(vec.empty()) return os;
    os << vec[0];
    for(auto it = vec.begin(); ++it != vec.end(); ) os << ' ' << *it;
    return os;
}

int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    int h, w, k;
    cin >> h >> w >> k;
    if(k > h + w){
        cout << 1'000'000'000 << '\n';
        return 0;
    }
    k = min(k, h + w);
    vector dp(h, vector(w, vector<ll>(k + 1, -1'000'000'003)));
    vector A(h, vector<ll>(w));
    cin >> A;
    priority_queue<tuple<ll,int,int,int>> pq;
    dp[0][0][0] = A[0][0];
    pq.emplace(A[0][0], 0, 0, 0);
    if(k >= 1){
        dp[0][0][1] = 1'000'000'000;
        pq.emplace(1'000'000'000, 0, 0, 1);
    }
    vector<pair<int,int>> dir = {{0, 1}, {1, 0}, {0, -1}, {-1, 0}};
    while(!pq.empty()){
        auto [d, y, x, s] = pq.top();
        pq.pop();
        if(d < dp[y][x][s]) continue;
        for(auto [ny, nx] : dir){
            ny += y, nx += x;
            if(ny < 0 || nx < 0 || ny >= h || nx >= w) continue;
            if(min(d, A[ny][nx]) > dp[ny][nx][s]){
                dp[ny][nx][s] = min(d, A[ny][nx]);
                pq.emplace(dp[ny][nx][s], ny, nx, s);
            }
            if(s + 1 <= k && d > dp[ny][nx][s + 1]){
                dp[ny][nx][s + 1] = d;
                pq.emplace(d, ny, nx, s + 1);
            }
        }
    }
    cout << *max_element(dp[h - 1][w - 1].begin(), dp[h - 1][w - 1].end()) << '\n';
}
0