Submission #229990

#TimeUsernameProblemLanguageResultExecution timeMemory
229990osaaateiasavtnlPohlepko (COCI16_pohlepko)C++14
80 / 80
56 ms22120 KiB
#include<bits/stdc++.h>
using namespace std;
#define int long long
#define ii pair <int, int>
#define app push_back
#define all(a) a.begin(), a.end()
#define bp __builtin_popcountll
#define ll long long
#define mp make_pair
#define f first
#define s second
#define Time (double)clock()/CLOCKS_PER_SEC
const int N = 2007;
int n, m;
string a[N];
ii par[N][N];
signed main() {
    #ifdef HOME
    freopen("input.txt", "r", stdin);
    #else
    #define endl '\n'
    ios_base::sync_with_stdio(0); cin.tie(0);
    #endif
    cin >> n >> m;
    for (int i = 0; i < n; ++i) {
        cin >> a[i];
    }   
    vector <ii> c = { {0, 0} };

    vector <ii> v = { {0, 1}, {1, 0} };

    for (int t = 0; t < n + m - 2; ++t) {
        char mn = 'z';
        for (auto p : c) {
            for (auto e : v) {
                int x = p.f + e.f;
                int y = p.s + e.s;
                if (0 <= x && x < n && 0 <= y && y < m) {
                    mn = min(mn, a[x][y]);
                }   
            }   
        }   
        vector <ii> to;
        for (auto p : c) {
            for (auto e : v) {
                int x = p.f + e.f;
                int y = p.s + e.s;
                if (0 <= x && x < n && 0 <= y && y < m && a[x][y] == mn) {
                    par[x][y] = p;
                    to.app(mp(x, y));
                }   
            }   
        }   
        sort(all(to));
        to.resize(unique(all(to)) - to.begin());
        c = to;
    }   
    int x = n - 1, y = m - 1;
    string ans;
    while (1) {
        ans += a[x][y];
        if (x == 0 && y == 0)
            break;
        tie(x, y) = par[x][y];
    }   
    reverse(all(ans));
    cout << ans << endl;
}
#Verdict Execution timeMemoryGrader output
Fetching results...