Submission #1328148

#TimeUsernameProblemLanguageResultExecution timeMemory
1328148rsinventorTracks in the Snow (BOI13_tracks)C++20
91.25 / 100
2126 ms1114112 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[4000];
int vis[4000][4000];

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;
        cin >> arr[i];
    }

    deque<pii> deq;
    deq.pb({0, 0});
    vis[0][0] = 0;
    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) {
            int i = cur.fi, j = cur.se;
            if(i+dir.fi < 0 || i+dir.fi >= h || j+dir.se < 0 || j+dir.se >= w) continue;
            if(arr[i+dir.fi][j+dir.se] == '.') continue;
            pair<pii, int> a = {{i+dir.fi, j+dir.se}, arr[i+dir.fi][j+dir.se]!=arr[i][j]};

            if(vis[a.fi.fi][a.fi.se] < INF) continue;
            if(a.se) {
                vis[a.fi.fi][a.fi.se] = vis[cur.fi][cur.se] + 1;
                deq.push_back(a.fi);
            } else {
                vis[a.fi.fi][a.fi.se] = vis[cur.fi][cur.se];
                deq.push_front(a.fi);
            }
        }
    }

    cout << res+1 << 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...