#include <bits/stdc++.h>
using namespace std;
#define endl '\n'
#define fast_io ios_base::sync_with_stdio(0);cin.tie(0);
typedef long long ll;
const int INF = 0x3f3f3f3f;
const ll LINF = 0x3f3f3f3f3f3f3f3fLL;
int main() {
fast_io;
int n, m;
cin >> n >> m;
vector<string> grid(n);
for (auto &x : grid) cin >> x;
string ans(1, grid[0][0]);
vector<pair<int, int>> layer = {{0, 0}};
for (int it = 1; it < n + m - 1; it++) {
char mn = 'z';
for (auto [i, j] : layer) {
if (i + 1 < n) {
mn = min(mn, grid[i + 1][j]);
}
if (j + 1 < m) {
mn = min(mn, grid[i][j + 1]);
}
}
ans += mn;
vector<pair<int, int>> next_layer;
for (auto [i, j] : layer) {
if (i + 1 < n && grid[i + 1][j] == mn) {
next_layer.emplace_back(i + 1, j);
}
if (j + 1 < m && grid[i][j + 1] == mn) {
next_layer.emplace_back(i, j + 1);
}
}
layer = next_layer;
}
cout << ans << endl;
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
600 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
344 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
860 KB |
Output is correct |
7 |
Correct |
3 ms |
3160 KB |
Output is correct |
8 |
Correct |
7 ms |
8536 KB |
Output is correct |
9 |
Correct |
1 ms |
344 KB |
Output is correct |
10 |
Correct |
2 ms |
604 KB |
Output is correct |
11 |
Correct |
3 ms |
604 KB |
Output is correct |
12 |
Correct |
2 ms |
1116 KB |
Output is correct |
13 |
Correct |
2 ms |
900 KB |
Output is correct |
14 |
Execution timed out |
1016 ms |
51008 KB |
Time limit exceeded |
15 |
Runtime error |
61 ms |
65536 KB |
Execution killed with signal 9 |
16 |
Runtime error |
59 ms |
65536 KB |
Execution killed with signal 9 |