Submission #93940

#TimeUsernameProblemLanguageResultExecution timeMemory
93940ahmedie404Pohlepko (COCI16_pohlepko)C++14
15 / 80
1084 ms29816 KiB
#include <iostream>

using namespace std;
int n, m;
string a[2000+9];

bool check(int i, int j){
    return i >= 0 && j >= 0 && i < n && j < m;
}

string finds(int i, int j, string s){

    string sol="zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz";
    s += a[i][j];

    if( i == n-1 && j == m-1 )
        return s;


    if(check(i, j+1) && string(s+a[i][j+1]) < string(s+a[i+1][j]))
        sol = min(string(finds(i, j+1, s)), sol);
    else if(check(i+1, j) && string(s+a[i][j+1]) < string(s+a[i+1][j]))
        sol = min(string(finds(i+1, j, s)), sol);
    else{
        if(check(i+1, j))
            sol = min(string(finds(i+1, j, s)), sol);
        if(check(i, j+1))
            sol = min(string(finds(i, j+1, s)), sol);
    }

    return sol;

}

int main(){
    cin >> n >> m;
    for(int i=0;i<n;i++) cin >> a[i];

    cout << finds(0, 0, "") << endl;

    return 0;
}
#Verdict Execution timeMemoryGrader output
Fetching results...