#include <bits/stdc++.h>
using namespace std;
typedef pair<int,int> pii;
const int MN=2002;
int N,M;
char grid[MN][MN];
set<pii> ol,nl;
string ans="";
int main() {
ios::sync_with_stdio(0);cin.tie(0);
cin>>N>>M;
string in;
for(int i=1;i<=N;i++) {
cin>>in;
for(int j=1;j<=M;j++)
grid[i][j]=in[j-1];
}
ol.insert({1,1});
ans+=grid[1][1];
for(int i=1;i<N+M-1;i++) {
char opt='|';
for(pii p:ol) {
char d=grid[p.first+1][p.second],r=grid[p.first][p.second+1];
if(p.first+1<=N) {
if(d<opt) {
opt=d;
nl.clear();
nl.insert({p.first+1,p.second});
} else if(d==opt) nl.insert({p.first+1,p.second});
}
if(p.second+1<=M) {
if(r<opt) {
opt=r;
nl.clear();
nl.insert({p.first,p.second+1});
} else if(r==opt) nl.insert({p.first,p.second+1});
}
}
ans+=opt;
ol=nl;
nl.clear();
}
cout<<ans<<'\n';
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
1236 KB |
Output is correct |
3 |
Correct |
0 ms |
340 KB |
Output is correct |
4 |
Correct |
0 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
1236 KB |
Output is correct |
7 |
Correct |
4 ms |
4308 KB |
Output is correct |
8 |
Correct |
10 ms |
8092 KB |
Output is correct |
9 |
Correct |
1 ms |
468 KB |
Output is correct |
10 |
Correct |
1 ms |
724 KB |
Output is correct |
11 |
Correct |
1 ms |
596 KB |
Output is correct |
12 |
Correct |
2 ms |
2516 KB |
Output is correct |
13 |
Correct |
3 ms |
4436 KB |
Output is correct |
14 |
Correct |
10 ms |
8124 KB |
Output is correct |
15 |
Correct |
2 ms |
596 KB |
Output is correct |
16 |
Correct |
86 ms |
4788 KB |
Output is correct |