#include <bits/stdc++.h>
using namespace std;
int main() {
// your code goes here
int n , m;
cin >> n >> m;
char arr[n][m];
for(int i = 0; i < n; i++){
for(int j = 0; j < m; j++)cin >> arr[i][j];
}
bool vis[n][m];
memset(vis,0,sizeof vis);
vis[0][0] = 1;
if(n > 1)vis[1][0] = 1;
if(m > 1)vis[0][1] = 1;
for(int di = 1; di < n + m; di++){
char mn = 'z';
for(int j = 0; j <= min(di , m - 1); j++){
if(di - j >= n){
continue;
}
int i = di - j;
if(vis[i][j]){
mn = min(mn , arr[i][j]);
}
}
for(int j = 0; j <= min(di , m - 1); j++){
if(di - j >= n){
continue;
}
int i = di - j;
if(vis[i][j]){
if(mn == arr[i][j]){
if(i + 1 < n)vis[i + 1][j] = 1;
if(j + 1 < m)vis[i][j + 1] = 1;
}
else vis[i][j] = 0;
}
}
}
string ans = "";
ans += arr[n-1][m-1];
int i = n-1 , j = m - 1;
while(i != 0 || j != 0){
if(i - 1 >= 0 && vis[i-1][j]){
i--;
ans += arr[i][j];
}
else {
j--;
ans += arr[i][j];
}
}
reverse(ans.begin() , ans.end());
cout << ans << '\n';
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
292 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
3 |
Correct |
0 ms |
292 KB |
Output is correct |
4 |
Correct |
1 ms |
204 KB |
Output is correct |
5 |
Correct |
2 ms |
204 KB |
Output is correct |
6 |
Correct |
18 ms |
888 KB |
Output is correct |
7 |
Correct |
79 ms |
4196 KB |
Output is correct |
8 |
Correct |
244 ms |
11980 KB |
Output is correct |
9 |
Correct |
1 ms |
204 KB |
Output is correct |
10 |
Correct |
4 ms |
388 KB |
Output is correct |
11 |
Correct |
8 ms |
640 KB |
Output is correct |
12 |
Correct |
20 ms |
1224 KB |
Output is correct |
13 |
Correct |
12 ms |
800 KB |
Output is correct |
14 |
Correct |
230 ms |
11972 KB |
Output is correct |
15 |
Correct |
1 ms |
332 KB |
Output is correct |
16 |
Correct |
89 ms |
4908 KB |
Output is correct |