#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,us;
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'){
pos=us;
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'){
pos=us;
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 time | Memory | Grader output |
|---|
| Fetching results... |