#include <bits/stdc++.h>
#define all(x) x.begin(),x.end()
#define sz(x) ((int)x.size())
#define i2 array<int,2>
#define PB push_back
using namespace std;
const int N = 2010;
const int oo = 2e9;
vector<i2> vc, nw;
char c[N][N];
int n, m;
string s = "";
int main(){
ios_base::sync_with_stdio(0); cin.tie(0);
#ifdef _LOCAL
freopen("in.txt","r",stdin);
#endif // _LOCAL
cin >> n >> m;
for (int i = 0; i < n; i++)
for (int j = 0; j < m; j++)
cin >> c[i][j];
vc.PB({0, 0});
while (sz(vc) > 0){
char mn = 'z';
nw.clear();
for (i2 cr : vc)
mn = min(mn, c[cr[0]][cr[1]]);
for (i2 cr : vc){
int x = cr[0], y = cr[1];
if (c[x][y] != mn) continue;
if (x < n - 1)
nw.PB({x + 1, y});
if (y < m - 1)
nw.PB({x, y + 1});
}
s += mn;
sort(all(nw));
nw.resize(unique(all(nw)) - nw.begin());
vc = nw;
}
cout << s;
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
384 KB |
Output is correct |
2 |
Correct |
5 ms |
1280 KB |
Output is correct |
3 |
Correct |
5 ms |
384 KB |
Output is correct |
4 |
Correct |
5 ms |
512 KB |
Output is correct |
5 |
Correct |
5 ms |
384 KB |
Output is correct |
6 |
Correct |
8 ms |
1280 KB |
Output is correct |
7 |
Correct |
33 ms |
4344 KB |
Output is correct |
8 |
Correct |
62 ms |
8184 KB |
Output is correct |
9 |
Correct |
5 ms |
512 KB |
Output is correct |
10 |
Correct |
6 ms |
768 KB |
Output is correct |
11 |
Correct |
6 ms |
640 KB |
Output is correct |
12 |
Correct |
10 ms |
2688 KB |
Output is correct |
13 |
Correct |
11 ms |
4480 KB |
Output is correct |
14 |
Correct |
61 ms |
8184 KB |
Output is correct |
15 |
Correct |
6 ms |
640 KB |
Output is correct |
16 |
Correct |
82 ms |
4864 KB |
Output is correct |