結果

問題 No.2855 Move on Grid
コンテスト
ユーザー zjsdut
提出日時 2025-11-28 00:06:25
言語 C++23
(gcc 13.3.0 + boost 1.89.0)
結果
AC  
実行時間 141 ms / 3,000 ms
コード長 1,314 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 2,999 ms
コンパイル使用メモリ 286,380 KB
実行使用メモリ 7,848 KB
最終ジャッジ日時 2025-11-28 00:06:34
合計ジャッジ時間 8,856 ms
ジャッジサーバーID
(参考情報)
judge4 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 40
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

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

int dir[4][2] = {1, 0, -1, 0, 0, 1, 0, -1};

int main() {
    int n, m, k;
    cin >> n >> m >> k;
    vector<vector<int>> a(n, vector<int>(m));
    for (int i = 0; i < n; i++)
        for (int j = 0; j < m; j++)
            cin >> a[i][j];
    
    auto check = [&](int x) {
        vector<vector<int>> d(n, vector<int>(m, -1));
        deque<pair<int,int>> q; // 双端队列
        q.push_back({0, 0});
        d[0][0] = a[0][0] < x;
        while (!q.empty()) {
            auto p = q.front();
            q.pop_front();
            for (int i = 0; i < 4; i++) {
                int r = p.first + dir[i][0];
                int c = p.second + dir[i][1];
                if (0 <= r && r < n && 0 <= c && c < m && d[r][c] == -1) {
                    d[r][c] = d[p.first][p.second] + (a[r][c] < x);
                    if (a[r][c] < x) {
                        q.push_back({r, c});
                    } else {
                        q.push_front({r, c});
                    }
                }
            }
        }
        return d[n - 1][m - 1] <= k;
    };
    
    int ok = 1, ng = 1e9 + 1;
    while (ng - ok > 1) {
        int x = (ok + ng) / 2;
        if (check(x))
            ok = x;
        else
            ng = x;
    }
    cout << ok << '\n';
}
0