#include <bits/stdc++.h>
#define F first
#define S second
#define LL long long
#define rep(i, x) for(auto &i : (x))
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define left(x) ((x)<<1)
#define right(x) ((x)>>1)
#define LSOne(x) (x & -(x))
using namespace std;
using T = pair<pair<int,char>, pair<int, int>>;
const int SZ = 2e5 + 1;
const LL MOD = 1e9 + 7;
const LL INF = 1e18 + 1;
LL bp (LL b, LL e, LL m = MOD) {
if ( e == 0 ) return 1;
LL T = bp(b, e / 2);
T *= T; T %= m;
if ( e & 1 ) T *= b;
return T %= m;
}
LL nv_i (LL a) { return bp(a, MOD - 2); };
LL gcd (LL a, LL b) { return (b == 0 ? a : gcd(b, a % b)); }
LL lcm(LL a, LL b) { return (a * (b / gcd(a, b))); }
LL ceil (LL a, LL b) { return ((a + b - 1) / b); }
vector<vector<char>> G;
vector<vector<bool>> VIS;
vector<char> LETTERS;
int n, m;
void bfs() {
queue<T> Q;
Q.push({{1, 'z'}, {1, 1}});
while(!Q.empty()) {
int dist = Q.front().F.F, i = Q.front().S.F, j = Q.front().S.S, prev_l = Q.front().F.S;
Q.pop();
if(VIS[i][j] || i > n || j > m || LETTERS[dist - 1] != prev_l) continue;
VIS[i][j] = 1;
LETTERS[dist] = min(LETTERS[dist], G[i][j]);
Q.push({{dist + 1, G[i][j]}, {i + 1, j}});
Q.push({{dist + 1, G[i][j]}, {i, j + 1}});
}
}
void solve() {
cin >> n >> m;
G.resize(2e3 + 1, vector<char> (2e3 + 1));
VIS.resize(2e3 + 1, vector<bool> (2e3 + 1));
LETTERS.resize(n + m + 1, 'z');
for(int i = 1; i <= n; i ++) {
for(int j = 1; j <= m; j ++) {
cin >> G[i][j];
}
}
bfs();
for(int i = 1; i < n + m; i ++) {
cout << LETTERS[i];
}
}
signed main() {
ios_base::sync_with_stdio(0);
cin.tie(0), cout.tie(0);
solve();
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
4944 KB |
Output is correct |
2 |
Correct |
4 ms |
4944 KB |
Output is correct |
3 |
Correct |
4 ms |
4944 KB |
Output is correct |
4 |
Correct |
4 ms |
4944 KB |
Output is correct |
5 |
Correct |
5 ms |
5112 KB |
Output is correct |
6 |
Correct |
6 ms |
4944 KB |
Output is correct |
7 |
Correct |
16 ms |
4944 KB |
Output is correct |
8 |
Runtime error |
40 ms |
9800 KB |
Execution killed with signal 11 |
9 |
Correct |
4 ms |
5116 KB |
Output is correct |
10 |
Correct |
5 ms |
4944 KB |
Output is correct |
11 |
Correct |
6 ms |
4944 KB |
Output is correct |
12 |
Correct |
8 ms |
4960 KB |
Output is correct |
13 |
Correct |
6 ms |
4944 KB |
Output is correct |
14 |
Runtime error |
45 ms |
9800 KB |
Execution killed with signal 11 |
15 |
Correct |
4 ms |
4944 KB |
Output is correct |
16 |
Correct |
28 ms |
5064 KB |
Output is correct |