Submission #1312685

#TimeUsernameProblemLanguageResultExecution timeMemory
1312685rodolfovertoPohlepko (COCI16_pohlepko)C++20
65 / 80
1096 ms131072 KiB
#include <bits/stdc++.h>

#define space ' '
#define endl '\n'
#define F first
#define S second
#define int long long
#define pd pair <int,int>

using namespace std;

int32_t main(){
    int n,m;
    cin >> n >> m;
    vector< vector <char> >mat(n,vector <char>(m));
    for(int i=0; i<n; i++){
        for(int j=0; j<m; j++){
            cin >> mat[i][j];
        }
    }
    queue<pd>q;
    string res;
    res.push_back(mat[0][0]);
    q.push({0,0});
    while(q.front().F != n-1 || q.front().S != m-1){
        stack<pd>pos;
        while(!q.empty()){
            int x,y;
            x=q.front().F;
            y=q.front().S;
            q.pop();
            if(x+1 != n){
                if(pos.empty() || mat[pos.top().F][pos.top().S] == mat[x+1][y]){
                    pos.push({x+1,y});
                }else if(mat[pos.top().F][pos.top().S]-'a'>mat[x+1][y]-'a'){
                    while(!pos.empty()){
                        pos.pop();
                    }
                    pos.push({x+1,y});
                }
            }
            if(y+1 != m){
                if(pos.empty() || mat[pos.top().F][pos.top().S] == mat[x][y+1]){
                    pos.push({x,y+1});
                }else if(mat[pos.top().F][pos.top().S]-'a'>mat[x][y+1]-'a'){
                    while(!pos.empty()){
                        pos.pop();
                    }
                    pos.push({x,y+1});
                }
            }
        }
        res.push_back(mat[pos.top().F][pos.top().S]);
        while(!pos.empty()){
            q.push(pos.top());
            pos.pop();
        }
    }
    cout << res << endl;
    return 0;
}

#Verdict Execution timeMemoryGrader output
Fetching results...