#include <bits/stdc++.h>
using namespace std;
int main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
int n, m;
cin >> n >> m;
vector<vector<int>> mark(n, vector<int>(m));
vector<string> a(n);
for (string &s : a) cin >> s;
vector<pair<int, int>> p;
for (int j = 1; j < m; j++) p.emplace_back(0, j);
for (int i = 1; i < n; i++) p.emplace_back(i, m - 1);
cout << a[0][0];
mark[0][0] = 1;
for (auto [x, y] : p) {
vector<tuple<char, int, int>> s;
while (x < n && y >= 0) {
if ((x > 0 && mark[x - 1][y]) || (y > 0 && mark[x][y - 1])) {
s.emplace_back(a[x][y], x, y);
}
x++;
y--;
}
sort(s.begin(), s.end());
cout << get<0>(s[0]);
for (auto [c, _x, _y] : s) {
if (c == get<0>(s[0])) {
mark[_x][_y] = 1;
}
}
}
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |