제출 #474869

#제출 시각아이디문제언어결과실행 시간메모리
474869dqkTracks in the Snow (BOI13_tracks)C++17
100 / 100
669 ms123868 KiB
#include <bits/stdc++.h>

int main() {
    std::ios_base::sync_with_stdio(false);
    std::cin.tie(nullptr);
    int n, m;
    std::cin >> n >> m;
    std::string g[n];
    for (int i = 0; i < n; ++i) {
        std::cin >> g[i];
    }
    auto inside=[&](int x, int y) {
        if (x >= 0 && x < n && y >= 0 && y < m && g[x][y] != '.')
            return true;
        return false;
    };
    std::vector<int> dx={1, -1, 0, 0}, dy={0, 0, 1, -1};
    std::vector<std::vector<int>> d(n, std::vector<int>(m, 0));
    std::deque<std::pair<int, int>> q;
    q.push_back({0, 0});
    d[0][0] = 1;
    int ans = 0;
    while (!q.empty()) {
        auto u = q.front();
        q.pop_front();
        ans = std::max(ans, d[u.first][u.second]);
        for (int i = 0; i < 4; ++i) {
            int x = u.first + dx[i];
            int y = u.second + dy[i];
            if (!inside(x, y)) 
                continue;
            if (d[x][y])
                continue;
            if (g[x][y] == g[u.first][u.second]) {
                d[x][y] = d[u.first][u.second];
                q.push_front({x, y});
            }
            else {
                d[x][y] = d[u.first][u.second] + 1;
                q.push_back({x, y});
            }
            ans = std::max(ans, d[x][y]);
        }
    }
    std::cout << ans << "\n";
    return 0;
}

#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...