// author : daohuyenchi
#include <bits/stdc++.h>
using namespace std;
#define ull unsigned long long
#define db double
#define i32 int32_t
#define i64 int64_t
#define ll long long
//
#define fi first
#define se second
// #define int long long // consider carefully
//
#define pii pair<int, int>
#define pli pair<ll, int>
#define pll pair<ll, ll>
#define pil pair<int, ll>
#define PAIR make_pair
// TIME IS LIMITED ...
#define rep(i, a, b) for (int i = (a), _b = (b); i <= _b; ++i)
#define repd(i, b, a) for (int i = (b), _a = (a); i >= _a; --i)
#define repv(v, H) for(auto &v: H)
// REFLECT ON THE PAST ...
#define RESET(c, x) memset(c, x, sizeof(c))
#define MASK(i) (1LL << (i))
#define BIT(mask, i) (((mask) >> (i)) & 1LL)
#define ONBIT(mask, i) ((mask) | (1LL << (i)))
#define OFFBIT(mask, i) ((mask) &~ (1LL << (i)))
#define COUNTBIT __builtin_popcountll
// 30 / 1 / 2024 ? love is zero... start from zero
#define PB push_back
#define EB emplace_back
#define vi vector<int>
#define vll vector<ll>
#define lwb lower_bound
#define upb upper_bound
#define all(v) (v).begin(), (v).end()
#define special(H) (H).resize(distance(H.begin(), unique(all(H))))
//
#define sp ' '
#define nl '\n'
#define EL {cerr << '\n';}
#define yes "YES"
#define no "NO"
#define Log2(n) (63 - __builtin_clzll(n))
#define left __left__
#define right __right__
//____________________________________________________________________
template <class X, class Y> bool maximize(X &a, const Y &b) {
if(a < b) return a = b, true;
return false;
}
template <class X, class Y> bool minimize(X &a, const Y &b) {
if(a > b) return a = b, true;
return false;
}
template <class... T>
void print(T&&... n) {
using exp = int[];
exp{0, (cerr << n << sp, 0)...};
cerr << nl;
}
template <class... T>
void print2(T&&... n) {
using exp = int[];
exp{0, (cerr << n << sp, 0)...};
// cerr << sp;
}
template <class T, class... C>
void assign(int n, T v, C&&... a) {
using e = int[];
e{(a.assign(n, v), 0)...};
}
template <class... C>
void resize(int n, C&&... a) {
using e = int[];
e{(a.resize(n), 0)...};
}
template <class T>
using vector2d = vector<vector<T>>;
template <class T>
using vector3d = vector<vector2d<T>>;
template <class T> int ssize(T &a) { return (int) a.size(); }
//____________________________________________________________________
mt19937 rng(chrono::steady_clock().now().time_since_epoch().count());
const int MOD = 1000000007;
// const int MOD[2] = {1000000009, 998244353};
template<class X> void modmize(X &x, int cur_Mod = MOD) {
if(x >= cur_Mod) x -= cur_Mod;
if(x < 0) x += cur_Mod;
}
const long long oo = 1e18 + 7;
const int INF = 2e9;
const int nmax = 1e6 + 10;
const int MAX = 2e5;
const int base = 311;
const db eps = 1e-6;
const int block = 500;
static const double PI = acos(-1.0);
//____________________________________________________________________
int n, m;
vector2d <char> a;
int dx[] = {0, 1};
int dy[] = {1, 0};
vector2d <int> dp;
int solve(int x, int y) {
if (x == n && y == m) return 1;
auto &ret = dp[x][y];
if (ret != -1) return ret;
int cur = 0;
for (int k = 0; k < 2 && cur == 0; ++k) {
int nx = x + dx[k];
int ny = y + dy[k];
if (nx > n || ny > m || a[nx][ny] == '#') continue;
cur = solve(nx, ny);
}
return ret = cur;
}
vector <pii> pat;
void trace(int x, int y) {
pat.push_back({x, y});
if (x == n && y == m) return;
priority_queue <pii> pq;
for (int k = 0; k < 2; ++k) {
int nx = x + dx[k];
int ny = y + dy[k];
if (nx > n || ny > m || a[nx][ny] == '#') continue;
if (solve(nx, ny)) {
pq.push({- a[nx][ny], k});
}
}
int k = pq.top().se;
int nx = x + dx[k];
int ny = y + dy[k];
trace(nx, ny);
return;
}
struct Node {
int t, x, y;
bool operator < (const Node &ot) const {
if (t != ot.t) return t > ot.t;
else {
return a[x][y] >= a[ot.x][ot.y];
}
}
} ;
set <pii> s[nmax * 2];
void tintingyn() {
cin >> n >> m;
assign(n + 2, vector <char>(m + 2, 'a'), a);
rep (i, 1, n) rep (j, 1, m) {
cin >> a[i][j];
}
assign(n + 2, vector <int>(m + 2, -1), dp);
queue <pii> q;
vector2d <bool> vis;
assign(n + 2, vector <bool> (m + 2, 0), vis);
vector <int> d(n + m + 2, -1);
d[1] = a[1][1] - 'a';
priority_queue <Node> pq;
pq.push({1, 1, 1});
while (pq.empty() == 0) {
auto cur_v = pq.top();
pq.pop();
int t = cur_v.t;
int x = cur_v.x;
int y = cur_v.y;
if (d[t] != a[x][y] - 'a') continue;
// print(t, x, y);
rep (k, 0, 1) {
int nx = x + dx[k];
int ny = y + dy[k];
if (nx > n || ny > m || a[nx][ny] == '#' || solve(nx, ny) == 0) continue;
if (d[t + 1] == -1 || d[t + 1] >= a[nx][ny] - 'a') {
if (s[t + 1].count(PAIR(nx, ny))) continue;
d[t + 1] = a[nx][ny] - 'a';
pq.push({t + 1, nx, ny});
s[t + 1].insert({nx, ny});
}
}
}
rep (i, 1, n + m - 1) {
cout << (char) (d[i] + 'a');
}
}
signed main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
//________________________________________________________________
#define TASK "MINTRIX"
if(fopen(TASK".inp", "r")) {
freopen(TASK".inp", "r", stdin);
freopen(TASK".out", "w", stdout);
}
//________________________________________________________________
// CODE FROM HERE ...!
int num_test = 1;
// cin >> num_test;
while(num_test--) {
tintingyn();
}
cerr << '\n' << "Time elapsed: " << (1.0 * clock() / CLOCKS_PER_SEC) << " s\n" << nl;
return 0;
}
Compilation message (stderr)
pohlepko.cpp: In function 'int main()':
pohlepko.cpp:246:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
246 | freopen(TASK".inp", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
pohlepko.cpp:247:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
247 | freopen(TASK".out", "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |