Submission #1328162

#TimeUsernameProblemLanguageResultExecution timeMemory
1328162rsinventorTracks in the Snow (BOI13_tracks)C++20
91.25 / 100
463 ms118800 KiB
#include <bits/stdc++.h>

using namespace std;
typedef long long ll;

/* DEBUGGING */
#ifdef LOCAL
#define dbg(v)                                                                 \
  cerr << "Line(" << __LINE__ << ") -> " << #v << " = " << (v) << endl;
#else
#define dbg(v)
#endif

/* MODULAR OPERATIONS */
#define MOD 998244353

int mul(int x, int y) { return x * 1ll * y % MOD; }

int binpow(int x, int y) {
    int z = 1;
    while (y) {
        if (y % 2 == 1)
            z = mul(z, x);
        x = mul(x, x);
        y /= 2;
    }
    return z;
}

int inv(int x) { return binpow(x, MOD - 2); }

int divide(int x, int y) { return mul(x, inv(y)); }

/* TYPES */
typedef vector<int> vi;
typedef vector <vector<int>> vvi;
typedef vector<bool> vb;
typedef vector <vector<bool>> vvb;
typedef vector <string> vs;
typedef vector<char> vc;
typedef vector <vector<char>> vvc;
typedef pair<int, int> pii;
typedef vector <pii> vpii;

/* UTILS */
#define all(a) (a).begin(), (a).end()
#define rall(a) (a).rbegin(), (a).rend()
#define pb push_back
#define endl "\n"
#define fi first
#define se second

#define INF 1e6
vpii dirs = {{-1, 0}, {1, 0}, {0, 1}, {0, -1}};
string arr[4005];
int vis[4005][4005];

signed main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    int h, w;
    cin >> h >> w;

    for(int i = 0; i < h; i++) {
        for(int j = 0; j < w; j++) {
            vis[i][j] = INF;
        }
    }

    for(int i = 0; i < h; i++) {
        cin >> arr[i];
    }

    deque<pii> deq;
    deq.pb({0, 0});

    vis[0][0] = 1;

    int res = -INF;
    while(!deq.empty()) {
        pii cur = deq.front();
        deq.pop_front();
        res = max(res, vis[cur.fi][cur.se]);

        for(pii& dir: dirs) {
            pii adj = {cur.fi + dir.fi, cur.se + dir.se};

            if(adj.fi < 0 || adj.fi >= h || adj.se < 0 || adj.se >= w) continue;
            if(arr[adj.fi][adj.se] == '.') continue;

            if(arr[adj.fi][adj.se]!=arr[cur.fi][cur.se] &&  vis[adj.fi][adj.se] > vis[cur.fi][cur.se] + 1) {
                vis[adj.fi][adj.se] = vis[cur.fi][cur.se] + 1;
                deq.push_back(adj);
            } else if(arr[adj.fi][adj.se]==arr[cur.fi][cur.se] && vis[adj.fi][adj.se] > vis[cur.fi][cur.se]) {
                vis[adj.fi][adj.se] = vis[cur.fi][cur.se];
                deq.push_front(adj);
            }
        }
    }

    cout << res << endl;

    return 0;
}

// DONT GET STUCK IN ONE APPROACH
// ROTATED PREFIX SUM: (x, y) => (x+y, n-x+y)
// NO SMALL VECTORS!!!
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...