結果

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

ソースコード

diff #

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

vector<int> dx={1,0,-1,0},dy={0,1,0,-1};

int main() {

    cin.tie(nullptr);
    ios::sync_with_stdio(false);

    ll N,M,K;
    cin>>N>>M>>K;
    vector<vector<ll>> A(N,vector<ll>(M));
    set<ll> S;
    S.insert(1e9);
    for(int i=0;i<N;i++)for(int j=0;j<M;j++){
        cin>>A[i][j];
        S.insert(A[i][j]);
    }
    vector<ll> V;
    for(auto s:S)V.push_back(s);
    ll VN=V.size();
    ll L=0,R=VN;
    while(R-L>1){
        ll md=(R+L)/2;
        ll mid=V[md];
        vector<vector<int>> DP(N,vector<ll>(M,1e9));
        deque<pair<ll,pair<int,int>>> PQ;
        DP[0][0]=(A[0][0]<mid);
        PQ.push_back({DP[0][0],{0,0}});
        vector<vector<bool>> seen(N,vector<bool>(M,0));
        while(!PQ.empty()){
            ll c=PQ.front().first;
            ll y=PQ.front().second.first;
            ll x=PQ.front().second.second;
            PQ.pop_front();
            if(seen[y][x])continue;
            seen[y][x]=1;
            for(int d=0;d<4;d++){
                int ny=y+dy[d];
                int nx=x+dx[d];
                if(ny<0||nx<0||ny>=N||nx>=M)continue;
                if(seen[ny][nx])continue;
                ll nc=c+(A[ny][nx]<mid);
                if(DP[ny][nx]<=nc)continue;
                DP[ny][nx]=nc;
                if(nc==c)PQ.push_front({nc,{ny,nx}});
                else PQ.push_back({nc,{ny,nx}});
            }
        }
        if(DP[N-1][M-1]<=K)L=md;
        else R=md;
    }
    cout<<V[L]<<endl;
}
0