#include <iostream>
using namespace std;
int n, m;
string a[2000+9];
bool vis[2000+9][2000+9];
bool check(int i, int j){
return i >= 0 && j >= 0 && i < n && j < m;
}
string finds(int i, int j, string s){
string sol="zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz";
s += a[i][j];
if( i == n-1 && j == m-1 )
return s;
if(check(i, j+1) && string(s+a[i][j+1]) < string(s+a[i+1][j]) && !vis[i][j+1]){
sol = min(string(finds(i, j+1, s)), sol);
vis[i][j+1]=true;
} else if(check(i+1, j) && string(s+a[i][j+1]) < string(s+a[i+1][j])){
sol = min(string(finds(i+1, j, s)), sol);
vis[i][j+1]=true;
} else{
if(check(i+1, j) && !vis[i+1][j]){
sol = min(string(finds(i+1, j, s)), sol);
vis[i+1][j]=true;
}
if(check(i, j+1) && !vis[i][j+1]){
sol = min(string(finds(i, j+1, s)), sol);
vis[i][j+1]=true;
}
}
return sol;
}
int main(){
cin >> n >> m;
for(int i=0;i<n;i++) cin >> a[i];
cout << finds(0, 0, "") << endl;
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
3 ms |
1784 KB |
Output is correct |
3 |
Correct |
2 ms |
248 KB |
Output is correct |
4 |
Correct |
4 ms |
504 KB |
Output is correct |
5 |
Correct |
15 ms |
1536 KB |
Output is correct |
6 |
Correct |
520 ms |
2848 KB |
Output is correct |
7 |
Execution timed out |
1083 ms |
10632 KB |
Time limit exceeded |
8 |
Execution timed out |
1077 ms |
26224 KB |
Time limit exceeded |
9 |
Incorrect |
6 ms |
632 KB |
Output isn't correct |
10 |
Incorrect |
71 ms |
1400 KB |
Output isn't correct |
11 |
Incorrect |
273 ms |
2804 KB |
Output isn't correct |
12 |
Incorrect |
992 ms |
5172 KB |
Output isn't correct |
13 |
Incorrect |
764 ms |
9864 KB |
Output isn't correct |
14 |
Execution timed out |
1084 ms |
25764 KB |
Time limit exceeded |
15 |
Correct |
22 ms |
760 KB |
Output is correct |
16 |
Execution timed out |
1076 ms |
11112 KB |
Time limit exceeded |