結果

問題 No.2064 Smallest Sequence on Grid
ユーザー qqspeed20015
提出日時 2022-09-02 22:03:24
言語 C++14
(gcc 13.3.0 + boost 1.87.0)
結果
MLE  
実行時間 -
コード長 1,116 bytes
コンパイル時間 1,914 ms
コンパイル使用メモリ 181,784 KB
実行使用メモリ 814,976 KB
最終ジャッジ日時 2024-11-16 03:32:40
合計ジャッジ時間 28,245 ms
ジャッジサーバーID
(参考情報)
judge4 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other AC * 15 MLE * 14
権限があれば一括ダウンロードができます

ソースコード

diff #

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

vector <vector <bool>> isVisited;
vector <string> listMatrix;
vector <vector <string>> dp;
int H, W;

string solve(int i, int j) {
    string ans;
    if (i == H - 1 && j == W - 1) {
        ans = listMatrix[i][j];
        return ans;
    }
    if (isVisited[i][j])
        return dp[i][j];
    if (i == H - 1)
        ans = listMatrix[i][j] + solve(i, j + 1);
    else if (j == W - 1)
        ans = listMatrix[i][j] + solve(i + 1, j);
    else {
        string ans1 = solve(i, j + 1);
        string ans2 = solve(i + 1, j);
        if (ans1.compare(ans2) <= 0)
            ans = listMatrix[i][j] + ans1;
        else
            ans = listMatrix[i][j] + ans2;
    }
    isVisited[i][j] = true;
    return dp[i][j] = ans;
}

int main() {
    string str;
    scanf("%d %d", &H, &W);
    for (int i = 0; i < H; i++) {
        cin >> str;
        cin.ignore();
        listMatrix.push_back(str);
    }
    isVisited = vector <vector <bool>> (H, vector <bool> (W, false));
    dp = vector <vector <string>> (H, vector <string> (W));
    cout << solve(0, 0);
    return 0;
}
0