#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]);
set<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;
set<pair<int, int>> next_layer;
for (auto [i, j] : layer) {
if (i + 1 < n && grid[i + 1][j] == mn) {
next_layer.emplace(i + 1, j);
}
if (j + 1 < m && grid[i][j + 1] == mn) {
next_layer.emplace(i, j + 1);
}
}
layer = next_layer;
}
cout << ans << endl;
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 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 |
2920 KB |
Output is correct |
8 |
Correct |
7 ms |
7004 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
604 KB |
Output is correct |
12 |
Correct |
1 ms |
1112 KB |
Output is correct |
13 |
Correct |
1 ms |
860 KB |
Output is correct |
14 |
Correct |
6 ms |
7004 KB |
Output is correct |
15 |
Correct |
2 ms |
504 KB |
Output is correct |
16 |
Correct |
62 ms |
3676 KB |
Output is correct |