#include <bits/stdc++.h>
using namespace std;
int n;
int m;
string ans = "{";
vector<vector<char>> board;
void bt(int i, int j, string cur){
if(i==n-1 and j==m-1){
ans = cur;
return;
}
if(i==n-1){
j++;
cur+=board[i][j];
if(cur<=ans){
ans = cur;
bt(i,j,cur);
}
return;
}
else if(j==m-1){
i++;
cur+=board[i][j];
if(cur<=ans){
ans = cur;
bt(i,j,cur);
}
return;
}
else{
if(board[i+1][j]!=board[i][j+1]){
if(board[i+1][j]<board[i][j+1]) i++;
else j++;
cur += board[i][j];
if(cur<=ans){
ans = cur;
bt(i,j,cur);
}
return;
}
else{
cur += board[i+1][j];
if(cur<=ans){
ans = cur;
bt(i+1,j,cur);
bt(i,j+1,cur);
}
return;
}
}
}
int main(){
cin >> n >> m;
board = vector<vector<char>>(n,vector<char>(m));
for(int i=0;i<n;i++){
for(int j=0;j<m;j++){
cin >> board[i][j];
}
}
string c;
c += board[0][0];
bt(0,0,c);
cout << ans << endl;
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Incorrect |
0 ms |
344 KB |
Output isn't correct |
3 |
Incorrect |
0 ms |
344 KB |
Output isn't correct |
4 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
5 |
Incorrect |
1 ms |
344 KB |
Output isn't correct |
6 |
Incorrect |
8 ms |
604 KB |
Output isn't correct |
7 |
Incorrect |
42 ms |
1804 KB |
Output isn't correct |
8 |
Incorrect |
130 ms |
4188 KB |
Output isn't correct |
9 |
Incorrect |
1 ms |
348 KB |
Output isn't correct |
10 |
Incorrect |
2 ms |
344 KB |
Output isn't correct |
11 |
Incorrect |
4 ms |
348 KB |
Output isn't correct |
12 |
Incorrect |
14 ms |
796 KB |
Output isn't correct |
13 |
Incorrect |
6 ms |
696 KB |
Output isn't correct |
14 |
Incorrect |
134 ms |
4400 KB |
Output isn't correct |
15 |
Incorrect |
1 ms |
344 KB |
Output isn't correct |
16 |
Incorrect |
49 ms |
1884 KB |
Output isn't correct |