結果

問題 No.2855 Move on Grid
ユーザー ryota2357
提出日時 2023-10-25 13:12:03
言語 C++17(clang)
(17.0.6 + boost 1.87.0)
結果
AC  
実行時間 188 ms / 3,000 ms
コード長 1,755 bytes
コンパイル時間 927 ms
コンパイル使用メモリ 129,152 KB
実行使用メモリ 6,948 KB
最終ジャッジ日時 2024-08-25 13:01:19
合計ジャッジ時間 7,397 ms
ジャッジサーバーID
(参考情報)
judge3 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 40
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <cassert>
#include <deque>
#include <iostream>
#include <tuple>
#include <vector>
using namespace std;

int main() {
    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];
        }
    }

    assert(2 <= n && n <= 500);
    assert(2 <= m && m <= 500);
    assert(0 <= k && k <= n * m);
    for (int i = 0; i < n; ++i) {
        for (int j = 0; j < m; ++j) {
            assert(1 <= a[i][j] && a[i][j] <= 1000000000);
        }
    }

    int l = 0, r = 1000000001;
    while (r - l > 1) {
        int mid = (l + r) / 2;

        deque<tuple<int, int, int>> que;
        vector cost(n, vector<int>(m, -1));

        const int dx[] = {1, 0, -1, 0};
        const int dy[] = {0, 1, 0, -1};

        que.emplace_back(0, 0, (a[0][0] < mid ? 1 : 0));
        while (que.size()) {
            auto [x, y, c] = que.front();
            que.pop_front();
            if (cost[y][x] != -1) {
                continue;
            }
            cost[y][x] = c;
            for (int i = 0; i < 4; ++i) {
                int nx = x + dx[i];
                int ny = y + dy[i];
                if (nx < 0 || nx >= m || ny < 0 || ny >= n) {
                    continue;
                }
                if (cost[ny][nx] != -1) {
                    continue;
                }
                if (a[ny][nx] < mid) {
                    que.emplace_back(nx, ny, c + 1);
                } else {
                    que.emplace_front(nx, ny, c);
                }
            }
        }

        if (cost[n - 1][m - 1] <= k) {
            l = mid;
        } else {
            r = mid;
        }
    }
    cout << l << endl;
    return 0;
}
0