제출 #1328153

#제출 시각아이디문제언어결과실행 시간메모리
1328153rsinventorTracks in the Snow (BOI13_tracks)C++20
18.65 / 100
668 ms133988 KiB
#include <bits/stdc++.h>

using namespace std;
typedef long long ll;

/* TYPES */
typedef pair<int, int> pii;
typedef vector <pii> vpii;

#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}};
int h, w;
string arr[4000];
int vis[4000][4000];

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

    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 = 1;

    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(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+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...