#include<bits/stdc++.h>
using namespace std;
#define int long long
#define ii pair <int, int>
#define app push_back
#define all(a) a.begin(), a.end()
#define bp __builtin_popcountll
#define ll long long
#define mp make_pair
#define f first
#define s second
#define Time (double)clock()/CLOCKS_PER_SEC
const int N = 2007;
int n, m;
string a[N];
ii par[N][N];
signed main() {
#ifdef HOME
freopen("input.txt", "r", stdin);
#else
#define endl '\n'
ios_base::sync_with_stdio(0); cin.tie(0);
#endif
cin >> n >> m;
for (int i = 0; i < n; ++i) {
cin >> a[i];
}
vector <ii> c = { {0, 0} };
vector <ii> v = { {0, 1}, {1, 0} };
for (int t = 0; t < n + m - 2; ++t) {
char mn = 'z';
for (auto p : c) {
for (auto e : v) {
int x = p.f + e.f;
int y = p.s + e.s;
if (0 <= x && x < n && 0 <= y && y < m) {
mn = min(mn, a[x][y]);
}
}
}
vector <ii> to;
for (auto p : c) {
for (auto e : v) {
int x = p.f + e.f;
int y = p.s + e.s;
if (0 <= x && x < n && 0 <= y && y < m && a[x][y] == mn) {
par[x][y] = p;
to.app(mp(x, y));
}
}
}
sort(all(to));
to.resize(unique(all(to)) - to.begin());
c = to;
}
int x = n - 1, y = m - 1;
string ans;
while (1) {
ans += a[x][y];
if (x == 0 && y == 0)
break;
tie(x, y) = par[x][y];
}
reverse(all(ans));
cout << ans << endl;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
384 KB |
Output is correct |
2 |
Correct |
5 ms |
2432 KB |
Output is correct |
3 |
Correct |
4 ms |
384 KB |
Output is correct |
4 |
Correct |
5 ms |
640 KB |
Output is correct |
5 |
Correct |
5 ms |
512 KB |
Output is correct |
6 |
Correct |
6 ms |
2304 KB |
Output is correct |
7 |
Correct |
10 ms |
8576 KB |
Output is correct |
8 |
Correct |
18 ms |
16640 KB |
Output is correct |
9 |
Correct |
5 ms |
872 KB |
Output is correct |
10 |
Correct |
5 ms |
1408 KB |
Output is correct |
11 |
Correct |
5 ms |
1152 KB |
Output is correct |
12 |
Correct |
7 ms |
5120 KB |
Output is correct |
13 |
Correct |
9 ms |
8960 KB |
Output is correct |
14 |
Correct |
18 ms |
16640 KB |
Output is correct |
15 |
Correct |
6 ms |
1152 KB |
Output is correct |
16 |
Correct |
56 ms |
22120 KB |
Output is correct |