#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ar array
const int mxN=2000;
int n, m;
string g[mxN], ans;
bool vis[mxN][mxN];
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cin >> n >> m;
for (int i=0; i<n; ++i)
cin >> g[i];
vector<ar<int, 2>> q(1, {0, 0});
while(q.size()) {
ans+=g[q.front()[0]][q.front()[1]];
int mn=69;
vector<ar<int, 2>> nq;
auto ck=[&](int i, int j) {
if (i>=n||j>=m||vis[i][j])
return;
vis[i][j]=1;
int cur=g[i][j]-'a';
if (cur<mn) {
mn=cur;
nq={{i, j}};
} else if (cur==mn)
nq.push_back({i, j});
};
for (ar<int, 2> i : q) {
ck(i[0]+1, i[1]);
ck(i[0], i[1]+1);
}
swap(q, nq);
}
cout << ans;
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
380 KB |
Output is correct |
2 |
Correct |
1 ms |
1356 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
460 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
2 ms |
1548 KB |
Output is correct |
7 |
Correct |
5 ms |
5708 KB |
Output is correct |
8 |
Correct |
14 ms |
12324 KB |
Output is correct |
9 |
Correct |
1 ms |
516 KB |
Output is correct |
10 |
Correct |
1 ms |
844 KB |
Output is correct |
11 |
Correct |
1 ms |
844 KB |
Output is correct |
12 |
Correct |
3 ms |
3020 KB |
Output is correct |
13 |
Correct |
4 ms |
4620 KB |
Output is correct |
14 |
Correct |
18 ms |
12324 KB |
Output is correct |
15 |
Correct |
1 ms |
588 KB |
Output is correct |
16 |
Correct |
14 ms |
6476 KB |
Output is correct |