結果

問題 No.2855 Move on Grid
ユーザー srjywrdnprkt
提出日時 2024-10-15 14:52:06
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 131 ms / 3,000 ms
コード長 1,563 bytes
コンパイル時間 2,221 ms
コンパイル使用メモリ 206,772 KB
最終ジャッジ日時 2025-02-24 19:48:33
ジャッジサーバーID
(参考情報)
judge5 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 40
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

using namespace std;
using ll = long long;

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

    /*
       f(X)=Xより小さい数を通る回数がK回以下か?
       これのleft maxが答え。
    */

    int N, M, K;
    cin >> N >> M >> K;
    vector a(N, vector<int>(M));
    for (int i=0; i<N; i++){
        for (int j=0; j<M; j++){
            cin >> a[i][j];
        }
    }

    auto f=[&](int i, int j){
        return 0 <= i && i < N && 0 <= j && j < M;
    };
    int dx[4] = {0,1,0,-1};
    int dy[4] = {1,0,-1,0};
    const int inf=1e9;

    auto bfs=[&](ll X){
        int x, y, nx, ny, c, alt;
        deque<pair<int, int>> que;
        vector dist(N, vector<int>(M, inf));
        dist[0][0] = (a[0][0] < X);
        que.push_back({0, 0});
        while(!que.empty()){
            tie(x, y) = que.front();
            que.pop_front();
            for (int i=0; i<4; i++){
                nx = x+dx[i]; ny = y+dy[i];
                if (f(nx, ny)){
                    c = (a[nx][ny]<X);
                    alt = dist[x][y]+c;
                    if (alt < dist[nx][ny]){
                        dist[nx][ny] = alt;
                        if (c) que.push_back({nx, ny});
                        else que.push_front({nx, ny});
                    }
                }
            }
        }
        return dist[N-1][M-1] <= K;
    };

    ll l=0, r=1e9+1, c;
    while(r-l>1){
        c = (l+r)/2;
        if (bfs(c)) l=c;
        else r=c;
    }

    cout << l << endl;

    return 0;
}
0