Submission #1143038

#TimeUsernameProblemLanguageResultExecution timeMemory
1143038hewhocooksTracks in the Snow (BOI13_tracks)C++17
100 / 100
495 ms210272 KiB
#include <bits/stdc++.h>
using namespace std;
#define int long long  // Keep for main logic
#define all(x) (x).begin(), (x).end()
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<int, int> pi;
#define f first
#define s second
#define pb push_back
#define endl "\n"

const int N = 4005;
int dx[] = {-1, 0, 1, 0};
int dy[] = {0, 1, 0, -1};

int n, m;
vector<string> snow;
vector<vector<int>> depth;  // Use dynamic allocation

bool inside(int x, int y) {
    return (x >= 0 && x < n && y >= 0 && y < m && snow[x][y] != '.');
}

void solve() {
    cin >> n >> m;
    
    snow.assign(n, "");   // Resize snow vector
    depth.assign(n, vector<int>(m, 0));  // Dynamically allocate depth

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

    int ans = 0;
    deque<pi> dq;

    if (inside(0, 0)) {
        dq.push_back({0, 0});
        depth[0][0] = 1;
    }

    while (!dq.empty()) {
        auto [x, y] = dq.front();
        dq.pop_front();
        ans = max(ans, depth[x][y]);

        for (int i = 0; i < 4; i++) {
            int newx = x + dx[i], newy = y + dy[i];

            if (inside(newx, newy) && depth[newx][newy] == 0) {
                if (snow[x][y] == snow[newx][newy]) {
                    depth[newx][newy] = depth[x][y];
                    dq.push_front({newx, newy});
                } else {
                    depth[newx][newy] = depth[x][y] + 1;
                    dq.push_back({newx, newy});
                }
            }
        }
    }
    cout << ans << endl;
}

signed main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
    int t = 1;
    // cin >> t;
    while (t--) {
        solve();
    }
    return 0;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...