結果

問題 No.2064 Smallest Sequence on Grid
ユーザー wanuiwanui
提出日時 2022-09-02 22:15:19
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 201 ms / 3,000 ms
コード長 2,445 bytes
コンパイル時間 2,080 ms
コンパイル使用メモリ 209,600 KB
実行使用メモリ 21,216 KB
最終ジャッジ日時 2024-04-27 21:49:59
合計ジャッジ時間 5,493 ms
ジャッジサーバーID
(参考情報)
judge5 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,812 KB
testcase_01 AC 2 ms
6,940 KB
testcase_02 AC 2 ms
6,940 KB
testcase_03 AC 1 ms
6,948 KB
testcase_04 AC 1 ms
6,944 KB
testcase_05 AC 2 ms
6,940 KB
testcase_06 AC 2 ms
6,944 KB
testcase_07 AC 1 ms
6,944 KB
testcase_08 AC 2 ms
6,940 KB
testcase_09 AC 2 ms
6,940 KB
testcase_10 AC 2 ms
6,944 KB
testcase_11 AC 2 ms
6,940 KB
testcase_12 AC 2 ms
6,944 KB
testcase_13 AC 2 ms
6,940 KB
testcase_14 AC 2 ms
6,944 KB
testcase_15 AC 2 ms
6,944 KB
testcase_16 AC 3 ms
6,944 KB
testcase_17 AC 89 ms
20,960 KB
testcase_18 AC 92 ms
21,084 KB
testcase_19 AC 88 ms
20,960 KB
testcase_20 AC 161 ms
21,088 KB
testcase_21 AC 170 ms
21,088 KB
testcase_22 AC 147 ms
21,088 KB
testcase_23 AC 145 ms
21,088 KB
testcase_24 AC 142 ms
21,216 KB
testcase_25 AC 143 ms
21,084 KB
testcase_26 AC 188 ms
21,084 KB
testcase_27 AC 201 ms
21,216 KB
testcase_28 AC 88 ms
20,964 KB
testcase_29 AC 163 ms
21,216 KB
testcase_30 AC 82 ms
20,580 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
// clang-format off
using namespace std; using ll=long long; using ull=unsigned long long; using pll=pair<ll,ll>; const ll INF=4e18;
void print0(){}; template<typename H,typename... T> void print0(H h,T... t){cout<<h;print0(t...);}
void print(){print0("\n");}; template<typename H,typename... T>void print(H h,T... t){print0(h);if(sizeof...(T)>0)print0(" ");print(t...);}
void perr0(){}; template<typename H,typename... T> void perr0(H h,T... t){cerr<<h;perr0(t...);}
void perr(){perr0("\n");}; template<typename H,typename... T>void perr(H h,T... t){perr0(h);if(sizeof...(T)>0)perr0(" ");perr(t...);}
void ioinit() { cout<<fixed<<setprecision(15); cerr<<fixed<<setprecision(6); ios_base::sync_with_stdio(0); cin.tie(0); }
#define rep(i,n) for(int i=0;i<(n);i++)
// clang-format on

using pii = pair<int, int>;
char S[3002][3002];
bool seen[3002][3002];
int h, w;
bool valid(pii x) {
    if (x.first >= h || x.second >= w || x.first < 0 || x.second < 0) return false;
    return true;
}
int main() {
    ioinit();

    cin >> h >> w;

    rep(i, h) {
        rep(j, w) {
            char c;
            cin >> c;
            S[i][j] = c;
        }
    }
    char terminal = S[h - 1][w - 1];

    string ans = {S[0][0]};
    vector<pii> cands = {{0, 0}};
    while (true) {
        vector<pii> ncands;
        char minchar = 'z' + 1;
        for (auto cand : cands) {
            pii na = {cand.first + 1, cand.second};
            pii nb = {cand.first, cand.second + 1};
            if (valid(na) && !seen[na.first][na.second]) {
                if (minchar > S[na.first][na.second]) {
                    minchar = S[na.first][na.second];
                    ncands = {na};
                } else if (minchar == S[na.first][na.second]) {
                    ncands.push_back(na);
                }
            }
            if (valid(nb) && !seen[nb.first][nb.second]) {
                if (minchar > S[nb.first][nb.second]) {
                    minchar = S[nb.first][nb.second];
                    ncands = {nb};
                } else if (minchar == S[nb.first][nb.second]) {
                    ncands.push_back(nb);
                }
            }
            seen[na.first][na.second] = true;
            seen[nb.first][nb.second] = true;
        }

        if (ncands.size() == 0) {
            break;
        }
        ans.push_back(minchar);
        swap(cands, ncands);
    }
    print(ans);
    return 0;
}
0