結果

問題 No.2064 Smallest Sequence on Grid
ユーザー Manuel1024Manuel1024
提出日時 2023-09-11 07:01:36
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 799 ms / 3,000 ms
コード長 1,286 bytes
コンパイル時間 877 ms
コンパイル使用メモリ 81,536 KB
実行使用メモリ 102,272 KB
最終ジャッジ日時 2023-09-11 07:01:46
合計ジャッジ時間 9,706 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 1 ms
4,380 KB
testcase_04 AC 1 ms
4,380 KB
testcase_05 AC 2 ms
4,376 KB
testcase_06 AC 2 ms
4,376 KB
testcase_07 AC 2 ms
4,376 KB
testcase_08 AC 2 ms
4,380 KB
testcase_09 AC 1 ms
4,380 KB
testcase_10 AC 2 ms
4,380 KB
testcase_11 AC 2 ms
4,380 KB
testcase_12 AC 2 ms
4,380 KB
testcase_13 AC 2 ms
4,376 KB
testcase_14 AC 2 ms
4,376 KB
testcase_15 AC 2 ms
4,380 KB
testcase_16 AC 2 ms
4,380 KB
testcase_17 AC 196 ms
54,320 KB
testcase_18 AC 196 ms
54,116 KB
testcase_19 AC 211 ms
54,364 KB
testcase_20 AC 556 ms
88,776 KB
testcase_21 AC 625 ms
92,104 KB
testcase_22 AC 439 ms
80,372 KB
testcase_23 AC 476 ms
78,204 KB
testcase_24 AC 508 ms
78,140 KB
testcase_25 AC 475 ms
78,184 KB
testcase_26 AC 799 ms
102,272 KB
testcase_27 AC 758 ms
102,152 KB
testcase_28 AC 196 ms
54,136 KB
testcase_29 AC 596 ms
88,712 KB
testcase_30 AC 179 ms
50,160 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
using namespace std;

int main(){
    int h, w; cin >> h >> w;
    vector<string> s(h);
    for(auto &it: s) cin >> it;

    vector<vector<vector<int>>> cnt(h+w, vector<vector<int>>(26));
    cnt[0][s[0][0]-'a'].emplace_back(0);
    vector<vector<int>> pre(h, vector<int>(w, -1));
    for(int phase = 0; phase < h+w-1; phase++){
        for(int x = 0; x < 26; x++){
            if(cnt[phase][x].size() == 0) continue;
            for(auto &it: cnt[phase][x]){
                int i = it/w, j = it%w;
                // cerr << i << " " << j << ", ";
                if(i+1 < h && pre[i+1][j] == -1){
                    cnt[phase+1][s[i+1][j]-'a'].emplace_back((i+1)*w+j);
                    pre[i+1][j] = i*w+j;
                }
                if(j+1 < w && pre[i][j+1] == -1){
                    cnt[phase+1][s[i][j+1]-'a'].emplace_back(i*w+j+1);
                    pre[i][j+1] = i*w+j;
                }
            }
            // cerr << endl;
            break;
        }
    }

    string ans = "";
    int cur = (h-1)*w+w-1;
    while(cur != -1){
        ans += s[cur/w][cur%w];
        cur = pre[cur/w][cur%w];
    }
    reverse(ans.begin(), ans.end());
    cout << ans << endl;
    return 0;
}
0