Submission #1017098

#TimeUsernameProblemLanguageResultExecution timeMemory
1017098chayiTracks in the Snow (BOI13_tracks)C++17
100 / 100
419 ms131312 KiB
#include <bits/stdc++.h>

using namespace std;

int depth[4001][4001], ans = 1;
string s[4001];

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

    int n, m;
    cin >> n >> m;
    for (int i = 0; i < n; i++) {
        cin >> s[i];
    }
    int dx[]{0, -1, 0, 1, 0};

    auto good = [&](int x, int y) { return x >= 0 && x < n && y >= 0 && y < m && s[x][y] != '.'; };

    deque<pair<int, int>> dq;
    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 nx = x + dx[i], ny = y + dx[i + 1];

            if (good(nx, ny) && depth[nx][ny] == 0) {
                if (s[x][y] == s[nx][ny]) {
                    depth[nx][ny] = depth[x][y];
                    dq.push_front({nx, ny});
                } else {
                    depth[nx][ny] = depth[x][y] + 1;
                    dq.push_back({nx, ny});
                }
            }
        }
    }
    cout << ans;
    return 0;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...