/*
author: Haunted_Cpp
"Persistence guarantees that results are inevitable"
*/
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#pragma GCC optimize ("Ofast")
#pragma GCC target("fma,sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,avx2,tune=native")
#pragma GCC optimize("unroll-loops")
using namespace std;
const int N = 2e3 + 5;
int step [N][N];
char g [N][N];
char dist [2 * N];
const int dr [] = {+0, +1};
const int dc [] = {+1, +0};
int main () {
ios::sync_with_stdio(0);
cin.tie(0);
int r, c;
cin >> r >> c;
for (int i = 0; i < r; i++) {
for (int j = 0; j < c; j++) {
cin >> g[i][j];
step[i][j] = -1;
}
}
for (int i = 0; i < 2 * N; i++) dist[i] = 'z';
const int K = r + c - 1;
dist[0] = g[0][0];
step[0][0] = 0;
queue<pair<int, int> > q;
q.push({0, 0});
while (!q.empty()) {
int linha = q.front().first;
int coluna = q.front().second;
q.pop();
if (g[linha][coluna] != dist[step[linha][coluna]]) continue;
for (int i = 0; i < 2; i++) {
if (linha + dr[i] < 0 || linha + dr[i] >= r || coluna < 0 || coluna + dc[i] >= c) continue;
if (step[linha + dr[i]][coluna + dc[i]] != -1) continue;
step[linha + dr[i]][coluna + dc[i]] = step[linha][coluna] + 1;
dist[step[linha + dr[i]][coluna + dc[i]]] = min (dist[step[linha + dr[i]][coluna + dc[i]]], g[linha + dr[i]][coluna + dc[i]]);
q.push({linha + dr[i], coluna + dc[i]});
}
}
for (int i = 0; i < K; i++) cout << dist[i];
cout << '\n';
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
384 KB |
Output is correct |
2 |
Correct |
6 ms |
3328 KB |
Output is correct |
3 |
Correct |
5 ms |
384 KB |
Output is correct |
4 |
Correct |
5 ms |
640 KB |
Output is correct |
5 |
Correct |
5 ms |
384 KB |
Output is correct |
6 |
Correct |
9 ms |
3328 KB |
Output is correct |
7 |
Correct |
30 ms |
13440 KB |
Output is correct |
8 |
Correct |
73 ms |
19960 KB |
Output is correct |
9 |
Correct |
5 ms |
1024 KB |
Output is correct |
10 |
Correct |
6 ms |
1792 KB |
Output is correct |
11 |
Correct |
7 ms |
1536 KB |
Output is correct |
12 |
Correct |
12 ms |
7552 KB |
Output is correct |
13 |
Correct |
13 ms |
13056 KB |
Output is correct |
14 |
Correct |
73 ms |
19960 KB |
Output is correct |
15 |
Correct |
5 ms |
1024 KB |
Output is correct |
16 |
Correct |
43 ms |
15104 KB |
Output is correct |